import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist=input_data.read_data_sets('MNIST_data',one_hot=True)
batch_size=100
n_batch=mnist.train.num_examples//batch_size
with tf.name_scope('input'):
x=tf.placeholder(tf.float32,[None,784],name='x_input')
y=tf.placeholder(tf.float32,[None,10],name='y_input')
with tf.name_scope('layer'):
with tf.name_scope('weights'):
w=tf.Variable(tf.random_normal([784,10]),name='W')
with tf.name_scope('biases'):
b=tf.Variable(tf.zeros([10]),'b')
with tf.name_scope('Wx_plus_b'):
Wx_plus_b=tf.matmul(x,w)+b
with tf.name_scope('softmax'):
y_pred=tf.nn.tanh(Wx_plus_b)
with tf.name_scope('loss'):
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=y_pred))
with tf.name_scope('accuracy'):
accuracy=tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y,1),tf.argmax(y_pred,1)),tf.float32))
init=tf.global_variables_initializer()
with tf.name_scope('train'):
train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)
with tf.Session() as sess:
sess.run(init)
writer=tf.summary.FileWriter('../logs',sess.graph)
for epoch in range(21):
for batch in range(n_batch):
x_batch,y_batch=mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:x_batch,y:y_batch})
print('Iter'+str(epoch)+', accuracy='+str(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})))
遇到了一个问题:Terminal返回的网址总是打不开
后来经过网络查询,把原本的输入
tensorboard --logdir='yourpath'
改为
tensorboard --logdir='yourpath' --host=127.0.0.1
就可以正常打开了,原本网址localhost名称的地方变为了127.0.0.1