0

I use .jsonnet file as a template for AllenNLP model config

I search through hyperparameter space with Optuna package and evaluate that template on each step with suggested hyperparameters as extVars. Then I train a model and save it to an archive.

The problem is, apart from hyperparameters, some parts of my .jsonnet config include file paths relative to a extVar variable(a dir inside my package). Those file paths get rendered, too and in the end I have absolute paths in my model.tar.gz, which is wrong, as they may even not exist on a machine loading that archive

.jsonnet:

{
...
  "train_data_path": std.extVar("TRAIN_DATA_PATH"),
  "validation_data_path": std.extVar("VALID_DATA_PATH"),
...
}

rendered.json :

{
...
  "train_data_path": "/home/user/datasets/train.json",
  "validation_data_path": "/home/user/datasets/valid.json",
...
}

So I would like to save the original path expression instead and supply an environment variable on loading, but I haven`t found a way to serialize a .jsonnet file in python, only .json

Ojomio
  • 843
  • 1
  • 8
  • 17
  • I would expect that if you set `TRAIN_DATA_PATH` to a relative path, it will be relative in the final .json file as well. Do you not see that? – Dirk Groeneveld Jul 23 '21 at 21:14

1 Answers1

0

thank you for trying Optuna integration. (I'm the author of AllenNLP integration for Optuna)

It's difficult to write path expressions in JSON because JSON doesn't support reading environment variables.

Instead, you can use allennlp-optuna to retrain a model with the original config file (not generated one). https://github.com/himkt/allennlp-optuna#4-retrain-a-model-with-optimized-hyperparameters

After installing allennlp-optuna, you can tune hyperparameters by allennlp tune and train a model with optimized hyperparameters by allennlp retrain.

himkt
  • 31
  • 4