This error message typically occurs when you try to save a PyTorch object that contains a reference to a C-level generator object. The C-level generator object is not serializable, so it cannot be pickled.
There are a few different ways to work around this issue, depending on the specific situation:
- Use
torch.manual_seed()to set the seed for the random number generator instead of using a generator object. This can be a good option if you only need to use the same random numbers across different runs of your code. - Create a new generator object each time you need to generate random numbers, rather than reusing the same generator object. This can be done by calling
torch.Generator()to create a new generator object. - If you need to use the same generator object across multiple processes or threads, you can use
torch.get_rng_state()andtorch.set_rng_state()to save and restore the state of the random number generator. This allows you to create a new generator object in each process or thread, but still ensure that they produce the same sequence of random numbers. - If you need to save and load a PyTorch object that contains a generator object, you can use the
dilllibrary instead of the built-inpicklelibrary.dillis a more advanced serializer that can handle a wider range of Python objects.
Here is an example of using dill to save and load a PyTorch object that contains a generator object:
import dill
import torch
# Create a PyTorch object that contains a generator object
model = torch.nn.Linear(10, 1)
generator = torch.Generator()
generator.manual_seed(123)
output = model(torch.randn(1, 10, generator=generator))
# Save the PyTorch object using dill
with open("model.pkl", "wb") as f:
dill.dump((model, generator, output), f)
# Load the PyTorch object using dill
with open("model.pkl", "rb") as f:
model, generator, output = dill.load(f)
