0
$\begingroup$

I am using the following code:

from tensorflow.keras.regularizers import l2
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Add, Conv2D, MaxPooling2D, Dropout, Flatten, Dense, BatchNormalization, Activation
from tensorflow.keras import activations

CNN_model = Sequential()
# The First Block
CNN_model.add(Conv2D(128, kernel_size=2,kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same', input_shape=(700, 460, 3)))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))


# The Second Block
CNN_model.add(Conv2D(128, kernel_size=2, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same'))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))

# The Third Block
CNN_model.add(Conv2D(128, kernel_size=2, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same'))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))

# The fourth Block
CNN_model.add(Conv2D(128, kernel_size=2, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same'))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))

# The fifth Block
CNN_model.add(Conv2D(128, kernel_size=2, kernel_initializer='he_uniform', kernel_regularizer=l2(0.0005), padding='same'))
CNN_model.add(Activation(activations.relu))
CNN_model.add(BatchNormalization())
CNN_model.add(MaxPooling2D(2, 2))
rom tensorflow.python.keras.engine.training import Model 
from tensorflow.keras import backend as K, regularizers
from tensorflow.keras import losses
CNN_model.add(Flatten()) 
# Layer 1
CNN_model.add(Dense(800,activation='relu',kernel_regularizer=l2(0.0005))) 
CNN_model.add(Dropout(0.5))
# Layer 2
#CNN_model.add(Dense(25, activation='relu',kernel_regularizer=l2(0.0005))) 
#CNN_model.add(Dropout(0.5))

# Layer 5
CNN_model.add(Dense(8, activation='softmax')) 
from tensorflow.keras.optimizers import SGD
opt=SGD(learning_rate=0.1, momentum=0.2, nesterov=True)
CNN_model.compile(SGD, loss = 'categorical_crossentropy', metrics = ['acc'])

However, I get the following error:

ValueError                                Traceback (most recent call last)
<ipython-input-9-a5e98777f528> in <module>
      1 from tensorflow.keras.optimizers import SGD
      2 opt=SGD(learning_rate=0.1, momentum=0.2, nesterov=True)
----> 3 CNN_model.compile(SGD, loss = 'categorical_crossentropy', metrics = ['acc'])

~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in compile(self, optimizer, loss, metrics, loss_weights, weighted_metrics, run_eagerly, steps_per_execution, **kwargs)
    566       self._run_eagerly = run_eagerly
    567 
--> 568       self.optimizer = self._get_optimizer(optimizer)
    569       self.compiled_loss = compile_utils.LossesContainer(
    570           loss, loss_weights, output_names=self.output_names)

~\anaconda3\lib\site-packages\tensorflow\python\keras\engine\training.py in _get_optimizer(self, optimizer)
    604       return opt
    605 
--> 606     return nest.map_structure(_get_single_optimizer, optimizer)
    607 
    608   @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\tensorflow\python\keras\engine\training.py in _get_single_optimizer(opt)
    595 
    596     def _get_single_optimizer(opt):
--> 597       opt = optimizers.get(opt)
    598       if (loss_scale is not None and
    599           not isinstance(opt, lso.LossScaleOptimizer)):

~\anaconda3\lib\site-packages\tensorflow\python\keras\optimizers.py in get(identifier)
    129     return deserialize(config)
    130   else:
--> 131     raise ValueError(
    132         'Could not interpret optimizer identifier: {}'.format(identifier))

ValueError: Could not interpret optimizer identifier: <class 'tensorflow.python.keras.optimizer_v2.gradient_descent.SGD'>

I am not mixing keras with tensorflow.keras, so why am I getting this error?

$\endgroup$
0
$\begingroup$

Change this line from

CNN_model.compile(SGD, loss = 'categorical_crossentropy', metrics = ['acc'])

to

CNN_model.compile(opt, loss = 'categorical_crossentropy', metrics = ['acc'])
$\endgroup$
0
$\begingroup$

The compile function expects an optimizer class instance or name of optimizer as string. Example:

  1. model.compile(optimizer = SGD(),..)

  2. model.compile(optimizer = SGD(learning_rate=0.1, momentum=0.2, nesterov=True),..)

  3. model.compile(optimizer = 'sgd',..)

$\endgroup$
1
  • $\begingroup$ Thank you so much. $\endgroup$
    – AAA
    Aug 16 at 17:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.