代码实现
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import random
tf.set_random_seed(1)
# 读取数据并进行独热编码
mnist = input_data.read_data_sets('MNIST', one_hot=True)
# 占位符
X = tf.placeholder(tf.float32, shape=[None, 784])
Y = tf.placeholder(tf.float32, shape=[None, 10])
# 初始化Wb
W = tf.Variable(tf.random_normal([784, 10]))
b = tf.Variable(tf.random_normal([10]))
# 前向传播
z = tf.matmul(X, W) + b
a = tf.nn.softmax(z)
# 代价
cost = -tf.reduce_mean(tf.reduce_sum(Y * tf.log(a), axis=1))
# 反向传播
dz = a - Y
dw = tf.matmul(tf.transpose(X), dz) / tf.cast(tf.shape(X)[0], tf.float32)
db = tf.reduce_mean(dz, axis=0)
# 更新参数
learning_rate = 0.05
update = [
tf.assign(W, W - learning_rate * dw),
tf.assign(b, b - learning_rate * db)
]
# 准确率
accurary = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(a, axis=1), tf.argmax(Y, axis=1)), dtype=tf.float32), axis=0)
# 大批次
train_times = 15
# 小批量下的选取的样本数
batch_size = 100
# 开启会话
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for times in range(train_times):
# 计算代价
cost_avg = 0
# 算出第二层循环的次数
n_batch = int(mnist.train.num_examples / batch_size)
for i in range(n_batch):
# 获取数据集里面传的是要的个数
train_X, train_Y = mnist.train.next_batch(batch_size)
# 获取代价
c, _ = sess.run([cost, update], feed_dict={X: train_X, Y: train_Y})
cost_avg += c / n_batch
print('次数', times + 1)
print('代价', cost_avg)
print('结束')
print('准确率为:', sess.run(accurary, feed_dict={X: mnist.test.images, Y: mnist.test.labels}))
r = random.randint(0, mnist.test.num_examples - 1)
print('labels', sess.run(tf.argmax(mnist.test.labels[r: r + 1], axis=1)))
print('预测值', sess.run(tf.argmax(a, 1), feed_dict={X: mnist.test.images[r: r + 1]}))
plt.imshow(mnist.test.images[r: r + 1].reshape(28, 28),
cmap='Greys'
)
plt.show()
效果如下
次数 1
代价 4.296778541694989
次数 2
代价 1.6005297816341582
次数 3
代价 1.2341548316587103
次数 4
代价 1.0615348825671462
次数 5
代价 0.9547244831106878
次数 6
代价 0.8794667452573784
次数 7
代价 0.8233596404032287
次数 8
代价 0.778786083026365
次数 9
代价 0.7426509524475442
次数 10
代价 0.712254016832872
次数 11
代价 0.6861341475898565
次数 12
代价 0.6635004966367382
次数 13
代价 0.6434360623088746
次数 14
代价 0.6261556869745252
次数 15
代价 0.6101320389996875
结束
准确率为: 0.871
labels [4]
预测值 [4]
总结
这里可以看出只用一层的神经网络的准确率能达到87%左右,调一下学习率应该能到90%左右,
但两层的神经网络可以轻松的达到90%以上