我实现以下代码。它可以在早期版本的Keras中成功运行:
max_sequence = 56
input_dim = 26
print("Build model..1")
first_input = Input(shape=(max_sequence,input_dim))
first_lstm = LSTM(5, return_sequences=True)(first_input)
first_bn = BatchNormalization()(first_lstm)
first_activation = Activation('tanh')(first_bn)
first_flat = Flatten()(first_activation)
print("Build model..2")
second_input = Input(shape=(max_sequence,input_dim))
second_lstm = LSTM(5, return_sequences=True)(second_input)
second_bn = BatchNormalization()(second_lstm)
second_activation = Activation('tanh')(second_bn)
second_flat = Flatten()(second_activation)
merge=concatenate([first_flat, second_flat])
merge_dense=Dense(3)(merge)
merge_bn = BatchNormalization()(merge_dense)
merge_activation = Activation('tanh')(merge_bn)
merge_dense2=Dense(1)(merge_activation)
merge_activation2 = Activation('tanh')(merge_dense2)
train_x_1 = np.reshape(np.array(train_x_1), [2999, 56, 26])
train_x_2 = np.reshape(np.array(train_x_2), [2999, 56, 26])
model=Model(inputs=[train_x_1,train_x_2], outputs=train_y_class)
optimizer = RMSprop(lr=0.5)
model.compile(optimizer=optimizer, loss='binary_crossentropy', metrics=['accuracy'])
history = model.fit([train_x_1, train_x_2], train_y_class, nb_epoch=300, batch_size=128,
validation_data=([val_x_1, val_x_2], val_y_class))
运行时:
history = model.fit([train_x_1, train_x_2], train_y_class, nb_epoch=300, batch_size=128,
validation_data=([val_x_1, val_x_2], val_y_class))
发生以下错误:
TypeError: unhashable type: 'numpy.ndarray' accours.
因此我选中了train_x_1
,train_x_2
,train_y_class
。它们的类型为<class 'numpy.ndarray'>
。我一直在寻找解决方案,所以我尝试将类型更改为元组,但没有用。
如果numpy.ndarray
无法散列,model.fit
会收到什么类型的输入?
火车数据的形状如下:
train_x_1.shape
(2999, 56, 26)
train_x_2.shape
(2999, 56, 26)
train_y_class.shape
(2999, 1)
train_x_1
的示例如下:
array([[[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
...,
[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00],
[ 1.62601626e-02, 2.26890756e-01, 1.17764920e-02, ...,
0.00000000e+00, 0.00000000e+00, 0.00000000e+00]],
答案 0 :(得分:3)
问题在于,在构建模型时,您直接将输入和输出数组(而不是输入和输出张量)传递给Model
类:
model = Model(inputs=[train_x_1,train_x_2], outputs=train_y_class)
相反,您需要像这样传递相应的输入和输出张量:
model = Model(inputs=[first_input,second_input], outputs=merge_activation2)