Siladittya
Siladittya

Reputation: 1205

AttributeError : 'RMSProp' has no attribute 'name'

I have declared a RMSProp optimizer instance

optimizer = tf.keras.optimizers.RMSProp(learning_rate = 0.001)

When I run this code

optimizer.get_config()

I am getting this output

{'name': 'RMSprop',
 'learning_rate': 0.001,
 'decay': 0.0,
 'rho': 0.9,
 'momentum': 0.0,
 'epsilon': 1e-07,
 'centered': False}

But when I am running this code

getattr(optimizer,'name')

I am getting this error

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-70-a9eb9a5d971b> in <module>
----> 1 getattr(optimizer,'name')

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py in __getattribute__(self, name)
    676       if name in self._hyper:
    677         return self._get_hyper(name)
--> 678       raise e
    679 
    680   def __setattr__(self, name, value):

~\AppData\Roaming\Python\Python38\site-packages\tensorflow\python\keras\optimizer_v2\optimizer_v2.py in __getattribute__(self, name)
    666     """Overridden to support hyperparameter access."""
    667     try:
--> 668       return super(OptimizerV2, self).__getattribute__(name)
    669     except AttributeError as e:
    670       # Needed to avoid infinite recursion with __setattr__.

AttributeError: 'RMSprop' object has no attribute 'name'

I don't understand the reason for this. Can anybody explain what is the wrong in this?

Upvotes: 0

Views: 2843

Answers (1)

David
David

Reputation: 8298

It indeed doesn't have name attribute. The result of the optimizer.get_config() is not the attribute of the optimizer object but the configuration of the current optimizer, as stated in tensorflow.org/api_docs/python/tf/keras/optimizers/Optimizer#get_config

You can list the available attribute using: dir(optimizer) to validate it.

The list of available attiburte of the RMSProp optimizer are:

>>> optimizer = tf.keras.optimizers.RMSprop(learning_rate=0.1)
>>> dir(optimizer)
['_HAS_AGGREGATE_GRAD', '__abstractmethods__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_abc_impl', '_add_variable_with_custom_getter', '_aggregate_gradients', '_assert_valid_dtypes', '_call_if_callable', '_checkpoint_dependencies', '_clip_gradients', '_compute_gradients', '_create_all_weights', '_create_hypers', '_create_or_restore_slot_variable', '_create_slots', '_decayed_lr', '_deferred_dependencies', '_deferred_slot_restorations', '_dense_apply_args', '_distributed_apply', '_distribution_strategy', '_distribution_strategy_scope', '_fallback_apply_state', '_gather_saveables_for_checkpoint', '_get_hyper', '_handle_deferred_dependencies', '_hyper', '_hypers_created', '_init_set_name', '_initial_decay', '_iterations', '_keras_api_names', '_keras_api_names_v1', '_list_extra_dependencies_for_serialization', '_list_functions_for_serialization', '_lookup_dependency', '_map_resources', '_maybe_initialize_trackable', '_momentum', '_name', '_name_based_attribute_restore', '_name_based_restores', '_no_dependency', '_object_identifier', '_preload_simple_restoration', '_prepare', '_prepare_local', '_resource_apply_dense', '_resource_apply_sparse', '_resource_apply_sparse_duplicate_indices', '_resource_scatter_add', '_resource_scatter_update', '_restore_from_checkpoint_position', '_restore_slot_variable', '_serialize_hyperparameter', '_set_hyper', '_setattr_tracking', '_single_restoration_from_checkpoint_position', '_slot_names', '_slots', '_sparse_apply_args', '_track_trackable', '_tracking_metadata', '_unconditional_checkpoint_dependencies', '_unconditional_dependency_names', '_update_uid', '_use_locking', '_valid_dtypes', '_weights', 'add_slot', 'add_weight', 'apply_gradients', 'centered', 'clipnorm', 'clipvalue', 'epsilon', 'from_config', 'get_config', 'get_gradients', 'get_slot', 'get_slot_names', 'get_updates', 'get_weights', 'iterations', 'minimize', 'set_weights', 'variables', 'weights']

Upvotes: 3

Related Questions