tensorflow程序-最简单softmax模型
来源:互联网 发布:盘古数据公司怎么样 编辑:程序博客网 时间:2024/06/03 19:07
#-*- coding=utf-8 -*-import numpyimport tensorflow as tfimport input_data#读取手写字体数据源 [None,28*28]mnist = input_data.read_data_sets("/home/tensorflow/tensorflow_workspace/data/", one_hot=True)x = tf.placeholder("float",[None,28*28]); #定义输入变量的大小[None,28*28]W = tf.Variable(tf.zeros([28*28,10])); #tf类型的一个变量。 W矩阵的大小为[28*28,10]b = tf.Variable(tf.zeros([10])); #tf类型的一个变量。 偏执的大小为[10]pre_y = tf.nn.softmax( tf.matmul(x,W) + b ); # pre_y: 存放预测的概率分布,大小为[None,10]rel_y = tf.placeholder("float",[None,10]); # rel_y: 存放实际的分布cross_entropy = -tf.reduce_sum( rel_y * tf.log(pre_y) ); #交叉熵作为损失函数learn_rate = 0.01train_step = tf.train.GradientDescentOptimizer(learn_rate).minimize(cross_entropy);with tf.Session() as sess:sess.run( tf.initialize_all_variables() ); #以上的variables只有在该行后生效#训练for i in range(2000): #训练2000次 batch_x, batch_y = mnist.train.next_batch(100); #每次从样本中随机选择100个样本训练sess.run( train_step, feed_dict = {x:batch_x, rel_y:batch_y} ); #训练模型#评估correct_prediction = tf.equal( tf.argmax(pre_y,1), tf.argmax(rel_y,1) );accuracy = tf.reduce_mean( tf.cast(correct_prediction,"float") );print sess.run(accuracy, feed_dict = {x:mnist.test.images, rel_y:mnist.test.labels});
函数:tf.argmax(pre_y,1)
表示将预测的结果集pre_y(形如:[[0,1],[1,0],[1,0],......])转换成最大值的id (形如:[1,0,0,......])
函数:tf.equal(tfvariable_a,tfvariable_b)
表示 判断tfvariable_a和tfvariable_b两个向量有哪些位置是相等的,最后返回:[True,False,False,......]
函数:tf.cast(correct_prediction,"float")
表示将:形如[False,False,True,......]的向量转换成float型向量如:[0,0,1,......]
最终返回测试正确率为:0.914
0 0
- tensorflow程序-最简单softmax模型
- tensorflow程序-最简单的CNN模型
- Tensorflow简单复现softmax模型对于mnist数据集分类
- MNIST(一):最简单的softmax regression 模型训练
- Tensorflow的Helloword:使用简单Softmax Regression模型来识别Mnist手写数字
- TensorFlow上实现Softmax回归模型
- TensorFlow Softmax
- tensorflow基于softmax模型实现手写体MNIST数字集识别
- TensorFlow学习笔记(1):最简单的入门程序
- softmax模型
- TensorFlow softmax VS sparse softmax
- tensorflow实现softmax回归(softmax regression)——简单的MNIST识别(第一课)
- tensorflow搭建简单回归模型
- Tensorflow实现softmax回归
- TensorFlow MNIST (Softmax)
- Tensorflow之softmax学习
- Tensorflow 01: mnist-softmax
- TensorFlow实现Softmax
- 图像特征提取三大法宝:HOG特征,LBP特征,Haar特征
- petalinux-package qspi启动镜像的问题
- .NET-TreeView选中节点失去焦点后颜色仍突出显示
- nginx日志过大导致磁盘不够
- Java序列化(Serializable)与反序列化
- tensorflow程序-最简单softmax模型
- fragment-activitys
- Hive中小表与大表关联(join)的性能分析
- Anaconda Navigator打不开
- HashMap内存泄露
- memset函数分析
- Combinations
- linux编程手册读书笔记第一章(20140329)
- highchart图表drilldown钻取功能及event点击事件添加--补充钻取多图显示(2)