今天在學習tensorflow時,將代碼敲完以後,輸出的結果全部爲 none,
原來是在compute_accuracy()函數中沒有寫 return.
在 compute_accuracy()函數中,將prediction 設置爲了global,不知時什麼原因。
將 這一句註釋掉以後,還是可以正常運行。
prediction 是這個分類神經網絡的輸出,
這個分類神經網絡沒有隱藏層,只有輸入層 和 輸出層。
所用的 反向傳播算法 是結合 批梯度下降法 的,
激勵函數 爲 softmax
損失函數時 交叉熵(cross entropy)
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot = True)
# add one moer layer ad return the output of this layer
def add_layer(inputs, in_size, out_size, n, activation_function = None):
layer_name ='layer%s' % n
with tf.name_scope(layer_name):
with tf.name_scope('Weights'):
Weights = tf.Variable(tf.random_normal([in_size, out_size]), name = 'W')
#writing the histogram summary
tf.summary.histogram(layer_name+'/weights', Weights)
with tf.name_scope('biases'):
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, name = 'b')
tf.summary.histogram(layer_name+'/biases', biases)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b,)
tf.summary.histogram(layer_name+'/Outputs', outputs)
return outputs
def compute_accuracy(v_xs, v_ys):
#global prediction #why???
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
#print(y_pre[1])
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict = {xs: v_xs, ys: v_ys})
return result
#define placeholder for inputs to network
with tf.name_scope('inputs'):
xs = tf.placeholder(tf.float32,[None, 784], name = 'x_input')#28*28
ys = tf.placeholder(tf.float32,[None, 10], name = 'y_input')
#add hidden layer
#l1 = add_layer(xs, 1, 10, n =1,activation_function = tf.nn.softmax)
#add hidden layer
#l2 = add_layer(l1,10, 10, n =1,activation_function = tf.nn.relu)
#output layer
prediction = add_layer(xs, 784, 10, n = 1, activation_function = tf.nn.softmax)
#loss
with tf.name_scope('loss'):
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction), reduction_indices = [1]))
tf.summary.scalar('loss', cross_entropy)
#train
with tf.name_scope('TRAIN'):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize( cross_entropy )
#initialize
init = tf.global_variables_initializer()
sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.train.SummaryWriter('logs/', sess.graph)
sess.run(init)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(train_step, feed_dict = {xs: batch_xs, ys: batch_ys})
if i%50 == 0:
print(compute_accuracy(mnist.test.images, mnist.test.labels))