简体   繁体   中英

I can't get a value with key in python dictionary

I'm using 'Matchzoo' text retrieval library which is based on keras. I want to use the trained result, but it's dictionary and I can't get the value with shown keys.

After training the model,

>>> history = model.fit_generator(train_generator, epochs=1, callbacks=[evaluate], workers=5, use_multiprocessing=False)
Epoch 1/1
17/17 [==============================] - 1s 84ms/step - loss: 1.0864
Validation: normalized_discounted_cumulative_gain@3(0.0): 0.03594548089735716 - normalized_discounted_cumulative_gain@5(0.0): 0.04159539212363794 - mean_average_precision(0.0): 0.044539607256746286

The result is in history.history dictionary.

>>> history.history
{'loss': [1.2375952883301495],
 mean_average_precision(0.0): [0.02962496886265635],
 normalized_discounted_cumulative_gain@3(0.0): [0.018740542440172665],
 normalized_discounted_cumulative_gain@5(0.0): [0.027987588892336258]}

There are 4 keys, I can see them when I check the dict's keys.

>>> history.history.keys()
dict_keys(['loss', normalized_discounted_cumulative_gain@3(0.0), normalized_discounted_cumulative_gain@5(0.0), mean_average_precision(0.0)])

But when I try to use them, I can't.

>>> history.history[normalized_discounted_cumulative_gain@3(0.0)]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-36-9526d848c6d7> in <module>()
----> 1 history.history[normalized_discounted_cumulative_gain@3(0.0)]

NameError: name 'normalized_discounted_cumulative_gain' is not defined

What I can't understand is that I can use some keys, but not all.

>>> history.history['loss']
[1.0869888107353283]
>>> history.history[mean_average_precision(0.0)]
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-985db34a9846> in <module>()
----> 1 history.history[mean_average_precision(0.0)]

NameError: name 'mean_average_precision' is not defined

I wondered if it's because the keys were not string, but it didn't work either.

>>> history.history['mean_average_precision(0.0)']
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-43-985db34a9846> in <module>()
----> 1 history.history[mean_average_precision(0.0)]

NameError: name 'mean_average_precision' is not defined

Can someone tell me why and how I can solve this problem? Is there anything I should check?

As you said, it could be because they are not strings. If you are sure that's the order they are coming in, try indexing through the dictionary using the keys as shown below..

history.history[list(history.history.keys())[2]]

and

history.history[list(history.history.keys())[3]]

etc..

By doing this, we avoid guessing the data type of the key by letting the computer retrieve the object.

Most possibly, normalized_discounted_cumulative_gain@3(0.0) , etc. are not strings. They might be objects of a different type. If you want to check what exactly it is, you can try this

type(list(history.history.keys())[2])

This will show str for 'loss' as it is a string and something else for the others. If you try that, please comment on what type it is in the comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM