tensorflow的基本用法(七)——使用MNIST训练神经网络
来源:互联网 发布:一键海淘 知乎 编辑:程序博客网 时间:2024/06/05 01:50
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
本文主要是使用tensorflow和mnist数据集来训练神经网络。
#!/usr/bin/env python# _*_ coding: utf-8 _*_import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data# 下载mnist数据mnist = input_data.read_data_sets('MNIST_data', one_hot=True)# 定义神经网络模型的评估部分def compute_accuracy(W, b): # 定义测试数据的placeholder x = tf.placeholder(tf.float32, [None, 784]) # 定义测试数据的真实标签的placeholder y_ = tf.placeholder(tf.float32, [None, 10]) # 定义预测值 y = tf.nn.softmax(tf.matmul(x, W) + b) # 判断预测值y和真实值y_中最大数的索引是否一致,y的值为1-10概率 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) # 计算准确率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 输入测试数据,执行准确率的计算并返回 return sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels})# 定义神经网络模型的训练部分# 下面定义的神经网络只有一层W*x+b# 定义输入数据placeholder,不定义输入样本的数目——None,但定义每个样本的大小为784x = tf.placeholder(tf.float32, [None, 784])# 定义神经网络层的权重参数W = tf.Variable(tf.zeros([784, 10]))# 定义神经网络层的偏置参数b = tf.Variable(tf.zeros([10]))# 定义一层神经网络运算,激活函数为softmaxy = tf.nn.softmax(tf.matmul(x, W) + b)# 定义训练数据真实标签的placeholdery_ = tf.placeholder(tf.float32, [None, 10])# 定义损失函数,损失函数为交叉熵,reduction_indices表示沿着tensor的哪个纬度来求和cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))# 定义神经网络的训练步骤,使用的是梯度下降法,学习率为0.5train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)# 初始化所有变量init = tf.global_variables_initializer()# 定义Sessionsess = tf.Session()# 执行变量的初始化sess.run(init)# 迭代进行训练for i in range(1000): # 取出mnist数据集中的100个数据 batch_xs, batch_ys = mnist.train.next_batch(100) # 执行训练过程并传入真实数据x, y_ sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) if i % 100 == 0: print compute_accuracy(W, b)
执行结果如下:
Extracting MNIST_data/train-images-idx3-ubyte.gzExtracting MNIST_data/train-labels-idx1-ubyte.gzExtracting MNIST_data/t10k-images-idx3-ubyte.gzExtracting MNIST_data/t10k-labels-idx1-ubyte.gz0.40750.89480.90310.90740.90370.91250.91580.9120.91810.9169
0 0
- tensorflow的基本用法(七)——使用MNIST训练神经网络
- tensorflow的基本用法(九)——定义卷积神经网络训练MNIST
- tensorflow的基本用法(五)——创建神经网络并训练
- tensorflow的基本用法(六)——神经网络可视化
- win10 tensorflow使用笔记——安装及训练mnist
- Tensorflow训练神经网络实现mnist手写识别
- 深度学习笔记——TensorFlow学习笔记(三)使用TensorFlow实现的神经网络进行MNIST手写体数字识别
- tensorflow的基本用法(十)——保存神经网络参数和加载神经网络参数
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试(MNIST For ML Beginners)
- TensorFlow训练mnist数据集(卷积神经网络lenet5)
- TensorFlow 入门之第一个神经网络训练 MNIST
- Tensorflow中mnist数据使用CNN训练
- 使用Google tensorflow 训练MNIST数据集
- 卷积神经网络(CNN)和Tensorflow初探——MNIST
- (四)Tensorflow学习之旅——MNIST分类的卷积神经网络CNN示例
- TensorFlow神经网络的训练过程
- 将eclipse工程导入到Android Studio时遇到的坑
- kali rolling upgrade后重启输入账户密码后死机
- 2017.04.15学习
- Android 点击变样式(变色)。
- spring cloud discovery bootstrap first 非对称加密坑
- tensorflow的基本用法(七)——使用MNIST训练神经网络
- 自定义test之java对象属性拷贝简单实现
- Scheme 语言概要(上)
- day01-04
- ApplicationContext——FileSystemXmlApplication(二)
- spark RDD算子(一) parallelize,makeRDD,textFile
- day05-08
- BNUOJ 52297 Coins——打表
- day09-10