Q:

Dictionary give the values to the certain key, but the others get the same values

I want to create a dictionary to record the accurancy when I run the program. There are some problem when I give the values to the certain key
Here, I create a dictionary to record the accurancy when it trains or validate 
acc = dict.fromkeys(['train', 'val'], dict.fromkeys(['pa', 'iou', 'dice'], 0))

The original dictionary looks like this 
{'train': {'pa': 0, 'iou': 0, 'dice': 0}, 'val': {'pa': 0, 'iou': 0, 'dice': 0}}

Now, I give the some values for testing
acc['train']['pa'] = 0.9
acc['train']['iou'] = 0.8
acc['train']['dice'] = 0.7

The problems is all the values in key val becomes the same
{'train': {'pa': 0.9, 'iou': 0.8, 'dice': 0.7}, 'val': {'pa': 0.9, 'iou': 0.8, 'dice': 0.7}}

A:

If the provided value is a mutable object (whose value can be modified) like list, dictionary, etc.,  when the mutable object is modified, each element of the sequence also gets updated. 
This is because, each element is assigned a reference to the same object (points to the same object in the memory). 
In your problem, dict.fromkeys(['pa', 'iou', 'dice'], 0) returns a dictionary which is mutable type, so each element is assigned to the same object. 
To avoid this issue, you can use dictionary comprehension. 
from copy import copy
# keys
keys = ['train', 'val']
value = dict.fromkeys(['pa', 'iou', 'dice'], 0 )

acc = { key : copy(value) for key in keys }

print(acc)

# updating the value
acc['train']['pa'] = 2 
print(acc)

Source: https://www.programiz.com/python-programming/methods/dictionary/fromkeys 

