import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#每批次放进50个
batch_size = 50
#计算一共有多少批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder 属性的个数是固定的 样本数是变化的
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
keep_prob = tf.placeholder(tf.float32)
L1_Node = 500
L2_Node = 300
L3_Node = 200
lr = tf.Variable(0.01,dtype = tf.float32)
#两个None对应位置是一样的
#创建第一个隐层神经网络
W_1 = tf.Variable(tf.truncated_normal([784,L1_Node],stddev=0.1))
b_1 = tf.Variable(tf.zeros([L1_Node])+0.1)
Wx_plus_b_1 = tf.matmul(x,W_1)+b_1
#虽然与ng讲的是相反的 但是这里维度是相对应乘起来的,所以没啥问题
#行数是样本数 一行里面有784个feature 所以列是784 列 最后维度要对应上
prediction_L1 = tf.nn.tanh(Wx_plus_b_1)
L1_drop = tf.nn.dropout(prediction_L1,keep_prob)
#创建第二隐层
W_2 = tf.Variable(tf.truncated_normal([L1_Node,L2_Node],stddev=0.1))
b_2 = tf.Variable(tf.zeros([L2_Node])+0.1)
Wx_plus_b_2 = tf.matmul(prediction_L1,W_2)+b_2
prediction_L2 = tf.nn.tanh(Wx_plus_b_2)
L2_drop = tf.nn.dropout(prediction_L2,keep_prob)
#创建第三隐层
W_3 = tf.Variable(tf.truncated_normal([L2_Node,L3_Node],stddev=0.1))
b_3 = tf.Variable(tf.zeros([L3_Node])+0.1)
Wx_plus_b_3 = tf.matmul(prediction_L2,W_3)+b_3
prediction_L3 = tf.nn.tanh(Wx_plus_b_3)
L3_drop = tf.nn.dropout(prediction_L3,keep_prob)
#创建输出层
W_4 = tf.Variable(tf.truncated_normal([L3_Node,10])*0.01)
b_4 = tf.Variable(tf.zeros([10])+0.1)
Wx_plus_b_4 = tf.matmul(prediction_L3,W_4)+b_4
prediction = tf.nn.softmax(Wx_plus_b_4)
#二次代价函数
#loss = tf.reduce_mean(tf.square(y-prediction))
#使用交叉熵损失
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels = y,logits= prediction))
#使用Adam优化器
train_step = tf.train.AdamOptimizer(lr).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
#
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#tf.equal(x,y) 看x,y是否一样 一样就true
# argmax的0就是纵向的元素中最大的那个的索引
# 1 就是每行横向的元素中最大的那个元素的索引
# 0轴就是竖向,1就是横向 但是要分清元素还是层级
#arg会返回一个array
#最终返回布尔类型列表
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#tf.cast 是将布尔类型转化为浮点型
#创建会话
with tf.Session() as sess:
sess.run(init)
for epoch in range(51):
sess.run(tf.assign(lr,0.001*(0.95**epoch)))
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 : 1.0})
learning_rate = sess.run(lr)
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)+',train acc'+str(train_acc)+',learning_rate'+str(learning_rate))
模型经过调参,修改层数,节点,优化器后 准确率得到提升,最终达到0.98以上