Tensorflow中的神经网络给出0精度

时间:2018-06-22 20:49:06

标签: python tensorflow neural-network

输入没有任何NaN值,但精度始终为0。

    n_nodes_hl1 = 3000
    n_nodes_hl2 = 1500
    n_nodes_hl3 = 1000

    n_classes   = 3
    batch_size  = 5
    hm_epochs   = 5 

def train_neural_network(x):
    prediction=neural_network_model(x)
    cost=tf.nn.softmax_cross_entropy_with_logits_v2(logits = prediction, labels = y)
    optimizer=tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)
    saver = tf.train.Saver()
    with tf.Session() as sess:

        sess.run(tf.global_variables_initializer())
        for epoch in range(hm_epochs):
            epoch_loss = 0
            i = 0
            #while i < len(train_x):
            t = len(train_x)
            f = t%batch_size
            while i < (t-f):
                start = i
                end = i+batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y})
                epoch_loss += c
                #epoch_loss = epoch_loss + c
                i+=batch_size
                #i = i + batch_size
            print('Epoch =', epoch+1, '/',hm_epochs,'loss:',epoch_loss)

        save_path = saver.save(sess, "sesionestensorflow/model1802.ckpt")
        print("Model saved in path: %s" % save_path)
        correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print('Accuracy:',accuracy.eval({x:test_x, y:test_y}))
我理解这可能是损失函数,如果是这种情况,则神经网络需要按值进行限幅。

0 个答案:

没有答案