一、构建计算图
- 准备训练数据
- 定义前向计算过程 Inference
- 定义loss(loss,accuracy等scalar用tensorboard展示)
- 定义训练方法
- 变量初始化
- 保存计算图
二、创建会话
- summary对象处理
- 喂入数据,得到观测的loss,accuracy等
- 用测试数据测试模型
import tensorflow as tf
import numpy as np
import os
from tensorflow.examples.tutorials.mnist import input_data
os.environ['TF_CPP_MIN_LOG_LEVEl']='3'
#
tf.reset_default_graph()
mnist = input_data.read_data_sets('D:\MyData\zengxf\.keras\datasets\MNIST_data',one_hot=True)
xq,yq = mnist.train.next_batch(2)# (2, 784),(2,10)
# Input
h1 = 100
h2 = 10
with tf.name_scope("Input"):
X = tf.placeholder("float",[None,784],name='X')
Y_true = tf.placeholder("float",[None,10],name='Y_true')
with tf.name_scope("Inference"):
with tf.name_scope("hidden1"):
W1 = tf.Variable(tf.random_normal([784, h1])*0.1, name='W1')
# W1 = tf.Variable(tf.zeros([784, h1]), name='W1')
b1 = tf.Variable(tf.zeros([h1]), name='b1')
y_1 = tf.nn.sigmoid(tf.matmul(X, W1)+b1)# (None,h1)
with tf.name_scope("hidden2"):
W2 = tf.Variable(tf.random_normal([h1, h2])*0.1, name='W2')
# W2 = tf.Variable(tf.zeros([h1, h2]), name='W2')
b2 = tf.Variable(tf.zeros([h2]), name='b2')
y_2 = tf.nn.sigmoid(tf.matmul(y_1, W2)+b2)# (h1,h2)
with tf.name_scope("Output"):
W3 = tf.Variable(tf.truncated_normal([h2, 10])*0.1, name='W3')
# W3 = tf.Variable(tf.zeros([h2, 10]), name='W3')
b3 = tf.Variable(tf.zeros([10]), name='b3')
y = tf.nn.softmax(tf.matmul(y_2, W3)+ b3)# (None,10)
with tf.name_scope("Loss"):
loss = tf.reduce_mean(-tf.reduce_sum(tf.multiply(Y_true,tf.log(y))))
loss_scalar = tf.summary.scalar('loss',loss)
accuracy = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(y,1),tf.argmax(Y_true,1)),tf.float32))
accuracy_scalar = tf.summary.scalar('accuracy', accuracy)
# loss = tf.reduce_mean(-tf.reduce_sum(Y_true * tf.log(y)))
# l = tf.multiply(Y_true,tf.log(y))
with tf.name_scope("Trian"):
# optimizer = tf.train.AdamOptimizer(learning_rate=0.05)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_op = optimizer.minimize(loss)
init = tf.global_variables_initializer()
merge_summary_op = tf.summary.merge_all()
# tf.merge_all_summaries()
writer = tf.summary.FileWriter('logs', tf.get_default_graph())
sess = tf.Session()
sess.run(init)
#
for step in range(5000):
# train_summary = sess.run(merge_summary_op,feed_dict = {...})#调用sess.run运行图,生成一步的训练过程数据
train_x,train_y = mnist.train.next_batch(500)
_,summary_op,train_loss,acc= sess.run([train_op,merge_summary_op,loss,accuracy],feed_dict={X:train_x,Y_true:train_y})
if step%100==99:
print('loss=',train_loss)
# print(sess.run(y,feed_dict={X:train_x}))
# summary_op = sess.run(merge_summary_op)
writer.add_summary(summary_op,step)
#测试集上预测
print(sess.run(accuracy, feed_dict={X: mnist.test.images, Y_true: mnist.test.labels})) # 0.9185
writer.close()
# #正确的预测结果
# correct_prediction = tf.equal(tf.argmax(Y_true, 1), tf.argmax(y, 1))
# # 计算预测准确率,它们都是Tensor
# accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# # 在Session中运行Tensor可以得到Tensor的值
# # 这里是获取最终模型的正确率