前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Tensorflow mnist 数据集测试代码 + 自己下载数据

Tensorflow mnist 数据集测试代码 + 自己下载数据

作者头像
用户1148525
发布2019-05-27 12:13:15
1.3K0
发布2019-05-27 12:13:15
举报

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1436560

https://blog.csdn.net/weixin_39673686/article/details/81068582

代码语言:javascript
复制
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#  自己下载 MNIST_data 数据集,  csdn  上下载很快
mnist_data_folder="/home/zhangjun/miniconda3/envs/tensorflow/MNIST_data"  
mnist=input_data.read_data_sets(mnist_data_folder,one_hot=True)

x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.matmul(x, W) + b

# Define loss and optimizer 

y_ = tf.placeholder(tf.float32, [None, 10]) 

# The raw formulation of cross-entropy, 

# # tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.nn.softmax(y)), 

# reduction_indices=[1])) # 
# can be numerically unstable. # 
# So here we use tf.nn.softmax_cross_entropy_with_logits on the raw 
# outputs of 'y', and then average across the batch.

cross_entropy = tf.reduce_mean( 
    tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
    
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 
sess = tf.InteractiveSession()

tf.global_variables_initializer().run()

# Train 
for _ in range(1000): 
  batch_xs, batch_ys = mnist.train.next_batch(100) 
  sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) 
# Test trained model

correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) 
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年11月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档