我正在尝试使用多输入输出模型(使用预先训练的词嵌入作为嵌入层)来构建Keras功能API LSTM层。
下面是我的代码:
#sequential padding text data
max_review_length = 300
text_seq_train = sequence.pad_sequences(text_train, maxlen=max_review_length)
text_seq_test = sequence.pad_sequences(text_test, maxlen=max_review_length)
print(text_seq_train.shape)
print(text_seq_test.shape)
# Loading pre-trained glove embedding file
embeddings_index = dict()
f = open('gdrive/My Drive/glove.6B.100d.txt')
for line in f:
values = line.split()
word = values[0]
coefs = np.asarray(values[1:], dtype='float32')
embeddings_index[word] = coefs
f.close()
print('Loaded %s word vectors.' % len(embeddings_index))
embedding_matrix = zeros((len(text_tokenizer.index_word), 100))
for word, i in text_tokenizer.index_word.items():
embedding_vector = embeddings_index.get(word)
if embedding_vector is not None:
embedding_matrix[i] = embedding_vector
print(embedding_matrix.shape) #weights for embedding layer
输出:
(76473, 300)
(32775, 300)
Loaded 400000 word vectors.
(55297, 100)
LSTM部分
input_layer = Input(shape=(300,))
embed = Embedding(input_dim = len(text_tokenizer.index_word) , output_dim = 100, input_length = len(text_seq_train[0]) ,weights=[embedding_matrix], trainable=False) (input_layer)
lstm = LSTM(100)(embed)
flat = Flatten() (lstm)
收到的错误声明:
ValueError Traceback (most recent call last)
<ipython-input-131-9118c8229a4a> in <module>()
2 embed = Embedding(input_dim = len(text_tokenizer.index_word) , output_dim = 100, input_length = len(text_seq_train[0]) ,weights=[embedding_matrix], trainable=False) (input_layer)
3 lstm = LSTM(100)(embed)
----> 4 flat = Flatten() (lstm)
1 frames
/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
325 self.name + ': expected min_ndim=' +
326 str(spec.min_ndim) + ', found ndim=' +
--> 327 str(K.ndim(x)))
328 # Check dtype.
329 if spec.dtype is not None:
ValueError: Input 0 is incompatible with layer flatten_26: expected min_ndim=3, found ndim=2
我不知道我在想什么,哪里出了问题。 任何帮助将不胜感激。
答案 0 :(得分:0)