Tensorflow实现简单Minist
来源:互联网 发布:linux新建用户并授权 编辑:程序博客网 时间:2024/05/20 01:11
TF安装成功后,我们就开始进入实战吧!
拿Minist作为例子;
有两种方式去下来Minist数据:
1、在Yann Lecun网站上载MNIST数据
2、用Python代码实现MNIST
数据的自动下载
在此,我们采用第二种方式去下载数据,代码如下:
from tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
载完成功后,会在屏幕上出现如下的信息:
分别为训练数据和测试数据。
import tensorflow as tfx = tf.placeholder(tf.float32, [None, 784])
这里设置op;
每个图片大小为28*28=784,即每个图片的像素点数。
y_ = tf.placeholder(tf.float32, [None, 10])
同理设置op;
每个图片的label是从0到9的期中一个数。
如果图片为2,那么在第三个位置上的值就为1。(从0开始的)W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
为模型创建相应的权值及其偏好y = tf.nn.softmax(tf.matmul(x, W) + b)
利用矩阵法则和softmax函数 效果:y={softmax}(Wx+b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(y),reduction_indices=[1]))
利用“交叉熵”
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
利用GD算法
init = tf.global_variables_initializer()
初始化我们所创建的变量sess=tf.Session();sess.run(init)
启动模型
for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
在此,需说明cast函数。
例如,原来correct_prediction的数据格式是bool,那么将其转化成float32以后,就能够将其转化成0和1的序列。
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
运行整个代码得出如下结果:
至此,一个TF入门程序就测试完毕。
0 0
- Tensorflow实现简单Minist
- python tensorflow 使用minist数据集实现手写数字识别
- Tensorflow 实现MINIST数据集多分类问题
- Tensorflow学习:MINIST手写体
- tensorflow 加载minist数据
- Tensorflow minist单层感知机
- tensorflow minist高阶训练代码
- tensorflow 学习(1) minist数据初入
- tensorflow minist数据集分类笔记
- tensorflow的第一个程序MINIST
- TensorFlow源码阅读——tensorflow/examples/tutorials/minist目录
- Tensorflow简单CNN实现
- 简单的Tensorflow实现NN
- Tensorflow实现简单图像探索
- TensorFlow实现简单卷积网络
- Tensorflow CNN 的简单实现
- 【Tensorflow网络架构简单实现】 用Tensorflow实现VGG模型
- TensorFlow实现简单的手写字体识别
- 位与运算与取余
- TCP-IP详解之TCP的坚持定时器5
- 利用结构体实现建议通讯录
- hdu1008
- opencv背景去除建模(BSM)
- Tensorflow实现简单Minist
- 凯立德(亿力吉奥)导航_地图缩放比例设置
- (10)关卡编辑器之视口显示标志
- libgmp问题记录
- [leetcode: Python]283. Move Zeroes
- Java中的数据类型
- Cookiejar处理cookie
- 好朋友
- 《锋利的jQuery》学习笔记---第3章 jQuery中的DOM操作