Syntax Test¶
This is a simple notebook for experimenting with Forgather's syntax using the low-level API
See: Low-level API
In [1]:
Copied!
from forgather.config import ConfigEnvironment
from forgather.latent import Latent
from forgather.codegen import generate_code
from forgather.nb.notebooks import display_codeblock
from pprint import pp
# Construct a configuration environment
env = ConfigEnvironment()
from forgather.config import ConfigEnvironment
from forgather.latent import Latent
from forgather.codegen import generate_code
from forgather.nb.notebooks import display_codeblock
from pprint import pp
# Construct a configuration environment
env = ConfigEnvironment()
Define a configuration¶
In [2]:
Copied!
# Define a configuration
document = """
random_tensor: !call:torch:randn [ 2, 2 ]
"""
# Convert the configuration to a graph
config = env.load_from_string(document).config
pp(config)
# Define a configuration
document = """
random_tensor: !call:torch:randn [ 2, 2 ]
"""
# Convert the configuration to a graph
config = env.load_from_string(document).config
pp(config)
{'random_tensor': SingletonNode('torch:randn', *(2, 2), identity=139888396855632, **{})}
Materialize the "random_tensor" target¶
In [3]:
Copied!
Latent.materialize(config, mtargets="random_tensor")
Latent.materialize(config, mtargets="random_tensor")
Out[3]:
tensor([[ 0.7128, -0.5204],
[ 1.4749, 0.8098]])
Convert a configuraiton into executable Python code¶
Note that generate_code() takes an optional Jinja2 template as an arguemnt, which can be used to customize the output.
In [4]:
Copied!
code = generate_code(config)
display_codeblock("python", code)
code = generate_code(config)
display_codeblock("python", code)
from torch import randn
def construct(
):
return {
'random_tensor': randn(
2,
2,
),
}
Run generated code¶
In [5]:
Copied!
exec(code)
construct()
exec(code)
construct()
Out[5]:
{'random_tensor': tensor([[-0.7962, 0.5893],
[ 0.8036, -0.2863]])}
In [ ]:
Copied!