import tensorflow as tf
# 加载数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('/tmp/data',one_hot=True)
Extracting /tmp/data/train-images-idx3-ubyte.gz
Extracting /tmp/data/train-labels-idx1-ubyte.gz
Extracting /tmp/data/t10k-images-idx3-ubyte.gz
Extracting /tmp/data/t10k-labels-idx1-ubyte.gz
一、定义和训练模型
1.参数
batch_size = 100 # 每个batch的大小
n_batch = mnist.train.num_examples//batch_size # 训练集共包含多少个batch
2.定义计算图
定义name_scope和name属性后,对应的名字会在TensorBoard中显示;而且定义合适的name_scope可以使TensorBoard的图更加简洁清晰
# 定义命名空间
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')
keep_prob = tf.placeholder(tf.float32) # dropout时,每个元素被保留的概率
with tf.name_scope('layer1'):
with tf.name_scope('weights'):
W1 = tf.Variable(tf.truncated_normal([784,2000],stddev=0.1),name='W1')
with tf.name_scope('biases'):
b1 = tf.Variable(tf.zeros([2000])+0.1,name='b1')
L1 = tf.nn.tanh(tf.matmul(x,W1)+b1)
L1_drop = tf.nn.dropout(L1,keep_prob)
with tf.name_scope('layer2'):
W2 = tf.Variable(tf.truncated_normal([2000,1000],stddev=0.1),name='W2')
b2 = tf.Variable(tf.zeros([1000])+0.1,name='b2')
L2 = tf.nn.tanh(tf.matmul(L1_drop,W2)+b2)
L2_drop = tf.nn.dropout(L2,keep_prob)
with tf.name_scope('layer3'):
W3 = tf.Variable(tf.truncated_normal([1000,10],stddev=0.1),name='W3')
b3 = tf.Variable(tf.zeros([10])+0.1,name='b3')
# 预测值
prediction = tf.nn.softmax(tf.matmul(L2_drop,W3)+b3)
# loss
# loss = tf.reduce_mean(tf.square(y-prediction)) # MSE
# cross entropy
with tf.name_scope('loss'):
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
# SGD
with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
# 初始化
init = tf.global_variables_initializer()
with tf.name_scope('accuracy'):
# 分类结果
# tf.argmax(y,1):在axis=1,y中最大值的下标
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
# 准确率
# tf.cast:将bool类型的correct_prediction转换为tf.float32类型
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
3.训练
with tf.Session() as sess:
sess.run(init)
# 将sess.graph写到文件夹logs下
writer = tf.summary.FileWriter('logs/',sess.graph)
for epoch in range(2):
for batch in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys,keep_prob:0.5})
test_acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels,keep_prob:1.0})
train_acc = sess.run(accuracy,feed_dict={x:mnist.train.images,y:mnist.train.labels,keep_prob:1.0})
print("Iter "+str(epoch)+",Testing accuracy "+str(test_acc)+",Training accuracy "+str(train_acc))
Iter 0,Testing accuracy 0.9049,Training accuracy 0.89774543
Iter 1,Testing accuracy 0.9204,Training accuracy 0.915
二、使用TensorBoard
在上面训练的过程中我们已经将有关于图信息的文件保存到了目录’logs/’,那么之后只要打开TensorBoard服务读取这个文件。然后再浏览器中就能看到我们的网络结构了。