1

我无法保存使用 KerasRegressor 包装器训练的模型...

model = KerasRegressor(build_fn=base_model, epochs=1, batch_size=10, verbose=1)
model.fit(X,Y)

model_json = model.to_json()
with open("model.json", "w") as json_file:
    json_file.write(model_json)
    model.save_weights("model.h5", overwrite=True)
    logger.info("Saved model to disk")

这是我的导入:

from keras.wrappers.scikit_learn import KerasRegressor
from keras.layers import Dense, Activation
from keras.models import Sequential
from keras.models import model_from_json

# multi gpu
import tensorflow as tf
from keras import backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Lambda
from keras.layers.merge import Concatenate

我得到的错误如下:

Traceback (most recent call last):
  File "train-model.py", line 175, in <module>
  File "train-model.py", line 114, in main
    model.save_weights("model.h5", overwrite=True)
AttributeError: 'KerasRegressor' object has no attribute 'to_json'

我使用返回模型的函数构建模型:

def base_model():
    global N, M

    model = Sequential()
    model.add(Dense(512, input_dim=M, kernel_initializer='normal', activation='relu'))
    model.add(Dense(128, kernel_initializer='normal', activation='relu'))
    model.add(Dense(1, kernel_initializer='normal', activation='linear'))

    model.compile(loss='mean_squared_error', optimizer='rmsprop')

    # model = to_multi_gpu(model)

    return model

有任何想法吗?

4

0 回答 0