1、导入所需包及数据
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('datasets', one_hot=True)
2、原始数据处理
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
3、生成参数函数(W_h、W_o)
def init_weights(shape):
return tf.Variable(tf.random_normal(shape, stddev=0.01))
4、创建模型
def model(X, w_h, w_o):
h = tf.nn.sigmoid(tf.matmul(X, w_h)) # this is a basic mlp, think 2 stacked logistic regressions
return tf.matmul(h, w_o) # note that we dont take the softmax at the end because our cost fn does that for us
5、创建输入数据的特征向量
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
6、初始化参数
w_h = init_weights([784, 625]) # create symbolic variables
w_o = init_weights([625, 10])
7、正向传播,计算输出
py_x = model(x, w_h, w_o)
8、计算损失函数
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=y)) # compute costs
9、反向优化函数
learning_rate = 0.5
train_op = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost) # construct an optimizer
10、得出模型的预测值
predict_op = tf.argmax(py_x, 1)
11、定义常量
training_epochs = 100 #训练的轮数
display_step = 10 #用来比较、输出结果
12、使用神经网络进行训练
with tf.Session() as sess:
# you need to initialize all variables
tf.global_variables_initializer().run()
costs = []
for i in range(training_epochs):
avg_cost = 0.
for start, end in zip(range(0, len(trX), 128), range(128, len(trX)+1, 128)):
sess.run(train_op, feed_dict={x: trX[start:end], y: trY[start:end]})
feeds = {x: trX[start:end], y: trY[start:end]}
avg_cost += sess.run(cost, feed_dict=feeds) / 128
if i % display_step == 0:
feed_train = {x: trX[1:128], y: trY[1:128]}
feed_test = {x: teX, y:teY}
train_acc = np.mean(np.argmax(trY[1:128], axis=1) ==
sess.run(predict_op, feed_dict={x: trX[1:128]}))
test_acc = np.mean(np.argmax(teY, axis=1) ==
sess.run(predict_op, feed_dict={x: teX}))
print(i, "cost:",avg_cost,"train:", train_acc,"test:" ,test_acc)
costs.append(avg_cost)
# plot the loss
plt.plot(costs)
plt.ylabel('cost')
plt.xlabel('iterations ')
plt.title("Learning rate =" + str(learning_rate))
plt.show()
输出:
0 cost: 2.917380750994198 train: 0.889763779527559 test: 0.8929
10 cost: 0.3393870070867706 train: 0.9448818897637795 test: 0.9635
20 cost: 0.17407534556696191 train: 0.952755905511811 test: 0.9727
30 cost: 0.10498515417566523 train: 0.9763779527559056 test: 0.9775
40 cost: 0.06881195909772941 train: 0.984251968503937 test: 0.9793
50 cost: 0.0477382161134301 train: 0.9921259842519685 test: 0.98
60 cost: 0.0347941225327304 train: 1.0 test: 0.9804
70 cost: 0.026499980357584718 train: 1.0 test: 0.9802
80 cost: 0.020956944943463895 train: 1.0 test: 0.9802
90 cost: 0.017077267546937946 train: 1.0 test: 0.9801