import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
def Full_Connected():
mnist = input_data.read_data_sets("mnist_data/", one_hot = True)
with tf.variable_scope("data"):
x=tf.placeholder(tf.float32,[None,784])
y_true=tf.placeholder(tf.int32,[None,10])
with tf.variable_scope("fc_model"):
weight=tf.Variable(tf.random_normal([784,10],mean=0.0,stddev=1.0,name="w"))
bias=tf.Variable(tf.constant(0.0),[10])
y_predict=tf.matmul(x,weight)+bias
with tf.variable_scope("soft_cross"):
loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_true,logits=y_predict))
with tf.variable_scope("opmizer"):
train_op=tf.train.GradientDescentOptimizer(0.1).minimize(loss)
with tf.variable_scope("acc"):
equal_list=tf.equal(tf.argmax(y_true,1),tf.argmax(y_predict,1))
accuracy=tf.reduce_mean(tf.cast(equal_list,tf.float32))
init_op=tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
for i in range(1000):
mnist_x,mnist_y=mnist.train.next_batch(50)
sess.run(train_op,feed_dict={x:mnist_x,y_true:mnist_y})
if i %50==0:
print("训练第%d步,准确率为:%f"%(i,sess.run(accuracy,feed_dict={x:mnist_x,y_true:mnist_y})))
return None
if __name__=="__main__":
Full_Connected()