'Writing a dictionary and a list class in a pickle file using python- (Error: Cant pickle local object)
I need to read a model file modify the weight and write back to an updated model file using the pickle library. The data consists of a dictionary and a ndarray list. While doing that I can successfully read the value and modify it but can not dump it using pickle.dump. The code is as below,
import pickle
# Viewing the element tye in the pickle file
with open('/content/Training/PPO_model_EV.pkl', 'rb') as f:
data = pickle.load(f)
# Viewing the element type in the pickle file and data
k=0
for i in data:
if k==0:
i_dict=i
print(type(i_dict),i_dict)
if k==1:
i_list=i
print(type(i_list),i_list)
k=k+1
# Write the value back to another pickle file
pickle_out = open("/content/Modified_model.pkl","wb")
pickle.dump((i_dict,i_list),pickle_out)
pickle_out .close()
Here,
dict: i_dict (16 items) {'gamma': 0.99, 'n_steps': 128, 'vf_coef': 0.5, 'ent_coef': 0.01, 'max_grad_norm': 0.5, ...}
list: i_list (15 items) [ndarray with shape (60, 64), ndarray with shape (64,), ndarray with shape (60, 64), ndarray with shape (64,), ndarray with shape (64, 64), ...]
The output of the code is as follows,
<class 'dict'> {'gamma': 0.99, 'n_steps': 128, 'vf_coef': 0.5, 'ent_coef': 0.01, 'max_grad_norm': 0.5, 'learning_rate': <function constfn.<locals>.func at 0x7f71599600e0>, 'lam': 0.95, 'nminibatches': 4, 'noptepochs': 4, 'cliprange': <function constfn.<locals>.func at 0x7f7159960290>, 'verbose': 1, 'policy': <class '__main__.CustomPolicy'>, 'observation_space': Box(0.0, 1.0, (12, 5), float16), 'action_space': Box(-1.0, 1.0, (2,), float16), 'n_envs': 1, '_vectorize_action': False}
<class 'list'> [array([[ 0.33291984, -0.1476613 , -0.207661 , ..., 0.26993886,
-0.4049534 , 0.30404437],
[-0.23681359, 0.02351136, 0.2928835 , ..., -0.30670926,
0.06748895, 0.12044807],
[-0.15989211, 0.03464196, -0.058044 , ..., -0.2320129 ,
-0.04890745, -0.14575315],
...,
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-175-c7240e147380> in <module>()
19 # Write the value back to another pickle file
20 pickle_out = open("/content/Modified_model.pkl","wb")
---> 21 pickle.dump((i_dict,i_list),pickle_out)
22 pickle_out .close()
23
AttributeError: Can't pickle local object 'constfn.<locals>.func'
Any help and suggestion will be greatly appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
