Tensorflow:词袋文本分类:无法提供形状值

Tensorflow: bag of words text classification: Cannot feed value of shape

我在这里截取了这段代码:https://sourcedexter.com/tensorflow-text-classification-python/ 尝试预测给定问题是否属于两个类别之一。

但是,我收到以下错误:

Cannot feed value of shape (1, 1666) for Tensor 'TargetsData/Y:0', which has shape '(?, 2)'

相关代码如下:

# train_x contains the Bag of words and train_y contains the label/ category
train_x = list(training[:,0])
train_y = list(training[:,1])

#reset underlying graph data
tf.reset_default_graph()
#Build neural network
net = tflearn.input_data(shape=[None,len(train_x[0])])
#layer?
net = tflearn.fully_connected(net,8)
#layer?
net = tflearn.fully_connected(net,8)
#output layer
net = tflearn.fully_connected(net, len(train_y[0]),activation='softmax')
net = tflearn.regression(net)


#define model and set up tensorboard
model = tflearn.DNN(net, tensorboard_dir = 'tflearn_logs')
#start training (grad descent algo)
model.fit(train_x, train_x, n_epoch = 1000, batch_size=1, show_metric = True)
model.save('model.tflearn')

我该如何解决?

  • 这是常见的形状不匹配错误
  • 错误不言自明
  • 您的目标张量的形状为 [None, 2]
  • 您正在为目标张量提供 (1, 1666)
  • 数组
  • 你的model.fit()应该是:

    model.fit(train_x, train_y, n_epoch=1000, batch_size=8, show_metric=True)

  • 查看作为目标的第二个参数 train_y 但您将第二个参数指定为 train_x
  • 这意味着你说你的输入是你的标签,这是不正确的
  • 可能这就是它向您抛出错误的原因