tensorflow nn mnist具有修改的图层尺寸的示例

时间:2016-12-07 13:46:06

标签: python neural-network tensorflow

我修改了这个mnist示例,以便它有两个输出和一个10个节点的中间层。它不起作用,一直给我一个.50的分数。我认为它只选择其中一个输出,无论输入是什么,都会响应。我怎么能解决这个问题,以便它实际上做了一些学习?输出应该代表0代表'肤色',1代表'无肤色'。我用png输入。

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}))

0 个答案:

没有答案