Tracking model parameters and settings for AI using JSON.
Project description
AI-JSONABLE
Parameter and settings tracking in Python3 for jsonable output.
Installation
pip3 install aijson
Philosophy
Saving and serializing in Python3 is supported by, for instance, pickle
and dill
. However, we believe that logging parameters in a Pythonic and flexible way is undersupported. Once a model or experiment has been executed, it should be easy to inspect which parameters were used. If the experiment is to be rerun or modified, it should be possible to do this with some simple overrides.
Minimum working example
A minimal example is in example/config.py
, which requires PyTorch and imports from example/themod.py
.
To install do:
pip3 install torch
example/themod.py
:
from aijson.decorate import aijson
import torch
@aijson
class MyPyTorchModel(torch.nn.Module):
def __init__(self, n_layers, n_hidden, n_input):
super().__init__()
self.rnn = torch.nn.GRU(n_hidden, n_hidden, n_layers)
self.embed = torch.nn.Embedding(n_input, n_hidden)
@aijson
class MyCompose:
def __init__(self, functions):
self.functions = functions
def __call__(self, x):
for f in self.functions:
x = f(x)
return x
example/config.py
:
import json
from example.themod import MyPyTorchModel, MyCompose
from aijson import aijson, logging_context
from torch.nn import GRU
with logging_context() as lc:
m = MyPyTorchModel(n_layers=1, n_hidden=512, n_input=64)
rnn = aijson(GRU)(
input_size=2,
hidden_size=5,
)
n = MyCompose(functions=[m, m, 2, rnn])
with open('mymodel.ai.json', 'w') as f:
json.dump(lc, f)
In example/themod.py
you can see that classes (and functions) whose parameter settings should be tracked are decorated with @aijson
. Predefined functions (as in torch.nn.XXX
) are similarly wrapped with aijson(...)
. To create a single JSON-able logging instance in a Python dictionary, one uses the logging_context
context manager. Having wired the model together in Python, all parameters chosen are recursively saved in the dictionary lc
.
To run do:
python3 -m example.config
This should give output in mymodel.ai.json
, which should look like this:
{
"var0": {
"module": "example.themod",
"caller": "MyPyTorchModel",
"kwargs": {
"n_layers": 1,
"n_hidden": 512,
"n_input": 64
}
},
"var1": {
"module": "torch.nn.modules.rnn",
"caller": "GRU",
"kwargs": {
"input_size": 2,
"hidden_size": 5
}
},
"var2": {
"module": "example.themod",
"caller": "MyCompose",
"kwargs": {
"functions": [
"$var0",
"$var0",
2,
"$var1"
]
}
}
}
The JSON output is a dictionary representation of the build tree/ graph. If a parameter is JSON-able, then it will be directly saved in the kwargs
subdictionary. Otherwise, it will be defined recursively. Hence the underlying assumption is that all parameters are either JSON-able or are Python objects whose parameters are JSON-able or are Python objects..., and so on. The base/ trunk node is the variable with highest index.
Once this output has been produced, it's possible to rebuild the object using the same parameters in the following way:
import json
from aijson import build
with open('mymodel.ai.json') as f:
cf = json.load(f)
rebuilt = build(cf)
This means that one doesn't need the code in example/config.py
but only the items imported there (i.e. whatever is in example/themod.py
and torch
etc.).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file ai-jsonable-0.0.1.tar.gz
.
File metadata
- Download URL: ai-jsonable-0.0.1.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.8.2 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b60c3b57b0157eb57a632d98ef2d20d6072a3b7028dc6f0d7b8662f970e5f52 |
|
MD5 | c841f82c535be4ca58275d8c06c24a6b |
|
BLAKE2b-256 | 4f38635bc62c80288d975adb2677dde46d9563fbb995b203bf68625cb17b443c |