如何解决此错误,logit和标签必须可广播:logits_size = [100,35947] labels_size = [100,900]

时间:2019-04-07 14:55:47

标签: tensorflow deep-learning

当我运行以下代码时,它显示错误日志,并且标签必须是可广播的:logits_size=[64,35947]labels_size=[16384,1]

def train_neural_network(x):
    prediction = neural_network_model(x)

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))
    optimizer = tf.train.AdamOptimizer().minimize(cost)

    hm_epochs = 10
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        for epoch in range(hm_epochs):
            epoch_loss = 0

            i=0
            while i<48980:
                start = i
                end = i+batch_size
                batch_x = np.array(train_x[start:end])
                batch_y = np.array(train_y[start:end])
                batch_x = np.reshape(batch_x, (-1, 900))
                #batch_x = array(batch_x).reshape(1,900)
                _,c = sess.run([optimizer,cost],feed_dict = {x: batch_x,y: batch_y})
                epoch_loss+=c
                i+=batch_size

            print('Epoch ', epoch, ' completed out of ',   hm_epochs, ' loss ',epoch_loss)


        correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
        accuracy = tf.reduce_mean(tf.cast(correct,'float'))
        print('Accutracy: ',accuracy.eval({x:test_x, y:test_y}))

0 个答案:

没有答案