batch_size = 100
num_batch = int(mnist.train.num_examples / batch_size)
num_epoch = 50
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])
W1 = tf.Variable(tf.random_normal([784, 10]))
b1 = tf.Variable(tf.zeros([10]))
a1 = tf.nn.softmax(tf.matmul(x, W1) + b1)
loss = tf.reduce_mean(-tf.reduce_sum(y * tf.log(a1),reduction_indices=1))
#loss = tf.reduce_mean(tf.square(y - a1))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
init = tf.global_variables_initializer()
correct_prediction = tf.equal(tf.argmax(a1, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# Launch the graph
with tf.Session() as sess:
sess.run(init)
for epoch in range(num_epoch):
for batch in range(num_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step, {x: batch_xs, y: batch_ys})
acc = sess.run(accuracy,{x: mnist.test.images, y: mnist.test.labels})
print("%3d, Accuracy:%5.3f"%(epoch,acc))
tf01
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。