2层DNN,Mnist的使用,交叉熵(cross_entropy)log + 1e-6, test集算accuracy用tf代码归为网络一部分
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
def add_layer(input, in_size, out_size, name='layer', activation=None):
with tf.variable_scope(name):
Weight = tf.get_variable(name='Weight', shape=(in_size, out_size), dtype=tf.float32, initializer=tf.random_normal_initializer())
bias = tf.get_variable(name='bias', shape=(out_size), dtype=tf.float32, initializer=tf.constant_initializer(0.1))
Wx_b = tf.matmul(input, Weight) + bias
if activation != None:
output = activation(Wx_b)
else:
output = Wx_b
return output
#placehoder
x = tf.placeholder(name='x', shape=(None, 784), dtype=tf.float32)
y_label = tf.placeholder(name='y_label', shape=(None, 10), dtype=tf.float32)
#network structure
hidden_size = 128
y_hidden = add_layer(x, 784, 128, name='hidden', activation=tf.nn.relu)
y = add_layer(y_hidden, 128, 10, name='output', activation=tf.nn.softmax)
#loss and train
# cross_entropy = tf.reduce_mean(tf.reduce_sum(-y_label * tf.log(y), reduction_indices=-1))
# train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_label * tf.log(y + 0.000001),
reduction_indices=[1])) # loss
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
#test structure
correct = tf.equal(tf.arg_max(y, 1), tf.arg_max(y_label, 1))
acuracy = tf.reduce_mean(tf.cast(correct, 'float'))
# acuracy = 1
#run sess
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for steps in range(1000):
batch_x, batch_y = mnist.train.next_batch(100)
_, loss = sess.run([train_step, cross_entropy], feed_dict={x:batch_x, y_label:batch_y})
if steps % 50 == 0:
print(loss)
accuracy = sess.run(acuracy, feed_dict={x:mnist.test.images, y_label:mnist.test.labels})
print(steps, accuracy)