def __getstate__(self): state = self.__dict__.copy() state['_random_state'] = random.getstate() state['_np_random_state'] = np.random.get_state() return state # For unpickling.