目录
逻辑回归
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/',one_hot=true)
x=tf.placeholder(tf.float32,[None,784])
y=tf.placeholder(tf.float32,[None,10])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
pred=tf.nn.softmax(tf.matmul(x,w)+b)
sess=tf.InteractiveSession()
cost=tf.reduce_mean(-tf.reduce_sum(y*tf.log(pred)))
optimizer=tf.train.GradientDescentOptimizer(0.01).minimize(cost)
corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accr=tf.reduce_mean(tf.cast(corr,tf.float32))
init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init)
epoch_n=100
batch_size=64
batch_n=int(mnist.train.num_examples/batch_size)
for epoch in range(epoch_n):
avg_cost=0
for _ in range(batch_n):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
feeds={x:batch_xs,y:batch_ys}
sess.run(optimizer,feed_dict=feeds)
avg_cost+=sess.run(cost,feed_dict=feeds)/batch_size
print(avg_cost)
one_hot=true 独热表示:一位有效编码
读取到的mnist lables: [1,0,0,0,0,0,0,0,0,0] [0,1,0,0,0,0,0,0,0,0] [0,0,1,0,0,0,0,0,0,0] ........
x=tf.placeholder(tf.float32,[None,784]) 未赋值,先占位 None 可传任意个数
pred=tf.nn.softmax(tf.matmul(x,w)+b)
一个简单的神经网络
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data/',one_hot=True)
n_input=784
n_hidden_1=256
n_hidden_2=128
n_classes=10
x=tf.placeholder(tf.float32,[None,n_input])
y=tf.placeholder(tf.float32,[None,n_classes])
stddev=0.1
weights={
'w1':tf.Variable(tf.random_normal([n_input,n_hidden_1],stddev=stddev)),
'w2':tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2],stddev=stddev)),
'out':tf.Variable(tf.random_normal([n_hidden_2,n_classes],stddev=stddev))
}
biases={
'b1':tf.Variable(tf.random_normal([n_hidden_1])),
'b2':tf.Variable(tf.random_normal([n_hidden_2])),
'out':tf.Variable(tf.random_normal([n_classes]))
}
print("-"*10+"NetWork Reday"+"-"*10)
#前向
def multiplayer_perception(x,weights,biases):
layer_1=tf.matmul(x,weights['w1']) #输入*权重
tf.add(layer_1,biases['b1']) #加偏置项
layer_1=tf.sigmoid(layer_1) #激活
layer_2=tf.sigmoid(tf.add(tf.matmul(layer_1,weights['w2']),biases['b2']))
out=tf.matmul(layer_2,weights['out'])+biases['out']
return out
pred=multiplayer_perception(x,weights,biases)
cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optimizer=tf.train.GradientDescentOptimizer(0.001).minimize(cost)
corr=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accr=tf.reduce_mean(tf.cast(corr,tf.float32))
init=tf.global_variables_initializer()
print("-"*10+"Function Ready"+"-"*10)
sess=tf.Session()
sess.run(init)
epoch_n=100
batch_size=64
batch_n=int(mnist.train.num_examples/batch_size)
for epoch in range(epoch_n):
avg_cost=0
for i in range(batch_n):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
feeds={x:batch_xs,y:batch_ys}
sess.run(optimizer,feed_dict=feeds)
avg_cost+=sess.run(cost,feed_dict=feeds)/batch_size
print(avg_cost)
模型的保存和读取
保存
saver=tf.train.Saver(max_to_keep=3) #max_to_keep保存模型的个数
for epoch in range(epoch_n):
avg_cost=0
for i in range(batch_n):
batch_xs,batch_ys=mnist.train.next_batch(batch_size)
feeds={x:batch_xs,y:batch_ys}
sess.run(optimizer,feed_dict=feeds)
avg_cost+=sess.run(cost,feed_dict=feeds)/batch_size
print(avg_cost)
saver.save(sess,'ckpt/mnist.ckpt',global_step=epoch+1)
提取
saver=tf.train.Saver(max_to_keep=3) #######max_to_keep保存模型的个数
is_train=0
if is_train:
for epoch in range(epoch_n):
avg_cost = 0
for i in range(batch_n):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
feeds = {x: batch_xs, y: batch_ys}
sess.run(optimizer, feed_dict=feeds)
avg_cost += sess.run(cost, feed_dict=feeds) / batch_size
print(avg_cost)
saver.save(sess, 'ckpt/mnist.ckpt', global_step=epoch + 1)
else:
model_file = tf.train.latest_checkpoint('ckpt/')
saver.restore(sess, model_file)
val_cost, val_accr = sess.run([cost, accr], feed_dict={x: mnist.test.images, y: mnist.test.labels})
print('val_loss:%f, val_acc:%f' % (val_cost, val_accr))
sess.close()