TensorFlow ValueError: Cannot feed value of shape (32, 2) for Tensor 'InputData/X:0', which has shape '(?, 100)'

TensorFlow ValueError: Cannot feed value of shape (32, 2) for Tensor 'InputData/X:0', which has shape '(?, 100)'

我是 TensorFlow 和机器学习的新手。我正在尝试使用 tensorflow 创建情绪分析 NN。

我已经设置了我的架构,我正在尝试训练模型,但我遇到了错误

ValueError: Cannot feed value of shape (32, 2) for Tensor 'InputData/X:0', which has shape '(?, 100)'

我认为错误与我的输入有关"layer net = tflearn.input_data([None, 100])"。 我遵循的教程建议使用此输入形状,批量大小为 None,长度为 100,因为这是序列长度。因此 (None, 100),据我了解,这是输入网络的训练数据所需的维度,对吗?

有人可以解释为什么建议的批量大小输入形状是 None 以及为什么 Tensor Flow 试图为网络提供 put 形状 (32,2)。 2 的序列长度从何而来?

如果我在这个解释中的任何地方的理解是错误的,请随时纠正我,我也在努力学习理论。

提前致谢

In [1]:

import tflearn
from tflearn.data_utils import to_categorical, pad_sequences
from tflearn.datasets import imdb

In [2]:

#Loading IMDB dataset
train, test, _ = imdb.load_data(path='imdb.pkl', n_words=10000,
                                valid_portion=0.1)
trainX, trainY = train
testX, testY = test

In [3]:

#Data sequence padding 
trainX = pad_sequences(trainX, maxlen=100, value=0.)  
testX = pad_sequences(testX, maxlen=100, value=0.)
#converting labels of each review to vectors
trainY = to_categorical(trainY, nb_classes=2)
trainX = to_categorical(testY, nb_classes=2)


In [4]:

#network building 
net = tflearn.input_data([None, 100])
net = tflearn.embedding(net, input_dim=10000, output_dim=128)
net = tflearn.lstm(net, 128, dropout = 0.8)
net = tflearn.fully_connected(net, 2, activation='softmax') 
net = tflearn.regression(net, optimizer = 'adam', learning_rate=0.0001,
                         loss='categorical_crossentropy')


WARNING:tensorflow:From C:\Users\Nason\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tflearn\objectives.py:66: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.
Instructions for updating:
keep_dims is deprecated, use keepdims instead


In [5]:

#Training
model = tflearn.DNN(net, tensorboard_verbose=0)   #train using tensorflow Deep nueral net
model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True,    #fit launches training process for training and validation data, metric displays data as its training.
          batch_size=32)


---------------------------------
Run id: U7NONK
Log directory: /tmp/tflearn_logs/
INFO:tensorflow:Summary name Accuracy/ (raw) is illegal; using Accuracy/__raw_ instead.
---------------------------------
Training samples: 2500
Validation samples: 2500
--

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-7ffd0a8836f9> in <module>()
      2 model = tflearn.DNN(net, tensorboard_verbose=0)   #train using tensorflow Deep nueral net
      3 model.fit(trainX, trainY, validation_set=(testX, testY), show_metric=True,    #fit launches training process for training and validation data, metric displays data as its training.
----> 4           batch_size=32)

~\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tflearn\models\dnn.py in fit(self, X_inputs, Y_targets, n_epoch, validation_set, show_metric, batch_size, shuffle, snapshot_epoch, snapshot_step, excl_trainops, validation_batch_size, run_id, callbacks)
    214                          excl_trainops=excl_trainops,
    215                          run_id=run_id,
--> 216                          callbacks=callbacks)
    217 
    218     def fit_batch(self, X_inputs, Y_targets):

~\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tflearn\helpers\trainer.py in fit(self, feed_dicts, n_epoch, val_feed_dicts, show_metric, snapshot_step, snapshot_epoch, shuffle_all, dprep_dict, daug_dict, excl_trainops, run_id, callbacks)
    337                                                        (bool(self.best_checkpoint_path) | snapshot_epoch),
    338                                                        snapshot_step,
--> 339                                                        show_metric)
    340 
    341                             # Update training state

~\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tflearn\helpers\trainer.py in _train(self, training_step, snapshot_epoch, snapshot_step, show_metric)
    816         tflearn.is_training(True, session=self.session)
    817         _, train_summ_str = self.session.run([self.train, self.summ_op],
--> 818                                              feed_batch)
    819 
    820         # Retrieve loss value from summary string

~\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tensorflow\python\client\session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~\Anaconda33\envs\TensorFlow1.8CPU\lib\site-packages\tensorflow\python\client\session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1109                              'which has shape %r' %
   1110                              (np_val.shape, subfeed_t.name,
-> 1111                               str(subfeed_t.get_shape())))
   1112           if not self.graph.is_feedable(subfeed_t):
   1113             raise ValueError('Tensor %s may not be fed.' % subfeed_t)

ValueError: Cannot feed value of shape (32, 2) for Tensor 'InputData/X:0', which has shape '(?, 100)'

错误来自 trainX = to_categorical(testY, nb_classes=2)。这需要更改为 testY = to_categorical(testY, nb_classes=2)

此外,将批量大小设置为 None 意味着它应该期望批量为任意大小。在您的情况下,您将批量大小设置为 32,因此您还可以将输入形状设置为 [32, 100]

您将 trainX 的类别数保留为 2,但您的模型需要 100

编辑:

我刚刚注意到您在这段代码中将 trainX 设置为 testY

trainX = to_categorical(testY, nb_classes=2)

而应该是:

trainX = to_categorical(trainX, nb_classes=100)

因此您需要将代码更改为:

#Data sequence padding
trainX = pad_sequences(trainX, maxlen=100, value=0.)  
testX = pad_sequences(testX, maxlen=100, value=0.)
#converting labels of each review to vectors
trainY = to_categorical(trainY, nb_classes=2)
#change the number of Classes
trainX = to_categorical(trainX, nb_classes=100) #CHANGE HERE!!

通过此更改,您应该没问题。我刚刚测试过并且有效!

可以使用 [None 设置输入的形状,100] 如果需要,您可以更灵活地更改批量大小!

tflearn.input_data([None, 100])

您期望输入是具有 100 个特征的任意数量实例的张量。

trainX = pad_sequences(trainX, maxlen=100, value=0.)  
testX = pad_sequences(testX, maxlen=100, value=0.)
#converting labels of each review to vectors
trainY = to_categorical(trainY, nb_classes=2)
trainX = to_categorical(testY, nb_classes=2) #HEREEEEEE

这在您的代码中存在问题。您正在将 trainX 重置为具有另一种形状而不是填充的形状。我想你的意思是:

testY = to_categorical(testY, nb_classes=2)

如果还是不行。

我怀疑您遗漏了数据重塑。您确实在使用填充,但在整个 trainX、trainY 等上。尝试分别填充每个 "row"。然后每个实例的长度将如您所期望的那样为“100”。

在此之前,打印张量的形状(如 print(trainX.shape) )以查看您是否真的在预处理数据(我还建议执行两个脚本,一个包含整个加载、预处理、重塑和填充另一个使用 tensorFlow 逻辑)