保存Keras模型错误:AttributeError:'numpy.dtype'对象没有属性'item'

时间:2019-01-04 17:13:44

标签: tensorflow machine-learning keras

我试图将我的Keras模型保存在pycharm中,但出现错误,这是我创建模型的方式:

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

当我运行这段代码model.save('model.h5')时,出现以下错误:

  

回溯(最近通话最近):文件   “ C:/.../ ENV / newDataset / combined3.py”,第209行,在       model.save('blah.h5')文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ site-packages \ keras \ engine \ network.py”,   第1085行,保存中       save_model(自我,文件路径,覆盖,include_optimizer)文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ site-packages \ keras \ engine \ saving.py”,   第117行,在save_model中       },默认= get_json_type).encode('utf8')文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ json__init __。py”,   237行,转储       ** kw).encode(obj)文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ json \ encoder.py”,   第198行,编码       块= self.iterencode(o,_one_shot = True)文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ json \ encoder.py”,   第256行,以iterencode       返回_iterencode(o,0)文件“ C:\ ProgramData \ Anaconda2 \ envs \ Building_Deep_Learning_Keras \ lib \ site-packages \ keras \ engine \ saving.py”,   第84行,在get_json_type中       返回obj.item()AttributeError:“ numpy.dtype”对象没有属性“ item”

如果我运行以下代码,我没有问题:

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 个答案:

答案 0 :(得分:0)

我相信您会遇到这个问题,因为在创建功能模型时Keras如何处理dtype参数。 Keras期望dtype只是一个简单的字符串,而不是numpy.dtype对象,因此,当您将一个numpy对象传递给该参数时,将很难保存模型。

要进行调整,我会使用字符串之一来描述数据输入类型,如https://keras.io/backend/所建议。

我遇到了类似的问题,当我将dtype参数更改为Keras期望的值(字符串)时,我能够保存模型而没有任何其他问题。

为解决您的问题,我建议将dtype=X_train.dtype参数更改为dtype=X_train.dtype.name,因为这将产生dtype的字符串形式,可由Keras处理。 / p>