学习进度笔记
学习进度笔记10
TensorFlow多层感知
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
import os
os.environ["CUDA_VISIBLE_DEVICES"]="0"
mnist=input_data.read_data_sets("/home/yxcx/tf_data",one_hot=True)
#Parameters
learning_rate=0.001
training_epochs=15
batch_size=100
display_step=1
#Network Parameters
n_hidden_1=256
n_hidden_2=256
n_input=784
n_classes=10
# tf Graph input
X=tf.placeholder("float",[None,n_input])
Y=tf.placeholder("float",[None,n_classes])
#Store layers weight & bias
weights={
"h1":tf.Variable(tf.random_normal([n_input,n_hidden_1])),
"h2":tf.Variable(tf.random_normal([n_hidden_1,n_hidden_2])),
"out":tf.Variable(tf.random_normal([n_hidden_2,n_classes]))
}
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]))
}
# Create model
def multiplayer_perceptron(x):
# Hidden fully connected layer with 256 neurons
layer_1=tf.add(tf.matmul(x,weights['h1']),biases['b1'])
#Hidden fully connected layer with 256 neurons
layer_2=tf.add(tf.matmul(layer_1,weights['h2']),biases['b2'])
#Output fully connected layer with a neuron for each class
out_layer=tf.matmul(layer_2,weights['out'])+biases['out']
return out_layer
#Construct model
logits=multiplayer_perceptron(X)
#Define loss and optimizer
loss_op=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
logits=logits,labels=Y))
train_op=tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss_op)
# Initializing the variables
init=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
#Training cycle
for epoch in range(training_epochs):
avg_cost=0
total_batch=int(mnist.train.num_examples/batch_size)
#loop over all batches
for i in range(total_batch):
batch_x,batch_y=mnist.train.next_batch(batch_size)
#Run optimizeation op and cost op to get loss value
_,c=sess.run([train_op,loss_op],feed_dict={X:batch_x,Y:batch_y})
#computer average loss
avg_cost+=c/total_batch
#Display logs per epoch step
if epoch % display_step==0:
print("Epoch:","%04d"%(epoch+1),"cost={:.9f}".format(avg_cost))
print("Optimization Finished!")
#Test model
pred=tf.nn.softmax(logits)
correct_prediction=tf.equal(tf.argmax(pred,1),tf.argmax(Y,1))
# Calculate accuracy
accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))
print("Accuracy:",accuracy.eval({X:mnist.test.images,Y:mnist.test.labels}))