运行Keras LSTM模型时,我收到上述错误。以下是该模型的要点:
inp = Input(shape=(170,200))
out = LSTM(25, activation='relu')(inp)
main_out = Dense(4, activation='sigmoid')(out)
model = Model(inputs = [inp], outputs = [main_out])
# optimizer, model.fit etc. etc.
model.fit([img_data, ], [y_train],
epochs=500, batch_size=1, callbacks = callbacks,
verbose=1, validation_split=0.1)
我的输入是250个170个向量的列表,每个向量长度为200.形状似乎正确:
X.shape = (170, 200, 250)
然而,当我运行模型时,我得到了
Traceback (most recent call last):
File "lstm_trials.py", line 62, in <module>
model = Model(inputs = [inp], outputs = [main_out])
File ".../keras/legacy/interfaces.py", line 88, in wrapper
return func(*args, **kwargs)
File ".../keras/engine/topology.py", line 1485, in __init__
inputs_set = set(self.inputs)
TypeError: unhashable type: 'numpy.ndarray'
出了什么问题?
答案 0 :(得分:1)
我认为您的输入数据img_data
有错误的type()
或形状。我没有成功尝试使用以下代码片段重现您的错误,该代码段在Keras 2.0.4上顺利运行。请将其输入数据格式与您的输入数据格式进行比较,以找出确切的错误来源。
import numpy as np
from keras import optimizers, losses
from keras.models import Model
from keras.layers import Input, Dense, LSTM
from keras.utils import to_categorical
# Generate dummy data
n_classes = 4
im_height = 170
im_width = 200
n_training_examples = 250
img_data = np.random.random(size=(n_training_examples, im_height, im_width))
y_train = to_categorical(
y=np.random.randint(n_classes, size=(n_training_examples, 1)),
num_classes=n_classes)
inp = Input(shape=(im_height, im_width))
out = LSTM(units=25, activation='relu')(inp)
main_out = Dense(units=n_classes, activation='softmax')(out)
model = Model(inputs=[inp], outputs=[main_out])
model.compile(optimizer=optimizers.sgd(),
loss=losses.categorical_crossentropy)
model.fit(x=[img_data], y=[y_train],
epochs=5, batch_size=10, verbose=1, validation_split=0.2)