gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 09:29:58 25 4
gpt4 key购买 nike

我已经阅读了 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]

最佳答案

问题似乎是 X_train_count你正在接受 LSTM 输入形状总是很棘手。
如果您的 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 Vector)来处理表示或 Glove作为嵌入层的权重,这在性能方面和结果方面会更好。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com