tensorflow nn mnist example with modified layer dimensions

戏子无情 提交于 2019-12-25 04:34:22

问题


I modified this mnist example so that it has two outputs and a middle layer of 10 nodes. It doesn't work, giving me a .50 score all the time. I think it just picks one of the outputs and responds with that no matter what the input is. How could I fix this so that it actually does some learning? The outputs are supposed to represent 0 for 'skin tone' and 1 for 'no skin tone'. I use png input.

def nn_setup(self):
    input_num = 784 * 3 # like mnist but with three channels
    mid_num = 10
    output_num = 2

    x = tf.placeholder(tf.float32, [None, input_num])
    W_1 = tf.Variable(tf.random_normal([input_num, mid_num], stddev=0.04))
    b_1 = tf.Variable(tf.random_normal([mid_num], stddev=0.5))

    y_mid = tf.nn.relu(tf.matmul(x,W_1) + b_1)

    W_2 = tf.Variable(tf.random_normal([mid_num, output_num],stddev=0.4))
    b_2 = tf.Variable(tf.random_normal([output_num],stddev=0.5))

    y_logits = tf.matmul(y_mid, W_2) + b_2
    y = tf.nn.softmax(y_logits)

    y_ = tf.placeholder(tf.float32, [None, output_num])

    cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(y_logits, y_))

    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) 

    init = tf.initialize_all_variables()
    self.sess = tf.Session()
    self.sess.run(init)

    for i in range(1000): 
        batch_xs, batch_ys = self.get_nn_next_train()
        self.sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


    self.nn_test.images, self.nn_test.labels =  self.get_nn_next_test()
    print(self.sess.run(accuracy, feed_dict={x: self.nn_test.images, y_: self.nn_test.labels}))

来源:https://stackoverflow.com/questions/41019116/tensorflow-nn-mnist-example-with-modified-layer-dimensions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!