我有一个keras_model.get_config()
返回的字典。 (由print(type(keras_model.get_config()))
决定)。我在代码行中收到错误:
if keras_model.get_config()[0]['config']['data_format'] == 'channels_first':
错误表明字典没有0
键,这很明显:
回溯(最近通话最近): 文件“ task1a.py”,行1204,在 sys.exit(main(sys.argv)) 主文件“ task1a.py”,第234行 覆盖=覆盖 do_testing中的文件“ task1a.py”,行982 如果keras_model.get_config()[0] ['config'] ['data_format'] ==>'channels_first': KeyError:0
我继续通过keras_model.get_config()[keras_model.get_config().keys()[0]]
进行访问,但是现在,我得到的是字典的列表,而不是像中的字典(请注意开头和结尾大括号):
[{'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}}]
一切都基于DCASE2018中的代码,我希望在此阶段尽可能少地更改它。如何访问该词典的第一本词典?如何链接访问这些词典?
顺便说一句,我已经尝试过type(keras_model.get_config()['layers']
,但仍然可以得到列表。
编辑:
添加原始的keras_model.get_config()
字典:
{'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'}
答案 0 :(得分:1)
是的。这个想法是从字典键构造一个列表。 (通过python 3.7测试)
d = {'x': 'y'}
print(list(d.keys())[0])
输出:
x
答案 1 :(得分:0)
在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
这意味着您无法通过dic[dic.keys()[0]]
访问字典的元素。
请提供字典keras_model.get_config()
答案 2 :(得分:0)