【发布时间】:2018-07-24 10:53:26
【问题描述】:
尝试按照https://blog.keras.io/using-pre-trained-word-embeddings-in-a-keras-model.html 上的教程,使用预先存在的词嵌入为少量训练数据训练模型。
我遇到的问题是,当我尝试运行 1D Convnet 时,出现错误:
Input 0 is incompatible with layer flatten_11: expected min_ndim=3, found ndim=2
我的张量的维度是:
数据张量的形状:(91, 1000) 标签张量的形状:(91, 3)
问题在于这部分代码:
sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
embedded_sequences = embedding_layer(sequence_input)
x = Conv1D(128, 5, activation='relu')(embedded_sequences)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = MaxPooling1D(5)(x)
x = Conv1D(128, 5, activation='relu')(x)
x = GlobalMaxPooling1D()(x)
x = Flatten()(x)
x = Dense(3, activation='relu')(x)
preds = Dense(len(labels_index), activation='softmax')(x)
model = Model(sequence_input, preds)
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['acc'])
model.fit(x_train, y_train,
batch_size=128,
epochs=10,
validation_data=(x_val, y_val))
没有展平,它会反馈错误:
Error when checking target: expected dense_25 to have shape (33,) but got array with shape (3,)
我正在尝试找出我需要更改的位置和内容,以确保尺寸正常工作,但是我还没有弄清楚我到底需要更改什么。任何帮助将不胜感激。
【问题讨论】:
标签: python tensorflow keras