Getting a list in return instead of a dictionary

create a dictionary with empty list
python dictionary get key from value
get values from dictionary python
python get value from dictionary if key exists
python defaultdict
jinja dictionary get
dictionary that defaults to a list
dictionary return default value

I have a dictionary returned by keras_model.get_config(). (variyed by print(type(keras_model.get_config()))). I am getting an error on the code line:

if keras_model.get_config()[0]['config']['data_format'] == 'channels_first':

The error indicates that the dictionary has no 0 key, which is obvious ehough:

Traceback (most recent call last): File "task1a.py", line 1204, in sys.exit(main(sys.argv)) File "task1a.py", line 234, in main overwrite=overwrite File "task1a.py", line 982, in do_testing if keras_model.get_config()[0]['config']['data_format'] == >'channels_first': KeyError: 0

I carried on to access via keras_model.get_config()[keras_model.get_config().keys()[0]] but now, I am getting a list of dictionary back instead of a dictionary as in (just note the begginning and end bracests):

[{'class_name': 'Conv2D', 'config': {'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'distribution': u'uniform', 'scale': 1.0, 'seed': None, 'mode': u'fan_avg'}}, 'name': u'conv2d_1', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'dtype': u'float32', 'activation': 'linear', 'trainable': True, 'data_format': u'channels_last', 'filters': 32, 'padding': u'same', 'strides': (1, 1), 'dilation_rate': (1, 1), 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'batch_input_shape': (None, 40, 500, 1), 'use_bias': True, 'activity_regularizer': None, 'kernel_size': (7, 7)}}, {'class_name': 'BatchNormalization', 'config': {'beta_constraint': None, 'gamma_initializer': {'class_name': 'Ones', 'config': {}}, 'moving_mean_initializer': {'class_name': 'Zeros', 'config': {}}, 'name': u'batch_normalization_1', 'epsilon': 0.001, 'trainable': True, 'moving_variance_initializer': {'class_name': 'Ones', 'config': {}}, 'beta_initializer': {'class_name': 'Zeros', 'config': {}}, 'scale': True, 'axis': 1, 'gamma_constraint': None, 'gamma_regularizer': None, 'beta_regularizer': None, 'momentum': 0.99, 'center': True}}, {'class_name': 'Activation', 'config': {'activation': 'relu', 'trainable': True, 'name': u'activation_1'}}, {'class_name': 'MaxPooling2D', 'config': {'name': u'max_pooling2d_1', 'trainable': True, 'data_format': u'channels_last', 'pool_size': (5, 5), 'padding': u'valid', 'strides': (5, 5)}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_1'}}, {'class_name': 'Conv2D', 'config': {'kernel_constraint': None, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'distribution': u'uniform', 'scale': 1.0, 'seed': None, 'mode': u'fan_avg'}}, 'name': u'conv2d_2', 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'linear', 'trainable': True, 'data_format': u'channels_last', 'padding': u'same', 'strides': (1, 1), 'dilation_rate': (1, 1), 'kernel_regularizer': None, 'filters': 64, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'use_bias': True, 'activity_regularizer': None, 'kernel_size': (7, 7)}}, {'class_name': 'BatchNormalization', 'config': {'beta_constraint': None, 'gamma_initializer': {'class_name': 'Ones', 'config': {}}, 'moving_mean_initializer': {'class_name': 'Zeros', 'config': {}}, 'name': u'batch_normalization_2', 'epsilon': 0.001, 'trainable': True, 'moving_variance_initializer': {'class_name': 'Ones', 'config': {}}, 'beta_initializer': {'class_name': 'Zeros', 'config': {}}, 'scale': True, 'axis': 1, 'gamma_constraint': None, 'gamma_regularizer': None, 'beta_regularizer': None, 'momentum': 0.99, 'center': True}}, {'class_name': 'Activation', 'config': {'activation': 'relu', 'trainable': True, 'name': u'activation_2'}}, {'class_name': 'MaxPooling2D', 'config': {'name': u'max_pooling2d_2', 'trainable': True, 'data_format': u'channels_last', 'pool_size': (4, 100), 'padding': u'valid', 'strides': (4, 100)}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_2'}}, {'class_name': 'Flatten', 'config': {'trainable': True, 'name': u'flatten_1', 'data_format': u'channels_last'}}, {'class_name': 'Dense', 'config': {'kernel_initializer': {'class_name': 'RandomUniform', 'config': {'maxval': 0.05, 'seed': None, 'minval': -0.05}}, 'name': u'dense_1', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'relu', 'trainable': True, 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'units': 100, 'use_bias': True, 'activity_regularizer': None}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_3'}}, {'class_name': 'Dense', 'config': {'kernel_initializer': {'class_name': 'RandomUniform', 'config': {'maxval': 0.05, 'seed': None, 'minval': -0.05}}, 'name': u'dense_2', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'softmax', 'trainable': True, 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'units': 10, 'use_bias': True, 'activity_regularizer': None}}]

Everything is based on the code from DCASE2018 and I wish to change it as little as possible in this stage. How do I access the first dictionary of that dictionary? how do I chain access these dictionaries?

By the way, I have tried type(keras_model.get_config()['layers'] and I am still getting a list back.

edit: adding the original keras_model.get_config() dictionary:

{'layers': [{'class_name': 'Conv2D', 'config': {'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'distribution': u'uniform', 'scale': 1.0, 'seed': None, 'mode': u'fan_avg'}}, 'name': u'conv2d_1', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'dtype': u'float32', 'activation': 'linear', 'trainable': True, 'data_format': u'channels_last', 'filters': 32, 'padding': u'same', 'strides': (1, 1), 'dilation_rate': (1, 1), 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'batch_input_shape': (None, 40, 500, 1), 'use_bias': True, 'activity_regularizer': None, 'kernel_size': (7, 7)}}, {'class_name': 'BatchNormalization', 'config': {'beta_constraint': None, 'gamma_initializer': {'class_name': 'Ones', 'config': {}}, 'moving_mean_initializer': {'class_name': 'Zeros', 'config': {}}, 'name': u'batch_normalization_1', 'epsilon': 0.001, 'trainable': True, 'moving_variance_initializer': {'class_name': 'Ones', 'config': {}}, 'beta_initializer': {'class_name': 'Zeros', 'config': {}}, 'scale': True, 'axis': 1, 'gamma_constraint': None, 'gamma_regularizer': None, 'beta_regularizer': None, 'momentum': 0.99, 'center': True}}, {'class_name': 'Activation', 'config': {'activation': 'relu', 'trainable': True, 'name': u'activation_1'}}, {'class_name': 'MaxPooling2D', 'config': {'name': u'max_pooling2d_1', 'trainable': True, 'data_format': u'channels_last', 'pool_size': (5, 5), 'padding': u'valid', 'strides': (5, 5)}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_1'}}, {'class_name': 'Conv2D', 'config': {'kernel_constraint': None, 'kernel_initializer': {'class_name': 'VarianceScaling', 'config': {'distribution': u'uniform', 'scale': 1.0, 'seed': None, 'mode': u'fan_avg'}}, 'name': u'conv2d_2', 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'linear', 'trainable': True, 'data_format': u'channels_last', 'padding': u'same', 'strides': (1, 1), 'dilation_rate': (1, 1), 'kernel_regularizer': None, 'filters': 64, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'use_bias': True, 'activity_regularizer': None, 'kernel_size': (7, 7)}}, {'class_name': 'BatchNormalization', 'config': {'beta_constraint': None, 'gamma_initializer': {'class_name': 'Ones', 'config': {}}, 'moving_mean_initializer': {'class_name': 'Zeros', 'config': {}}, 'name': u'batch_normalization_2', 'epsilon': 0.001, 'trainable': True, 'moving_variance_initializer': {'class_name': 'Ones', 'config': {}}, 'beta_initializer': {'class_name': 'Zeros', 'config': {}}, 'scale': True, 'axis': 1, 'gamma_constraint': None, 'gamma_regularizer': None, 'beta_regularizer': None, 'momentum': 0.99, 'center': True}}, {'class_name': 'Activation', 'config': {'activation': 'relu', 'trainable': True, 'name': u'activation_2'}}, {'class_name': 'MaxPooling2D', 'config': {'name': u'max_pooling2d_2', 'trainable': True, 'data_format': u'channels_last', 'pool_size': (4, 100), 'padding': u'valid', 'strides': (4, 100)}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_2'}}, {'class_name': 'Flatten', 'config': {'trainable': True, 'name': u'flatten_1', 'data_format': u'channels_last'}}, {'class_name': 'Dense', 'config': {'kernel_initializer': {'class_name': 'RandomUniform', 'config': {'maxval': 0.05, 'seed': None, 'minval': -0.05}}, 'name': u'dense_1', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'relu', 'trainable': True, 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'units': 100, 'use_bias': True, 'activity_regularizer': None}}, {'class_name': 'Dropout', 'config': {'rate': 0.3, 'noise_shape': None, 'trainable': True, 'seed': None, 'name': u'dropout_3'}}, {'class_name': 'Dense', 'config': {'kernel_initializer': {'class_name': 'RandomUniform', 'config': {'maxval': 0.05, 'seed': None, 'minval': -0.05}}, 'name': u'dense_2', 'kernel_constraint': None, 'bias_regularizer': None, 'bias_constraint': None, 'activation': 'softmax', 'trainable': True, 'kernel_regularizer': None, 'bias_initializer': {'class_name': 'Zeros', 'config': {}}, 'units': 10, 'use_bias': True, 'activity_regularizer': None}}], 'name': u'sequential_1'}

Here it is. The idea is to construct a list from the dict keys. (Tested with python 3.7)

d = {'x': 'y'}

print(list(d.keys())[0])

Output:

x

Lesson 6, You might get new cats, some may die, some may become your dinner (we should For these three problems, Python uses three different solutions - Tuples , lists, Notice how instead of identifying the value by a number, like in the cats and is not in the dictionary" #Use the function keys() - #This function returns a list #of� Return Value from get() get() method returns: the value for the specified key if key is in dictionary.; None if the key is not found and value is not specified.; value if the key is not found and value is specified.

In python 3.6

>>> dic = {'a':'b'}

>>> dic.keys()
dict_keys(['a'])

>>> dic.keys()[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'dict_keys' object does not support indexing

This means that you cannot access the elements of a dictionary by dic[dic.keys()[0]].

Please provide the dictionary keras_model.get_config()

20. Dictionaries — How to Think Like a Computer Scientist: Learning , Another way to create a dictionary is to provide a list of key:value pairs using the same Instead of two integer indices, we use one index, which is a tuple of integers. argument is the value get should return if the key is not in the dictionary:. When you return, you go back somewhere after being away. If you haven't been to Disney World since your fifth birthday, it might be fun to return when you're older.

Turns out it was a difference of versions for keras.

Python, Python program to get. # dictionary keys as list. def getList( dict ):. list = []. for key in dict .keys():. list .append(key). return list. # Driver program. Standard dict objects are not sorted and so do not guarantee or preserve any ordering. This is because since you usually use a dict by get a value for a key ordering is unimportant.

5. Data Structures — Python 3.8.5 documentation, Remove the item at the given position in the list, and return it. Sort the items of the list in place (the arguments can be used for sort customization, see Another useful data type built into Python is the dictionary (see Mapping Types — dict). In return definition: If you do something in return for what someone else has done for you, you do it because | Meaning, pronunciation, translations and examples

Python Dictionary get(), Python get() method Vs dict[key] to Access Elements. get() method returns a default value if the key is missing. However, if the key is not found when you use � 38 synonyms of return from the Merriam-Webster Thesaurus, plus 86 related words, definitions, and antonyms. Find another word for return. Return: to bring, send, or put back to a former or proper place.

15 things you should know about Dictionaries in Python, Alternatively, we can construct a dictionary using an iterable (e.g. list of tuples). To avoid getting an exception with undefined keys, we can use the method This method modifies the dictionary in-place, returning None. Support for generalized return types means that you can return a lightweight value type instead of a reference type to avoid additional memory allocations. .NET provides the System.Threading.Tasks.ValueTask<TResult> structure as a lightweight implementation of a generalized task-returning value.

Comments
  • I wish NOT to get a list but a dictionary.
  • Can you post the dict that is returned from keras_model.get_config() ?
  • OK. The config data contains a list under the 'layers' key. So data['layers'][0] return the first item in the list. I hope it is the one you are looking for. (Each entry contains another dict named 'config')
  • Hi, I have added the dictionary keras_model.get_config().