Alex Ivanov
Alex Ivanov

Reputation: 737

tuning neural network parameter via Hyperopt: how to dump trials

When I try to save hyperopt.trials object, which contains information about auto params tuning in neural network,

best = fmin(fn = objective, 
            space = space, 
            algo = tpe.suggest, # or rand.suggest for random params selection 
            max_evals = max_trials, 
            trials = trials) #, rstate = np.random.RandomState(50)
pickle.dump(trials, open("neuro.hyperopt", "wb"))

it gives the error:

can't pickle _thread.RLock objects

Moreover, it loads on my local drive a file of 10GB size. That is, it saves not only the trials object, but the whole model. Would you help me to save trials object with less size (e.g. the XGBoost trials file's size is 1Mb) and avoid the error.

Thank you.

Upvotes: 0

Views: 365

Answers (1)

Alex Ivanov
Alex Ivanov

Reputation: 737

In my case it was because the models stored in the trials were not pickle-able. I tried to save tf.keras.optimizers.Adam(learning_rate = 0.001) object. When I added the string 'Adam' instead, the error disapeared.

Of course, it creates another problem: how to setup learining rate for the optimizor. But it seems to be easier. One way is to replace the keras object with string in the trials.trials object before saving:

for trial in trials.trials:
    if 'result' in trials.keys():
        trials['result'].pop('model', None) # https://stackoverflow.com/questions/15411107/delete-a-dictionary-item-if-the-key-exists
# proceed with pickling
pickle.dump(trials, open("trials.pkl","wb"))

(I took it from here)

Upvotes: 0

Related Questions