tensorflow实践——MNIST,2个全连接层,第1层激活函数sigmoid,第2层激活函数softmax,损失函数为交叉熵,学习率0.2,隐藏层节点600,训练集训练200轮的模型,测试集正确率98.06%以上。
直接上代码,代码参考如下文章:
Tensorflow实战(一)——MNIST_mnist tensorflow_陈陈陈Chann的博客-CSDN博客
完整代码如下:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
#加载数据, 会自动下载数据文件mnist.npz, 一般保存在C:/user/你的用户名/.keras/datasets
mint=tf.keras.datasets.mnist
(x_tr,y_tr),(x_te,y_te)=mint.load_data()
#标签数据由类别向量转换为浮点型类别矩阵
y_tr = tf.keras.utils.to_categorical(y_tr,10,dtype='float32')
y_te = tf.keras.utils.to_categorical(y_te,10,dtype='float32')
#占位符
x_f_ph=tf.compat.v1.placeholder(tf.float32)
x_n_ph=tf.compat.v1.placeholder(tf.int32)
# 手写数字图片位图转换为浮点型向量, 并除以255做简单正规化
x_ftr_op = tf.cast(x_tr, tf.float32)
x_vtr_op = tf.reshape(x_f_ph / 255.0, [x_n_ph, -1])
x_fte_op = tf.cast(x_te, tf.float32)
x_vte_op = tf.reshape(x_f_ph / 255.0, [x_n_ph, -1])
with tf.compat.v1.Session() as sess:
tf.compat.v1.global_variables_initializer().run()
x_ftr = sess.run(x_ftr_op)
x_vtr = sess.run(x_vtr_op, feed_dict={x_f_ph:x_ftr, x_n_ph:x_ftr.shape[0]})
x_fte = sess.run(x_fte_op)
x_vte = sess.run(x_vte_op, feed_dict={x_f_ph:x_fte, x_n_ph:x_fte.shape[0]})
# print(x_ftr[0:1,1:10, 2:8])
print(x_fte.shape)
# print(x_vtr[0:1,0:784])
print(x_vte.shape)
#显示一个位图, 显示图片时程序执行会暂停, 关闭图片后继续
import matplotlib.pyplot as plt
plt.imshow(x_fte[0],cmap="gray")
plt.show()
print(x_fte[0:1,1:10])
print(y_te[0])
# x是特征值,28*28=784
x = tf.compat.v1.placeholder(tf.float32, [None, 784])
#隐藏层, 是一个全连接层
w = tf.Variable(tf.random.uniform([784, 600],-1e-3,1e-3))
b = tf.Variable(tf.zeros([600]))
net1 = tf.matmul(x, w) + b
#激活函数sigmoid
output1=1.0/(1+tf.math.exp(-net1))
#输出层, 是一个全连接层
w2 = tf.Variable(tf.random.uniform([600, 10],-1e-3,1e-3))
b2 = tf.Variable(tf.zeros([10]))
output = tf.matmul(output1, w2) + b2
y = tf.nn.softmax(output)
# y_是图片对应的真实值
y_ = tf.compat.v1.placeholder(tf.float32, [None, 10])
cross_entropy = tf.compat.v1.reduce_mean(-tf.compat.v1.reduce_sum(y_ * tf.math.log(y + 1e-10), reduction_indices=[1]))
train_step = tf.compat.v1.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)
dataset_size=x_vtr.shape[0]
batch_size = 100
print(dataset_size)
with tf.compat.v1.Session() as sess:
tf.compat.v1.global_variables_initializer().run()
print(x_vtr[0:1,1:10])
# 训练数据
for i in range(72000):
start=(i*batch_size)%dataset_size
end=min(start+batch_size,dataset_size)
xs_tr = x_vtr[start:end]
ys_tr = y_tr[start:end]
if i % 2000 == 0:
# if i < 5:
print("iter idx: %d" %(i), end=' ')
print(start, end=' ')
print(end,end=' ')
print( xs_tr.shape,end=' ')
print( ys_tr.shape,end=' ')
#print ("b: ", sess.run(b))
#print ("w: ", sess.run(w))
# print ("out: ", sess.run(output))
print()
sess.run(train_step, feed_dict={x: xs_tr, y_: ys_tr})
if i % 2000 == 0:
# 计算测试集预测正确率
# 取得y的最大概率对应的数组索引来和y_的数组索引对比,如果索引相同,则表示预测正确
correct_prediction = tf.math.equal(tf.math.argmax(y, 1), tf.math.argmax(y_, 1))
accuracy = tf.compat.v1.reduce_mean(tf.cast(correct_prediction, 'float'))
print('正确率:', sess.run(accuracy, feed_dict={x: x_vte, y_: y_te}))
# 最终结果
correct_prediction = tf.math.equal(tf.math.argmax(y, 1), tf.math.argmax(y_, 1))
accuracy = tf.compat.v1.reduce_mean(tf.cast(correct_prediction, 'float'))
print('正确率:', sess.run(accuracy, feed_dict={x: x_vte, y_: y_te}))