我正在使用Kfold进行ML,但是我没有成功复制和保存自适应的keras模型,但是,相同的代码对于adam和sgd都工作正常,有关如何复制,保存和重用自适应模型的任何帮助会很棒。
我也在用 模型1 =深度复制(模型) 而不是使用保存 model1.save('my_class.h5') 为了预测使用它的加载 new_model = models.load_model('my_class.h5')
for train, test in kfold.split(X, Y):
# create model
model = Sequential()
print('model',type(model))
model.add(Dense(250, input_dim=train_inputs.shape[1],
activation='relu'))
model.add(Dropout(0.))
model.add(Dense(250, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mean_squared_error', optimizer=opti_Bound,
metrics=['accuracy'])
history = model.fit(X[train], Y[train], \
validation_data=(X[test], Y[test]), \
epochs=2,batch_size=1000, verbose=2)
_, train_acc = model.evaluate(X[train], Y[train], verbose=0)
_, test_acc = model.evaluate(X[test], Y[test], verbose=0)
print('Train: %.3f, Test: %.3f' % (train_acc, test_acc))
if test_acc>=accc_tst and train_acc>=accc_trn:
accc_tst=test_acc
accc_trn=train_acc
model1=deepcopy(model)
plt.plot(history.history['acc'], label='train')
plt.plot(history.history['val_acc'], label='test')
plt.legend()
plt.show()
cvscores.append(test_acc * 100)
print("%.2f%% (+/- %.2f%%)" % (np.mean(cvscores),np.std(cvscores)))
model1.save('my_class.h5')
不久我收到此错误:
ValueError Traceback (most recent
call last)
<ipython-input-4-43690e87e00a> in <module>()
34 accc_tst=test_acc
35 accc_trn=train_acc
---> 36 model1=deepcopy(model)
37
38 # plot history
/usr/lib/python3.6/copy.py in deepcopy(x, memo, _nil)
178 y = x
179 else:
--> 180 y = _reconstruct(x, memo, *rv)
181
182 # If is its own copy, don't memoize.
/usr/lib/python3.6/copy.py in _reconstruct(x, memo, func, args,
state, listiter, dictiter, deepcopy)
280 state = deepcopy(state, memo)
281 if hasattr(y, '__setstate__'):
--> 282 y.__setstate__(state)
283 else:
284 if isinstance(state, tuple) and len(state) ==
2:
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in
__setstate__(self, state)
1264
1265 def __setstate__(self, state):
-> 1266 model = saving.unpickle_model(state)
1267 self.__dict__.update(model.__dict__)
1268
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in
unpickle_model(state)
433 def unpickle_model(state):
434 f = h5dict(state, mode='r')
--> 435 return _deserialize_model(f)
436
437
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py in
_deserialize_model(f, custom_objects, compile)
297 optimizer_config =
training_config['optimizer_config']
298 optimizer =
optimizers.deserialize(optimizer_config,
--> 299
custom_objects=custom_objects)
300
301 # Recover loss functions and metrics.
/usr/local/lib/python3.6/dist-packages/keras/optimizers.py in
deserialize(config, custom_objects)
766
module_objects=all_classes,
767
custom_objects=custom_objects,
--> 768
printable_module_name='optimizer')
769
770
/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py
in deserialize_keras_object(identifier, module_objects,
custom_objects, printable_module_name)
136 if cls is None:
137 raise ValueError('Unknown ' +
printable_module_name +
--> 138 ': ' + class_name)
139 if hasattr(cls, 'from_config'):
140 custom_objects = custom_objects or {}
ValueError: Unknown optimizer: AdaBound
我怀疑,如果我尝试保存和加载该优化程序,也会出现错误。 因此,我实际上正在寻找使用keras复制,保存和重用\重新加载自动绑定的方法。