348. Jinja2: Model Configuration Version Control

Creating Templates

Jinja2 is a template engine for Python which can be useful when you want to keep track of model training configuration versions.

Here is one way to Implement it by combining them with a YAML file.

1. Create Jinja2 Template: template.yaml.j2
DATASET:
  TEST_SET: '{{TEST_SET_DIR | default('./test')}}'
  TRAIN_SET: '{{TRAIN_SET_DIR | default('./train')}}'
MODEL:
  NAME: FCN
TRAIN:
  BASE_SIZE: {{BATCH_SIZE | default(4)}}
  EPOCHS: {{EPOCHS | default(25)}}
TEST:
  BASE_SIZE: {{BATCH_SIZE | default(4)}}
  EPOCHS: {{EPOCHS | default(25)}}
2. Render Configuration YAML file from template
from jinja2 import Template

#File name to export rendered file
render_to  ='configured.yaml'

# Open jinja2 template which we've just created
with open('model_config.yaml.j2') as file_:
    template = Template(file_.read())

# Set hyper-parameter configuration:
# It might be useful for the user to specify these through a GUI
param_config = {
    "TEST_SET": "./test_set",
    "TRAIN_SET": "./train_set",
    "BASE_SIZE": "16",
    "EPOCHS": "25",
}

#Render using template
rendered_conf = template.render(param_config)

#Write file
with open(render_to, 'w') as f:
    f.write(rendered_conf)    
Generated Yaml FILE
DATASET:
  TEST_SET: './test'
  TRAIN_SET: './train'
MODEL:
  NAME: FCN
TRAIN:
  BASE_SIZE: 4
  EPOCHS: 25
TEST:
  BASE_SIZE: 4
  EPOCHS: 25

Now you can use the generated YAML file to start the training.

Join the Newsletter

Subscribe to get our latest content by email.
    We won't send you spam. Unsubscribe at any time.