【发布时间】:2021-09-16 17:08:57
【问题描述】:
我有一个我训练的 Keras LSTM 模型,它应该可以按顺序预测下一个:
from tensorflow.keras.layers import LSTM, Dropout, Input, Dense, Flatten
from tensorflow.keras.models import Sequential
import tensorflow.keras.optimizers as o
model = Sequential(
[
Input(shape= (500,5)), #500 arrays like this one -> [0,0,0,0,0]
LSTM(500, return_sequences=False),
Dense(972, activation="softmax"), #the 972 unique words in the vocab
]
)
optimizer = o.Adam(learning_rate=0.01)
model.compile(loss="categorical_crossentropy", optimizer=optimizer)
model.fit(corpuswithids, np.asarray(ydata), batch_size=200, epochs=20)
到目前为止,这是我的预测功能:
def predict(text):
#text = "this is a test"
text = text.split(" ")
ids = []
for i in text:
ids.append(texids[np.where(textfull == i)][0]) #Converts text to its id which is
#in this format [0,0,0,0,0]
ids = np.asarray(ids)
print(ids)
#prints [[ 95. 0. 0. 5. 5.]
#[883. 0. 0. 4. 3.]
#[ 44. 0. 0. 2. 88.]
#[ 36. 0. 0. 3. 255.]]
print(ids.shape)
#prints (4, 5)
model.predict(x = ids)
return ids
这会导致以下错误:
ValueError: Input 0 of layer sequential_13 is incompatible with the layer: expected
ndim=3, found ndim=2. Full shape received: (None, None)
我是否需要更改或填充 id 的长度,使其长度为 500 以匹配火车数据? 感谢您的帮助!
【问题讨论】:
-
通过重塑添加批量维度可能有效:
model.predict(ids.reshape(1,-1)) -
@Kaveh 现在无论出于何种原因,它说:ValueError:层顺序的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2。收到的完整形状:(无,20)
-
好吧,显然它希望形状为 num_samples, 1, 4 所以我摆脱了 [0],现在它可以工作了
标签: python tensorflow keras lstm tf.keras