1.tensorboard可视化数据流图:
Mnist softmax模型数据流图
image.png
展开softmax_layer:
image.png
实现代码:
batch_size = 200
n_batch = mnist.train.num_examples // batch_size
with tf.name_scope('intput'):
x =tf.placeholder(tf.float32,([None,784]),name='input-x')
y =tf.placeholder(tf.float32,([None,10]),name='input-y')
with tf.name_scope('softmax_layer'):
with tf.name_scope('weight'):
weight = tf.Variable(tf.truncated_normal([784,10],stddev=0.2))
with tf.name_scope('bias'):
bias = tf.Variable(tf.zeros([1,10])+0.1)
with tf.name_scope('Wx_plus_b'):
predict= tf.matmul(x,weight)+bias
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=predict,labels=y))
with tf.name_scope('train'):
train_step = tf.train.AdagradOptimizer(1e-2).minimize(loss)
with tf.name_scope('correct'):
correct = tf.equal(tf.argmax(y,1),tf.argmax(predict,1))
with tf.name_scope('accuracy'):
accuracy = tf.reduce_mean(tf.cast(correct,tf.float32))
with tf.Session(config=config) as sess:
sess.run(tf.global_variables_initializer())
writer = tf.summary.FileWriter('/logs/graph',sess.graph)
for epoch in range(2):
for i in range(n_batch):
x_data,y_data = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:x_data,y:y_data})
acc = sess.run(accuracy, feed_dict={x: mnist.train.images, y: mnist.train.labels})
print('train-acc' + str(acc))
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print('test-acc'+str(acc))
网友评论