python - Keras model.predict 导致 ValueError
问题描述
我有一个我训练的 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 以匹配火车数据?谢谢你的帮助!
解决方案
问题在于 LSTM 输入形状,LSTM 期望输入形状为 的 3D 张量[batch, timesteps, feature]
。
我可以重现这个问题
import tensorflow as tf
input = tf.keras.layers.Input(500,5)
lstm = tf.keras.layers.LSTM(500, return_sequences=False)
output = lstm(input)
print(output.shape)
输出
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-0a52ed439ffd> in <module>()
2 input = tf.keras.layers.Input(500,5)
3 lstm = tf.keras.layers.LSTM(500, return_sequences=False)
----> 4 output = lstm(input)
5 print(output.shape)
6 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py in assert_input_compatibility(input_spec, inputs, layer_name)
216 'expected ndim=' + str(spec.ndim) + ', found ndim=' +
217 str(ndim) + '. Full shape received: ' +
--> 218 str(tuple(shape)))
219 if spec.max_ndim is not None:
220 ndim = x.shape.rank
ValueError: Input 0 of layer lstm_13 is incompatible with the layer: expected ndim=3, found ndim=2. Full shape received: (5, 500)
工作示例代码
import tensorflow as tf
input = tf.keras.layers.Input(500,5)
inputs = tf.expand_dims(input, axis=0)
print(inputs)
lstm = tf.keras.layers.LSTM(4)
output = lstm(inputs)
print(output.shape)
输出
KerasTensor(type_spec=TensorSpec(shape=(1, 5, 500), dtype=tf.float32, name=None), name='tf.expand_dims_2/ExpandDims:0', description="created by layer 'tf.expand_dims_2'")
(1, 4)
推荐阅读
- java - 设置后更改 InputStream 字符集
- drupal - Drupal 8 不断挂起并且无法安装插件
- java - Hybris 在单独的浏览器选项卡中打开外部链接
- dns - Postfix chroot dns 解决问题(Ubuntu 18.04.4 LTS,Postfix 3.3.0)
- c# - WebApi ActionName 删除路由不起作用
- c++ - 使用 VS2015 在 Windows 10 上构建 Fortran .lib x64 位并将其链接到 C++
- java - 使用 Fresco 库 android/java 将图像从手机加载到我的应用程序
- angular - 如何找到多个标记?
- angular - 完整的日历自定义按钮未定义
- django - django + ajax 发布表单