google / google/flax

Pickling a trained model (NNX)

Open
#4,247 14 comments 3 reactions 0 assignees View on GitHub
Dominant language
Jupyter Notebook
Stars
7.3k
Forks
833
Avg merge
5h 11m
Merged PRs (30d)
5

Description

I train small models and while prototyping and testing I wish to store trained models in a simple way (also having the model configuration and training-stats inside the model object as dicts/arrays). Idially i want to deal with a single object and being able to simply:

```python
with open('my-model.pk', 'wb') as file:
pickle.dump(model, file)
```
and later:
```python
model = pickle.load('my-model.pk')
```

Similar to `torch.save`.

When i naively try to do the above I get:

```
----> 2 pickle.dump(model, file)
AttributeError: Can't pickle local object 'variance_scaling..init'
```

Splitting into `graphdef` and `state` results in the following error:
```
cannot pickle 'PyTreeDef' object
```

While I am aware there is `orbax` and it might save the state, I really wish it would be possible to avoid that dependency and keep things simple. Is there a trick or a workaround I can use to achieve the desired functionality?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.