【问题标题】:Tensorflow: bag of words text classification: Cannot feed value of shapeTensorflow:词袋文本分类:无法输入形状的值
【发布时间】:2018-01-11 18:44:52
【问题描述】:

我在此处添加了此代码:https://sourcedexter.com/tensorflow-text-classification-python/,以尝试预测给定问题是否属于两个类别之一。

但是,我收到以下错误:

无法为形状为“(?, 2)”的张量“TargetsData/Y:0”提供形状 (1, 1666) 的值

相关代码如下:

# 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')

我该如何解决?

【问题讨论】:

    标签: python tensorflow text-classification


    【解决方案1】:
    • 这是常见的形状不匹配错误
    • 错误是不言自明的
    • 您的目标张量的形状为 [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
    • 这意味着你说你的输入是你的标签,这是不正确的
    • 可能这就是它向您抛出错误的原因

    【讨论】:

    • 天哪,露易丝,是的,已经解决了。谢谢你,很抱歉懒得检查我的代码
    猜你喜欢
    • 1970-01-01
    • 2020-09-09
    • 2018-02-09
    • 2019-02-28
    • 1970-01-01
    • 2016-08-28
    • 2011-02-20
    • 2020-05-25
    • 1970-01-01
    相关资源
    最近更新 更多