gin logo gin: functions as config

1 2 3 4 5
Notes

You can also configure functions in gin. That means that we can go further than mere settings, we can actively link code!

config.gin

simulate.n_samples = 200
simulate.random_func = @random_triangle

random_triangle.minval = 0
random_triangle.maxval = 100

simulate.py

import gin
import random

@gin.configurable
def random_uniform(minval=0, maxval=1):
    return random.uniform(minval, maxval)

@gin.configurable
def random_triangle(minval=0, maxval=1):
    middle = (maxval - minval)/2
    return random.triangular(minval, maxval, middle)

@gin.configurable
def simulate(random_func, n_samples):
    return sum(random_func() for i in range(n_samples))

if __name__ == "__main__":
    gin.parse_config_file('config.gin')
    print(simulate())

Notice how simulate() is able to run without any parameters being passed to it.