1 Star 0 Fork 1

diycp2015 / imageProcessing

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
mnist_.py 1.77 KB
一键复制 编辑 原始数据 按行查看 历史
北部湾的落日 提交于 2018-05-09 09:58 . Initial commit
import tensorflow as tf
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist = input_data.read_data_sets("./tensorflow/MNIST_data", one_hot=True)
'''
x不是一个特定的值,而是一个占位符placeholder,我们在TensorFlow运行计算时输入这个值。
我们希望能够输入任意数量的MNIST图像,每一张图展平成784维的向量。我们用2维的浮点数张量来表示这些图,
这个张量的形状是[None,784 ]。(这里的None表示此张量的第一个维度可以是任何长度的。)
'''
x = tf.placeholder(tf.float32, [None, 784])
'''
用全为零的张量来初始化W和b
'''
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
'''
训练模型
用tf.matmul(​​X,W)表示x乘以W,对应之前等式里面的
'''
y = tf.nn.softmax(tf.matmul(x,W) + b)
#评估指标
y_ = tf.placeholder("float", [None,10])
#交叉熵计算
cross_entropy = -tf.reduce_sum(y_*tf.log(y))
'''
用梯度下降算法(gradient descent algorithm)以0.01的学习速率最小化交叉熵
'''
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# Add an op to initialize the variables.
init = tf.initialize_all_variables()
#saver model
model_saver = tf.train.Saver()
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, "float"))
print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
# create dir for model saver
model_path = "./tensorflow/tmp/model.ckpt"
save_path =model_saver.save(sess, model_path)
print("Model saved in file: ", save_path)
1
https://gitee.com/diycp2015/imageProcessing.git
git@gitee.com:diycp2015/imageProcessing.git
diycp2015
imageProcessing
imageProcessing
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891