import tensorflow as tf
import numpy as np
import pandas as pd
import tensorflow.examples.tutorials.mnist.input_data as input_data
mnist=input_data.read_data_sets('MNIST_data/',one_hot=True)
'''
x=tf.placeholder(tf.float32,[None,784])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y=tf.nn.softmax(tf.matmul(x,w)+b)
y_=tf.placeholder("float",[None,10])
cross_entropy=-tf.reduce_sum(y_*tf.log(y)) #用交叉熵来作为损失函数
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
# for i in range(1000):
# print i
# batch_xs,batch_ys=mnist.train.next_batch(100) #随机抓取100个数据作为随机梯度下降数据集
# sess.run(train_step,{x:batch_xs,y_:batch_ys})
# print sess.run(b)
# correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(y_,1))
# accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
# print sess.run(accuracy,{x:mnist.test.images,y_:mnist.test.labels})
'''
'''
这个卷积神经网络的结构为:
第一层卷积+max pooling
第二层卷积+max pooling
一个全连接层+dropout
输出层
'''
x=tf.placeholder(tf.float32,[None,784],name='x_input')
w=tf.Variable(tf.zeros([784,10]),name='w_input')
b=tf.Variable(tf.zeros([10]),name='b_input')
y=tf.nn.softmax(tf.matmul(x,w)+b)
y_=tf.placeholder("float",[None,10],name='y_-input')
cross_entropy=-tf.reduce_sum(y_*tf.log(y))
train_step=tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
def variable_summaries(var):
"""Attach a lot of summaries to a Tensor (for TensorBoard visualization)."""
with tf.name_scope('summaries'):
mean = tf.reduce_mean(var)
tf.summary.scalar('mean', mean)
with tf.name_scope('stddev'):
stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))
tf.summary.scalar('stddev', stddev)
tf.summary.scalar('max', tf.reduce_max(var))
tf.summary.scalar('min', tf.reduce_min(var))
tf.summary.histogram('histogram', var)
def weight_variable(shape):
initial=tf.truncated_normal(shape,stddev=0.1)
return tf.Variable(initial)
def bias_variable(shape):
initial=tf.constant(0.1,shape=shape)
return tf.Variable(initial)
def conv2d(x,w):
return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
with tf.name_scope('w_conv1'): 用name_scope()方法创建变量name,调用前面的weight_variable函数。
w_conv1=weight_variable([5,5,1,32])
variable_summaries(w_conv1)
with tf.name_scope('b_conv1'):
b_conv1=bias_variable([32])
variable_summaries(b_conv1)
with tf.name_scope('input_reshape'):
x_image=tf.reshape(x,[-1,28,28,1])
tf.summary.image('input_image', x_image, 10)
h_conv1=tf.nn.relu(conv2d(x_image,w_conv1)+b_conv1)
tf.summary.histogram('activations', h_conv1)
h_pool1=max_pool_2x2(h_conv1)
tf.summary.histogram('h_pool1',h_pool1)
w_conv2=weight_variable([5,5,32,64])
tf.summary.histogram('w_conv2', w_conv2)
b_conv2=bias_variable([64])
tf.summary.histogram('b_conv2', b_conv2)
with tf.name_scope('h_conv2'):
h_conv2=tf.nn.relu(conv2d(h_pool1,w_conv2)+b_conv2)
variable_summaries(h_conv2)
h_pool2=max_pool_2x2(h_conv2)
tf.summary.histogram('h_pool2',h_pool2)
w_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_variable([1024])
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,w_fc1)+b_fc1)
tf.summary.histogram('h_fc1',h_fc1)
with tf.name_scope('dropout'):
keep_prob=tf.placeholder('float')
tf.summary.scalar('dropout_keep_probability', keep_prob)
h_fc1_drop=tf.nn.dropout(h_fc1,keep_prob)
w_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,w_fc2)+b_fc2)
cross_entropy=-tf.reduce_sum(y_*tf.log(y_conv))
tf.summary.scalar('loss', cross_entropy)
train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction=tf.equal(tf.arg_max(y_conv,1),tf.arg_max(y_,1))
accuracy=tf.reduce_mean(tf.cast(correct_prediction,'float'))
tf.summary.scalar('accuracy', accuracy)
merged = tf.summary.merge_all()
train_writer = tf.summary.FileWriter('log/train', sess.graph)
test_writer = tf.summary.FileWriter('log/test')
sess.run(tf.global_variables_initializer())
import time
start=time.time()
for i in range(100):
batch=mnist.train.next_batch(50)
if i%10==0:
print "step:",i,
print ",accuracy:",sess.run(accuracy,{x:batch[0],y_:batch[1],keep_prob:1.0})
sess.run(train_step,{x:batch[0],y_:batch[1],keep_prob:0.2})
result=sess.run(merged,{x:batch[0],y_:batch[1],keep_prob:1.0})
train_writer.add_summary(result,i)
train_writer.close()
print "Test accuracy:",sess.run(accuracy,{x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0})
print "Time:",time.time()-start
'''
#以下代码用于保存模型。
saver=tf.train.Saver()
sess.run(init)
save_path=saver.save(sess,"mytrain/test.ckpt") #后缀meta文件保存整个tensorflow graph,.data文件保存训练的variables
sess.close()
print "save to path",save_path
sess=tf.Session() #会话前面已关闭,这里需要重新开启
saver=tf.train.import_meta_graph('mytrain/test.ckpt.meta') #导入前面创建的网络
saver.restore(sess,tf.train.latest_checkpoint('./mytrain/')) #加载模型
print sess.run(accuracy,{x:mnist.test.images,y_:mnist.test.labels,keep_prob:1.0}) #测试模型
sess.close()
'''
最后训练完成之后,输入命令:tensorboard --logdir=log/
即可按照提示地址打开tensorboard图表。
MAC运行方式:
python /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorboard/main.py --logdir=log/