python - 如何在 Keras python 中输入 LSTM 模型?

标签 python tensorflow keras neural-network lstm

我读过有关 LSTM 的内容,我知道该算法采用前一个单词的值并在下一个单词参数中考虑它

现在我正在尝试应用我的第一个 LSTM 算法

我有这个代码。

model = Sequential()
model.add(LSTM(units=6, input_shape = (X_train_count.shape[0], X_train_count.shape[1]), return_sequences = True))
model.add(LSTM(units=6, return_sequences=True))
model.add(LSTM(units=6, return_sequences=True))
model.add(LSTM(units=ytrain.shape[1], return_sequences=True, name='output'))
model.compile(loss='cosine_proximity', optimizer='sgd', metrics = ['accuracy'])



model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])
model.summary()

cp=ModelCheckpoint('model_cnn.hdf5',monitor='val_acc',verbose=1,save_best_only=True)

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['acc'])
model.summary()

cp=ModelCheckpoint('model_cnn.hdf5',monitor='val_acc',verbose=1,save_best_only=True)


history = model.fit(X_train_count, ytrain,
                    epochs=20,
                    verbose=False,
                    validation_data=(X_test_count, yval),
                    batch_size=10,
                    callbacks=[cp])

1- 当我的数据集基于 TFIDF 构建时,我看不出 LSTM 如何知道单词序列?

2-我收到错误

ValueError: Input 0 of layer sequential_8 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: [None, 18644]

最佳答案

问题似乎在于您在 LSTM 输入形状中采用的 X_train_count 形状总是很棘手。

如果您的 X_train_count 不是 3D 格式,则使用以下行 reshape 形状。

X_train_count=X_train_count.reshape(X_train_count.shape[0],X_train_count.shape[1],1))

在 LSTM 层中,input_shape 应为 (timesteps, data_dim)

下面是说明这一点的示例。

from sklearn.feature_extraction.text import TfidfVectorizer
import tensorflow as tf
from tensorflow import keras
from sklearn.model_selection import train_test_split

X = ["first example","one more","good morning"]
Y = ["first example","one more","good morning"]

vectorizer = TfidfVectorizer().fit(X)

tfidf_vector_X = vectorizer.transform(X).toarray() 
tfidf_vector_Y = vectorizer.transform(Y).toarray() 
tfidf_vector_X = tfidf_vector_X[:, :, None] 
tfidf_vector_Y = tfidf_vector_Y[:, :, None] 

X_train, X_test, y_train, y_test = train_test_split(tfidf_vector_X, tfidf_vector_Y, test_size = 0.2, random_state = 1)

from tensorflow.keras import Sequential
from tensorflow.keras.layers import LSTM

model = Sequential()
model.add(LSTM(units=6, input_shape = X_train.shape[1:], return_sequences = True))
model.add(LSTM(units=6, return_sequences=True))
model.add(LSTM(units=6, return_sequences=True))
model.add(LSTM(units=1, return_sequences=True, name='output'))
model.compile(loss='cosine_proximity', optimizer='sgd', metrics = ['accuracy'])

模型摘要:

Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
lstm_9 (LSTM)                (None, 6, 6)              192       
_________________________________________________________________
lstm_10 (LSTM)               (None, 6, 6)              312       
_________________________________________________________________
lstm_11 (LSTM)               (None, 6, 6)              312       
_________________________________________________________________
output (LSTM)                (None, 6, 1)              32        
=================================================================
Total params: 848
Trainable params: 848
Non-trainable params: 0
_________________________________________________________________
None  

此处 X_train 的形状为 (2, 6, 1)

为了添加到解决方案中,我建议使用密集向量,而不是通过使用 Tf-Idf 方法表示生成的稀疏向量,并替换为 等预训练模型>Google News VectorGlove 作为嵌入层的权重,这在性能和结果方面会更好。

关于python - 如何在 Keras python 中输入 LSTM 模型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62885470/

相关文章:

python - TensorFlow 2.0 中的基本函数最小化和变量跟踪

tensorflow - 合并张量板的摘要时出错

python - 获取 Keras model.summary() 作为表格

python - Keras:使用 fit_generator 时出现 notImplementedError/RuntimeError

tensorflow - Keras 属性错误 : 'Functional' object has no attribute 'shape'

python - 如何根据给定值计算空间距离矩阵

python - 使用Python标准库显示函数状态

python - 如何以编程方式绘制多个变量

python - OutOfRangeError(请参阅上面的回溯): FIFOQueue '_1_batch/fifo_queue' is closed and has insufficient elements (requested 32, 当前大小 0)

node.js - 用于字符串分类的 tensorflow 与 Elasticsearch