tf01

作者: Jakai | 来源:发表于2017-08-07 18:45 被阅读0次
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

      本文链接:https://www.haomeiwen.com/subject/faialxtx.html