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. hessian 协议 版本 兼容

    环境 : 服务端:  hessian 4.0.38 , spring 4.3.6 ; spring文档指出spring4.0以上的版本只能使用hessian 4.0以上的版本 客户端: hessian ...

  2. Cloudify介绍

    一篇关于Cloudify的介绍,可以看一下:http://timeson.iteye.com/blog/1699730

  3. js缓动函数

    tween: { easeInQuad: function(pos){ return Math.pow(pos, 2); }, easeOutQuad: function(pos){ return - ...

  4. 每天一个linux命令:【转载】touch命令

    linux的touch命令不常用,一般在使用make的时候可能会用到,用来修改文件时间戳,或者新建一个不存在的文件. 1.命令格式: touch [选项]... 文件... 2.命令参数: -a    ...

  5. Codeforces 633H Fibonacci-ish II【线段树】

    LINK 题目大意 给你一个序列a,Q次询问,每次询问\([l,r]\) 把\([l,r]\)的数排序去重,得到序列b,f是斐波那契数列 求\(\sum_{b=1}^{len} b_if_i\) 思路 ...

  6. tableview小结-初学者的问题

    初学者的问题主要集中在,下面几个问题: 一.几个函数总是不被调用:例如: - (UIView *)tableView:(UITableView *)tableView viewForHeaderInS ...

  7. Scoi 2010 幸运数字

    [题目描述]在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的“幸运号码”是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是“幸运号码”!但是这种“幸 ...

  8. 使用npm install报错- operation not permitted解决

    原文:https://blog.csdn.net/weixin_41715295/article/details/79508104 这几天使用npm install时一直报错-4048 operati ...

  9. normalizr api 转换类库使用

    1. 项目初始化 yarn init yarn add normalizr 项目结构 app.js package.json user.json 2. 使用 a. app.js const userj ...

  10. Nunit中如何进行事务性单元测试

    单元测试要求:单元测试方法并不真正去变更数据库,也就是说单元测试不依赖于数据库中的数据.那我们如何解决执行单元测试方法后,不变更数据库中数据呢? 一般的解决方案有两种: 1. 新建一个单元测试数据库, ...