Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras fit model: TypeError: unhashable type: 'numpy.ndarray'

I implement following code. It successfully works in previous version of 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))

When running:

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))

the following error occurs:

TypeError: unhashable type: 'numpy.ndarray' accours.

So I checked train_x_1, train_x_2, train_y_class. Their type is <class 'numpy.ndarray'>. I have searched for a solution, so I tried to change type to tuple but It didn't work.

If numpy.ndarray is unhashable, what type of input does model.fit receive?

The shape of train data is as follows:

train_x_1.shape
(2999, 56, 26)
train_x_2.shape
(2999, 56, 26)
train_y_class.shape
(2999, 1)

A sample of train_x_1 is like below:

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]],
like image 638
Lucas Kim Avatar asked Oct 15 '25 14:10

Lucas Kim


1 Answers

The problem is that you are directly passing the input and output arrays (and not the input and output tensors) to Model class when constructing your model:

model = Model(inputs=[train_x_1,train_x_2], outputs=train_y_class)

Instead, you need to pass the corresponding input and output tensors like this:

model = Model(inputs=[first_input,second_input], outputs=merge_activation2)
like image 78
today Avatar answered Oct 17 '25 03:10

today



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!