ValueError:无法解释优化程序标识符:<tensorflow.python.keras.optimizers.SGD对象,位于0x0000013887021208>

时间:2019-10-07 15:06:46

标签: tf.keras

我尝试运行此代码,但出现此错误,请以前任何人都遇到相同的错误:

sgd = optimizers.SGD(lr = 0.01, decay = 1e-6, momentum = 0.9, nesterov = True)

编译模型

model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
fit_history = model.fit_generator(
        train_generator,
        steps_per_epoch=STEPS_PER_EPOCH_TRAINING,
        epochs = NUM_EPOCHS,
        validation_data=validation_generator,
        validation_steps=STEPS_PER_EPOCH_VALIDATION,
        callbacks=[cb_checkpointer, cb_early_stopper]
)
model.load_weights("../working/best.hdf5")

现在我遇到此错误:

File "", line 1, in runfile('C:/Users/ResNet50VF72.py', wdir='C:/Users/RESNET')
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile execfile(filename, namespace)
File "C:\ProgramData\Anaconda3\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/RESNET/ResNet50VF72.py", line 110, in model.compile(optimizer = sgd, loss = OBJECTIVE_FUNCTION, metrics = LOSS_METRICS)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py", line 96, in compile self.optimizer = optimizers.get(optimizer)
File "C:\ProgramData\Anaconda3\lib\site-packages\keras\optimizers.py", line 793, in get str(identifier))
ValueError: Could not interpret optimizer identifier : <tensorflow.python.keras.optimizers.SGD object at 0x0000013887021208>

1 个答案:

答案 0 :(得分:1)

我在另一个优化器上遇到了同样的问题:

ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizers.Adam object at 0x7f3fc4575ef0>

这是因为我使用keras而不是tensorflow.keras创建了模型,所以解决方案从以下位置切换:

from keras.models import Sequential

from tensorflow.keras.models import Sequential

或者也可以只使用keras而不能使用tensorflow.keras(我在混合旧代码和新代码),似乎是两者的混合导致了问题(这并不奇怪)。