from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
file = "./MNIST"
mnist = input_data.read_data_sets(file, one_hot=True)
# 模型的输入和输出
'''模型的输入x是一个22维的浮点数张量。它的大小为shape=[None, 784],
其中784是一张展平的MNIST图片的维度。None表示其值不固定。输出y_也
是一个22维张量,它的每一行都是一个10维的one-hot向量,用来表示对应的MNIST图片的类别。
'''
x = tf.placeholder(tf.float32, shape=[None, 784])
y_ = tf.placeholder(tf.float32, shape=[None, 10])
# 模型的权重和偏移量
'''
y_和x分别表示上述模型的输入和输出。W是一个784∗10的矩阵,因为输入有784个特征,同时有10个输出值。
b是一个10维的向量,是因为输出有10个分类。'''
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
'''把向量化后的图片x和权重矩阵W相乘,加上偏移量b,然后计算每个分类的softmax概率值'''
y = tf.nn.softmax(tf.matmul(x, W) + b)
'''为训练指定损失函数,损失函数是用来评估模型一次预测的好
tensorflow简单神经网络预测MNIST数据集
最新推荐文章于 2022-11-01 12:37:49 发布