0

我试图替换这个

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>
4

1 回答 1

0

我能够在 Tensorflow 2.5 中执行代码,如下所示

import tensorflow as tf
print(tf.__version__)
from tensorflow import keras
from tensorflow.keras.layers import Dense
from tensorflow.keras import Sequential
from tensorflow.keras.optimizers import SGD 
from tensorflow.keras.initializers import RandomUniform
from tensorflow.keras.callbacks import TensorBoard

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'])

model.summary()

输出:

2.5.0
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 5)                 15        
_________________________________________________________________
dense_1 (Dense)              (None, 5)                 30        
_________________________________________________________________
dense_2 (Dense)              (None, 5)                 30        
_________________________________________________________________
dense_3 (Dense)              (None, 5)                 30        
_________________________________________________________________
dense_4 (Dense)              (None, 5)                 30        
_________________________________________________________________
dense_5 (Dense)              (None, 1)                 6         
=================================================================
Total params: 141
Trainable params: 141
Non-trainable params: 0
_________________________________________________________________
于 2021-07-15T07:44:08.047 回答