0
votes

I have tried to save my Keras model in pycharm where I got the error, this is how I created the model:

main_input = Input(shape=(X_train.shape[1],), dtype=X_train.dtype, 
name='main_input')
xx = Embedding(output_dim=512, input_dim=3000, input_length=len(X)) 
(main_input)
xx= SpatialDropout1D(0.4)(xx)
lstm_out = LSTM(64)(xx)


#lstm_out = Dense(3,activation='softmax')(lstm_out)
from keras.layers import Input, Embedding, LSTM, Dense
from keras.models import Model

auxiliary_input = Input(shape=(Z_train.shape[1],), name='aux_input')
auxB= Input(shape=(hasB_train.shape[1],), name='aux_B')
auxM = Input(shape=(hasM_train.shape[1],), name='aux_M')
auxBM_input = keras.layers.concatenate([ auxB, auxM])


auxiliary_output = Dense(3, activation='softmax', name='aux_output')        (lstm_out)
auxBM_output = Dense(3, activation='softmax', name='auxBM_output')        (auxBM_input)


x = keras.layers.concatenate([lstm_out, auxiliary_input, auxBM_input])
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(64, activation='relu')(x)
main_output = Dense(3, activation='sigmoid', name='main_output')(x)



model = Model(inputs=[main_input, auxiliary_input, auxB, auxM], outputs=    [main_output, auxiliary_output, auxBM_output])
model.compile(optimizer='rmsprop', loss='categorical_crossentropy'     ,metrics = ['accuracy'], loss_weights=[4, 1, 10])

model.summary()

when I run the this code model.save('model.h5'), I receive the below error:

Traceback (most recent call last): File "C:/.../ENV/newDataset/combined3.py", line 209, in model.save('blah.h5') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\network.py", line 1085, in save save_model(self, filepath, overwrite, include_optimizer) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py", line 117, in save_model }, default=get_json_type).encode('utf8') File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json__init__.py", line 237, in dumps **kw).encode(obj) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py", line 198, in encode chunks = self.iterencode(o, _one_shot=True) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\json\encoder.py", line 256, in iterencode return _iterencode(o, 0) File "C:\ProgramData\Anaconda2\envs\Building_Deep_Learning_Keras\lib\site-packages\keras\engine\saving.py", line 84, in get_json_type return obj.item() AttributeError: 'numpy.dtype' object has no attribute 'item'

I have no problem, if I run the below code:

model = Sequential() 
model.add(Embedding(max_fatures, embed_dim,input_length = X.shape[1])) 
model.add(SpatialDropout1D(0.4)) 
model.add(LSTM(lstm_out, dropout=0.2, recurrent_dropout=0.2))  
model.add(Dense(3,activation='softmax')) 
model.compile(loss = 'categorical_crossentropy', optimizer='adam',metrics = ['accuracy']) 
X_train, X_test, Y_train, Y_test = train_test_split(X,Y,train_size=0.8, random_state = 42) 
model.fit(X_train, Y_train, epochs = 1, batch_size=32,shuffle=True)    
model.save('test.h5')
1

1 Answers

0
votes

I believe you are having this issue because of how Keras handles the dtype argument when you are creating a Functional Model. Keras expects the dtype to be just a simple string and not a numpy.dtype object, and therefore, it will have difficulty saving the model when you pass a numpy object into this argument.

To adjust, I would use one of the strings to describe the data input type, as suggested at https://keras.io/backend/.

I had a similar issue, and when I changed the dtype argument to what Keras was expecting (a string), I was able to save the model without any additional problem.

To fix your issue, I would suggest, changing the dtype=X_train.dtype argument to dtype=X_train.dtype.name, as this would produce the string form of the dtype, which can be handled by Keras.