ValueError:检查输入时出错:预期embedding_1_input有2个维度,但是有形状的数组(11802,25,1)

时间:2017-09-22 13:26:47

标签: python keras

print('Loading the dataset')
# Using keras to load the dataset with the top_words
top_words = 20000
(X_train, y_train), (X_test, y_test) = load_data_and_labels()
#(X_train, y_train), (X_test, y_test) = imdb.load_data(num_words=top_words)


print('Padding the data to a uniform length')
# Pad the sequence to the same length
max_review_length = 25
X_train = sequence.pad_sequences(X_train, maxlen=max_review_length)
X_test = sequence.pad_sequences(X_test, maxlen=max_review_length)
X_train.reshape((11802,25))
X_test.reshape((2950,25))

print('Creating the word embedding vector')
# Using embedding from Keras
embedding_vecor_length = 300
model = Sequential()
model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length))

print('Creating the layers')
# Convolutional model (3x conv, flatten, 2x dense)
model.add(Convolution1D(64, 6, padding='same'))
model.add(Convolution1D(32, 4, padding='same'))
model.add(Convolution1D(16, 2, padding='same'))
model.add(Flatten())
model.add(Dropout(0.1))
model.add(Dense(100,activation='sigmoid'))
model.add(Dropout(0.1))
model.add(Dense(1,activation='sigmoid'))

print('Activating the logging features')
# Log to tensorboard
tensorBoardCallback = TensorBoard(log_dir='./logs', write_graph=True)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

print('Actual training is about to start')
model.fit(X_train, y_train, epochs=3, callbacks=[tensorBoardCallback], batch_size=64)

print('Evaluating the test results...')
# Evaluation on the test set
scores = model.evaluate(X_test, y_test, verbose=0)
print("Accuracy: %.2f%%" % (scores[1]*100))

尝试运行时遇到错误:

Actual training is about to start
Traceback (most recent call last):
  File ".\keras_test.py", line 113, in <module>
    model.fit(X_train, y_train, epochs=3, callbacks=[tensorBoardCallback], batch_size=64)
  File "C:\Program Files\Python36\lib\site-packages\keras\models.py", line 867, in fit
    initial_epoch=initial_epoch)
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1522, in fit
    batch_size=batch_size)
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 1378, in _standardize_user_data
    exception_prefix='input')
  File "C:\Program Files\Python36\lib\site-packages\keras\engine\training.py", line 132, in _standardize_input_data
    str(array.shape))
ValueError: Error when checking input: expected embedding_1_input to have 2 dimensions, but got array with shape (11802, 25, 1)

有人指出,我必须对我的数据使用重塑功能。我做到了但我想我要么在正确的地方使用它,要么没有使用它。 11802是X_train中的句子数,25是max_review_length。我不知道1代表什么。

0 个答案:

没有答案