【发布时间】:2019-03-20 04:38:40
【问题描述】:
我是 TensorFlow 和机器学习的新手。我正在尝试使用 tensorflow 创建情绪分析 NN。
我已经建立了我的架构,我正在尝试训练模型,但我遇到了错误
ValueError: 无法为形状为“(?, 100)”的张量“InputData/X:0”提供形状 (32, 2) 的值
我认为错误与我的输入“layer net = tflearn.input_data([None, 100])”有关。 我正在关注的教程建议这个输入形状,批量大小为无,长度为 100,因为这是序列长度。因此(无,100),据我了解,这是输入网络的训练数据需要的维度,对吗?
有人可以解释为什么建议的批量大小输入形状为无,以及为什么张量流试图为网络输入形状 (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)'
【问题讨论】:
标签: python tensorflow sentiment-analysis training-data valueerror