tensorflow每个变量封装了一个程序,需要通过sess.run 进行调用

接下来我们使用一下使用mnist数据,这是一个手写图像的数据,训练集是55000*28*28, 测试集10000* 28*28

第一步:导入数据

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
# 导入数据
mnist = input_data.read_data_sets('data/', one_hot=True) print (" tpye of 'mnist' is %s" % (type(mnist)))
print (" number of trian data is %d" % (mnist.train.num_examples))
print (" number of test data is %d" % (mnist.test.num_examples)) training = mnist.train.images
traininglable = mnist.train.labels
testing = mnist.test.images
testinglabel = mnist.test.labels

第二步:初识化变量

#初始化x和y
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float', [None, 10])
# 初始化W和b
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10])) sess = tf.Session()

第三步: 构造初始化函数

# 构造多分类方程
actv = tf.nn.softmax(tf.matmul(x, W) + b)
# 构造代价函数y*log(y1), y1表示的是预测值
cost = tf.reduce_mean(-tf.reduce_sum(y*tf.log(actv), reduction_indices=1))
#训练模型 learning_rate = 0.01
#优化模型,使得cost最小化
optm = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # 预测结果的最大值索引与真实值的索引进行比对, tf.argmax( , 1) #找出一行中的最大值的索引
pred = tf.equal(tf.argmax(actv, 1), tf.argmax(y, 1))
# 计算正确率, tf.cast 把布尔值转换为数字形式
accr = tf.reduce_mean(tf.cast(pred, 'float'))

第四步:迭代优化参数

init = tf.global_variables_initializer()

# 训练次数
train_epoches = 50
# 每次抽取样本数
batch_size = 100
# 每5次循环打印一次结果
display_step = 5
sess = tf.Session()
sess.run(init) for train_epoch in range(train_epoches):
avg_cost = 0
# 每次选取100个数据,循环的次数
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
# 抽取数据
bacth_x, bacth_y = mnist.train.next_batch(batch_size)
# 进行cost优化
sess.run(optm, feed_dict={x:bacth_x, y:bacth_y})
# 加上cost的值
feeds = {x:bacth_x, y:bacth_y}
avg_cost += sess.run(cost, feed_dict=feeds)/num_batch
# 每5次打印一次结果
if train_epoch % display_step == 0:
feeds_train = {x:bacth_x, y:bacth_y}
feed_test = {x:mnist.test.images, y:mnist.test.labels}
# 计算训练集的准确率, feed_dict的参数
train_acc = sess.run(accr, feed_dict=feeds_train)
# 计算测试集的准确率
test_acc = sess.run(accr, feed_dict=feed_test)
print("Epoch: %03d/%03d cost: %.9f train_acc: %.3f test_acc: %.3f"
% (train_epoch, train_epoches, avg_cost, train_acc, test_acc))

跟我学算法-tensorflow 实现logistics 回归的更多相关文章

  1. 跟我学算法- tensorflow 实现RNN操作

    对一张图片实现rnn操作,主要是通过先得到一个整体,然后进行切分,得到的最后input结果输出*_w[‘out’] + _b['out']  = 最终输出结果 第一步: 数据载入 import ten ...

  2. 跟我学算法- tensorflow VGG模型进行测试

    我们使用的VGG模型是别人已经训练好的一个19层的参数所做的一个模型 第一步:定义卷积分部操作函数 mport scipy.io import numpy as np import os import ...

  3. 跟我学算法-tensorflow 实现卷积神经网络附带保存和读取

    这里的话就不多说明了,因为上上一个博客已经说明了 import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...

  4. 跟我学算法- tensorflow模型的保存与读取 tf.train.Saver()

    save =  tf.train.Saver() 通过save. save() 实现数据的加载 通过save.restore() 实现数据的导出 第一步: 数据的载入 import tensorflo ...

  5. 跟我学算法-tensorflow 实现卷积神经网络

    我们采用的卷积神经网络是两层卷积层,两层池化层和两层全连接层 我们使用的数据是mnist数据,数据训练集的数据是50000*28*28*1 因为是黑白照片,所以通道数是1 第一次卷积采用64个filt ...

  6. 跟我学算法-tensorflow 实现神经网络

    神经网络主要是存在一个前向传播的过程,我们的目的也是使得代价函数值最小化 采用的数据是minist数据,训练集为50000*28*28 测试集为10000*28*28 lable 为50000*10, ...

  7. 跟我学算法-tensorflow 实现线性拟合

    TensorFlow™ 是一个开放源代码软件库,用于进行高性能数值计算.借助其灵活的架构,用户可以轻松地将计算工作部署到多种平台(CPU.GPU.TPU)和设备(桌面设备.服务器集群.移动设备.边缘设 ...

  8. 跟我学算法- tensorflow 卷积神经网络训练验证码

    使用captcha.image.Image 生成随机验证码,随机生成的验证码为0到9的数字,验证码有4位数字组成,这是一个自己生成验证码,自己不断训练的模型 使用三层卷积层,三层池化层,二层全连接层来 ...

  9. logistics回归简单应用——梯度下降,梯度上升,牛顿算法(一)

    警告:本文为小白入门学习笔记 由于之前写过详细的过程,所以接下来就简单描述,主要写实现中遇到的问题. 数据集是关于80人两门成绩来区分能否入学: 数据集: http://openclassroom.s ...

随机推荐

  1. Python系列文章索引

    >>> import this The Zen of Python, by Tim Peters Beautiful is better than ugly. Explicit is ...

  2. tab页面自动跳转原因【在控制ul和li的时候没有细分】

    效果图 存储buy的tab跳转js代码 $(function() { $('.tabPanel ul li').click(function(){ $(this).addClass('hit').si ...

  3. BZOJ3609 Heoi2014 人人尽说江南好【推理+结论】

    BZOJ3609 Heoi2014 人人尽说江南好 Description 小 Z 是一个不折不扣的 ZRP(Zealot Round-game Player,回合制游戏狂热玩家),最近他 想起了小时 ...

  4. Git与github常用命令

    Git项目与github建立联系 首先,需要在github上建立一个repository mkdir github-project cd github-project git init 此时githu ...

  5. Oracle数据库安装完成后相关问题的解决

    笔者一直以来都是使用公司服务器上的oracle数据库,突然一天公司服务器宕机了,项目无法访问数据库跟着瘫痪了,所以准备在自己的机器上安装一个oracle数据库. 从官网下载安装了oracle 11g后 ...

  6. Python--线性代数篇

    讲解Python在线性代数中的应用,包括: 一.矩阵创建 先导入Numpy模块,在下文中均采用np代替numpy import numpy as np 矩阵创建有两种方法,一是使用np.mat函数或者 ...

  7. HL7 Tools suite

    HL7的官网有很多开源工具, 比如:RoseTree,V3Generator,RMIM Designer, Design Repository, V2 & V3 Mapping Tools等. ...

  8. css loading 效果

    .loading{ width:160px; height:56px; position: absolute; top:50%; left:50%; line-height:56px; color:# ...

  9. Java 面向对象 初探

    public class test { public static void main(String[] args) { // 利用new关键字创建了一个Person对象 Person p = new ...

  10. 20181122_C#中AOP初探_装饰器模式的AOP_Remoting实现AOP_Castle实现AOP

    一.   什么是AOP: a)         AOP是面向切面编程; 就像oop一样, 它也是一种编程思想; i.    Oop思想→一切皆对象, 对象交互组成功能, 功能叠加组成模块, 模块叠加组 ...