训练
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
print("packs loaded")
mnist = input_data.read_data_sets('./data/',one_hot=True)
#如果该位置下没有数据,第一次会自动下载
input_num = 784
hidden1 = 256
hidden2 = 128
output_num = 10
x = tf.placeholder(tf.float32, [None, input_num], name = 'input-x')
y = tf.placeholder(tf.float32, [None, output_num], name = 'input-y')
W = {
'w1':tf.Variable(tf.random_normal([input_num, hidden1],stddev=0.1)),
'w2':tf.Variable(tf.random_normal([hidden1, hidden2], stddev=0.1)),
'out':tf.Variable(tf.random_normal([hidden2, output_num]))
}
b = {
'b1':tf.Variable(tf.random_normal([hidden1])),
'b2':tf.Variable(tf.random_normal([hidden2])),
'b_out':tf.Variable(tf.random_normal([output_num]))
}
print "network init finish"
#获得x,y的名字,在加载模型测试时要用到
print(x.name)
print(y.name)
”“”
以下是变量名称,在notebook中多次运行会产生不一样的变量名,可能是input-x:1,input-y:1.要明确知道你的模型中保存的变量名称。
network init finish
input-x:0
input-y:0
“”“
def net(weight,b):
layer1 = tf.nn.sigmoid(tf.add(tf.matmul(x,weight['w1']), b['b1']))
layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1,weight['w2']),b['b2']))
return tf.add(tf.matmul(layer2,weight['out']) , b['b_out'])
#经过前向传播得到的预测值,是一个10维tf.float32向量
pred = net(W,b)
#计算交叉熵损失函数,y是标签,pred是预测值
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=pred,labels=y))
#梯度下降
optm = tf.train.GradientDescentOptimizer(0.05).minimize(cost)
"""
tf.argmax(pred,1),获得pred中最大值的下标,1表示在哪个维度上计算
tf.argmax(y,1)同理,
tf.equal(),对应坐标对比,如果相同,为True,返回的是数组
"""
corr = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
#计算准确率,tf.cast()更改类型,bool-->tf.float32,tf.reduce_mean()计算均值
accr = tf.reduce_mean(tf.cast(corr,tf.float32))
#初始化变量
init = tf.global_variables_initializer()
print(pred.name)
print(accr.name)
“”“
Add_2:0
Mean_1:0
”“”
training_epochs = 50
batch_size = 128
display_step = 5
sess = tf.Session()
sess.run(init)
#保存模型对象初始化
saver = tf.train.Saver()
for epcho in range(training_epochs):
avg_cost = 0
num_batch = int(mnist.train.num_examples/batch_size)
for i in range(num_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(optm,feed_dict = {x:batch_xs,y:batch_ys})
feeds = {x:batch_xs, y:batch_ys}
avg_cost= sess.run(cost,feed_dict = feeds)/num_batch
if epcho % 5 == 0:
feeds_train = {x:batch_xs,y:batch_ys}
feeds_test = {x:mnist.test.images,y:mnist.test.labels}
train_acc = sess.run(accr, feed_dict = feeds_train)
test_acc = sess.run(accr, feed_dict = feeds_test)
print("Epoch %03d/%03d cost: %0.9f train_acc: %0.3f test_acc: %0.3f"
%(epcho,training_epochs,avg_cost,train_acc,test_acc))
#保存模型
saver.save(sess, "./model/model.ckpt")
sess.close()
print "DONE"