我试图替换这个
from keras.optimizers import SGD
和
from tensorflow.keras.optimizers import SGD
但这仍然不起作用
这是我的代码
from tensorflow.keras.optimizers import SGD
from keras.initializers import RandomUniform
from keras.callbacks import TensorBoard
from tensorflow import keras
import tensorflow as tf
init = RandomUniform(minval=0, maxval=1)
model = Sequential()
model.add(Dense(5, input_dim=2, activation='tanh', kernel_initializer=init))
model.add(Dense(5, activation='tanh', kernel_initializer=init))
model.add(Dense(5, activation='tanh', kernel_initializer=init))
model.add(Dense(5, activation='tanh', kernel_initializer=init))
model.add(Dense(5, activation='tanh', kernel_initializer=init))
model.add(Dense(1, activation='sigmoid', kernel_initializer=init))
opt = SGD(learning_rate = 0.01, momentum=0.9)
model.compile(loss='binary_crossentropy', optimizer= opt, metrics=['accuracy'])
tb = TensorBoard(histogram_freq=1, write_grads=True)
model.fit(trainX, trainy, validation_data=(testX, testy), epochs=500, verbose=0, callbacks=[tb])
这是错误:
ValueError Traceback (most recent call last)
<ipython-input-26-a0282a5bd896> in <module>
28 # compile model
29 opt = SGD(learning_rate = 0.01, momentum=0.9)
---> 30 model.compile(loss='binary_crossentropy', optimizer= opt, metrics=['accuracy'])
31 # prepare callback
32 tb = TensorBoard(histogram_freq=1, write_grads=True)
~\Anaconda3\lib\site-packages\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, weighted_metrics, run_eagerly, steps_per_execution, **kwargs)
546 self._run_eagerly = run_eagerly
547
--> 548 self.optimizer = self._get_optimizer(optimizer)
549 self.compiled_loss = compile_utils.LossesContainer(
550 loss, loss_weights, output_names=self.output_names)
~\Anaconda3\lib\site-packages\keras\engine\training.py in _get_optimizer(self, optimizer)
584 return opt
585
--> 586 return tf.nest.map_structure(_get_single_optimizer, optimizer)
587
588 @trackable.no_automatic_dependency_tracking
~\Anaconda3\lib\site-packages\tensorflow\python\util\nest.py in map_structure(func, *structure, **kwargs)
865
866 return pack_sequence_as(
--> 867 structure[0], [func(*x) for x in entries],
868 expand_composites=expand_composites)
869
~\Anaconda3\lib\site-packages\tensorflow\python\util\nest.py in <listcomp>(.0)
865
866 return pack_sequence_as(
--> 867 structure[0], [func(*x) for x in entries],
868 expand_composites=expand_composites)
869
~\Anaconda3\lib\site-packages\keras\engine\training.py in _get_single_optimizer(opt)
575
576 def _get_single_optimizer(opt):
--> 577 opt = optimizers.get(opt)
578 if (loss_scale is not None and
579 not isinstance(opt, lso.LossScaleOptimizer)):
~\Anaconda3\lib\site-packages\keras\optimizers.py in get(identifier)
131 else:
132 raise ValueError(
--> 133 'Could not interpret optimizer identifier: {}'.format(identifier))
ValueError: Could not interpret optimizer identifier: <tensorflow.python.keras.optimizer_v2.gradient_descent.SGD object at 0x000001BDF251CB48>