import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import time
"""
mnist数据集有60000张图片,10000张图片作为测试集数据,每张图片都是28*28像素的手写数字图片,对应0-9中的每个数字
"""
mnist_data = input_data.read_data_sets("MNIST_data/", one_hot=True)
#独热编码:用n个维度来表示对n个类别进行编码,并且对每个类别,只有一个维度有效,记作数字1,其余的维度都为0,最后通过softmax函数输出的是每张图片属于10个类别的概率
"""define batch_size and batch_num"""
batch_size = 100 #一次处理100个
batch_num = mnist_data.train.num_examples//batch_size
#占位符,用于存放训练数据和对应的标签
x = tf.placeholder(tf.float32,[None,784]) #train data
y = tf.placeholder(tf.float32,[None,10]) #label
'''define weights and biases'''
weights = {
'hidden_1':tf.Variable(tf.random_normal([784,256])),
'out':tf.Variable(tf.random_normal([256,10]))
}
biases = {
'b1':tf.Variable(tf.random_normal([256])),
'out':tf.Variable(tf.random_normal([10]))
}
'''construct a simple CNN'''
def neural_network(x):
hidden_layer_1 = tf.add(tf.matmul(x,weights['hidden_1']),biases['b1'])
L1 = tf.nn.tanh(hidden_layer_1)
dropout1 = tf.nn.dropout(L1,0.5)
out_layer = tf.matmul(dropout1,weights['out']) + biases['out']
return out_layer
#调用神经网络
result = neural_network(x)
#预测类别
prediction = tf.nn.softmax(result)
#平方差损失函数:使用交叉熵代替平方差损失,模型收敛速度更快,训练的效果也更好。
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#梯度下降法
train_step = tf.train.AdamOptimizer(1e-2).minimize(loss)
#预测类标
correct_pred = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#计算准确度
accuracy = tf.reduce_mean(tf.cast(correct_pred,tf.float32))
#初始化变量
init = tf.global_variables_initializer()
step_num = 100
start = time.time()
with tf.Session() as sess:
sess.run(init)
for step in range(step_num+1):
for batch in range(batch_num):
batch_x,batch_y = mnist_data.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_x,y:batch_y})
acc = sess.run(accuracy,feed_dict={x:mnist_data.test.images,y:mnist_data.test.labels})
print("step"+str(step)+",Training accuracy"+"{:.3f}"+str(acc))
print("Finished!")
end = time.time()
print("运行时间:%s Seconds"%(end - start))
......
step100,Training accuracy{:.3f}0.9149
step100,Training accuracy{:.3f}0.9123
step100,Training accuracy{:.3f}0.9151
step100,Training accuracy{:.3f}0.9148
step100,Training accuracy{:.3f}0.9154
step100,Training accuracy{:.3f}0.9158
step100,Training accuracy{:.3f}0.917
step100,Training accuracy{:.3f}0.9155
step100,Training accuracy{:.3f}0.913
step100,Training accuracy{:.3f}0.9141
step100,Training accuracy{:.3f}0.9125
step100,Training accuracy{:.3f}0.9133
step100,Training accuracy{:.3f}0.9163
Finished!
运行时间:2764.080650806427 Seconds
Process finished with exit code 0