【问题标题】:Tflearn Input shape errorTflearn 输入形状错误
【发布时间】:2017-08-26 13:22:44
【问题描述】:

错误

"无法为张量 'TargetsData/Y:0' 提供形状 (128, 1) 的值, 形状为'(?,)'"。

代码

我有 4 个班级,词汇由单词 17355 组成。

tf.reset_default_graph()
net = tflearn.input_data(shape=(None,trainX.shape[1]),name='input')
net = tflearn.fully_connected(net, 200, activation='ReLU')
net = tflearn.fully_connected(net, 25, activation='ReLU')
net = tflearn.fully_connected(net, 4, activation='softmax')
net = tflearn.regression(net, optimizer='sgd', 
                         learning_rate=0.1, 
                         to_one_hot = True,n_classes =4,
                         loss='categorical_crossentropy')

model = tflearn.DNN(net)
model.fit(trainX, trainY, validation_set=0.1, show_metric=True, batch_size=128, n_epoch=100)

trainX.shape = 12384,17355 , trainY.shape = 12384,1 , testX.shape = 1376,17355 , testY.shape = 1376,1

【问题讨论】:

    标签: neural-network tflearn


    【解决方案1】:

    导致此错误的原因是什么?

    错误“Cannot feed value of shape ... for Tensor 'TargetsData/Y:0', which has shape ...”主要是由于trainY的形状与占位符形状不同估计器(回归)层。

    为什么?

    在您的情况下,主要问题是 trainY 的形状是 (?, 1),它是一个 2D 张量,但占位符的形状是 (?,),它是一个 1D 张量。所以我们得到了这个错误。

    如何解决?

    将 trainY 重塑为 1D 张量。因为您在回归层中设置了to_one_hot = True,所以占位符形状是一个包含类索引的一维张量。详情可以查看source code:tflearn/tflearn/layers/estimator.py关于回归:

       with tf.name_scope(pscope):
            p_shape = [None] if to_one_hot else input_shape
            placeholder = tf.placeholder(shape=p_shape, dtype=dtype, name="Y") 
    

    因此,我们需要在输入模型之前将 trainY 从 (12384,1) 重塑为 (12384,)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-30
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 2018-06-07
      • 2017-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多