非法参数错误:logits和label必须大小相同

时间:2017-05-01 12:27:56

标签: tensorflow

使用softmax计算成本函数时出错。它说我的logits和标签的形状不匹配

InvalidArgumentError (see above for traceback): logits and labels must be same size: logits_size=[1000,2] labels_size=[1,1000]
 [[Node: SoftmaxCrossEntropyWithLogits = SoftmaxCrossEntropyWithLogits[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Reshape, Reshape_1)]]

即使我进行整形或转置,形状也不匹配,因为logits是[1000,2]大小,标签是[1000,1]。我该如何解决这个问题?

n_nodes_hl1 = 250
n_nodes_hl2 = 250
n_classes = 2
batch_size = 1000

with open("xdf.pickle", 'rb') as f:
  features = pickle.load(f)
with open("ydf.pickle", 'rb') as f:
  labels = pickle.load(f)


def neural_network_model(data, feature_count):
  hidden_layer_1 = {'weights': tf.Variable(tf.random_normal([feature_count, n_nodes_hl1])),
                  'biases': tf.Variable(tf.random_normal([n_nodes_hl1]))}
  hidden_layer_2 = {'weights': tf.Variable(tf.random_normal([n_nodes_hl1, n_nodes_hl2])),
              'biases': tf.Variable(tf.random_normal([n_nodes_hl2]))}
  output_layer = {'weights': tf.Variable(tf.random_normal([n_nodes_hl2, n_classes])),
                'biases': tf.Variable(tf.random_normal([n_classes])), }

  l1 = tf.add(tf.matmul(data, hidden_layer_1['weights']), hidden_layer_1['biases'])
  l1 = tf.nn.relu(l1)
  l2 = tf.add(tf.matmul(l1, hidden_layer_2['weights']), hidden_layer_2['biases'])
  l2 = tf.nn.relu(l2)

  output = tf.matmul(l2, output_layer['weights']) +  output_layer['biases']
  return output


def train_neural_network(x, y, features, labels):
  X_train, X_test, y_train, y_test = train_test_split(features, labels, test_size=0.2)

  prediction = neural_network_model(x, len(features.columns))
  cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
  optimizer = tf.train.AdamOptimizer().minimize(cost)
  hm_epochs = 1

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

        for i in range(int(len(X_train) / batch_size)):
            epoch_x = X_train[i*batch_size: min((i + 1)*batch_size, len(X_train))]
            epoch_y = y_train[i*batch_size: min((i + 1)*batch_size, len(y_train))]
            i, c = sess.run([optimizer, cost], feed_dict = {x:epoch_x, y:epoch_y})
            epoch_loss += c

        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('Accuracy: ', accuracy.eval({x: X_test, y: y_test}))


 x = tf.placeholder('float', [None, len(features.columns)])
 y = tf.placeholder('float')
 train_neural_network(x, y, features, labels)

1 个答案:

答案 0 :(得分:1)

由于我不知道你有什么数据,我只能猜测。您的网络有n_classes个输出神经元(在您的情况下为2),但我认为您的标签是二进制(0或1)。您将要么必须将输出神经元的数量减少到1(这应该可以工作,因为您只有两个类),或者将您的标签转换为单热标签([1,0]表示标签0和[0,1]标签1)。

您也可以尝试使用tf.nn.sparse_softmax_cross_entropy_with_logits(),也许它会起作用,这样您就不必更改网络的其余部分......