Search code examples
jsonneural-networkpytorchconfig

config a neural network with a JSON file


I want to config a neural network model (number of layers, number of neurons per layer, activation functions, ...) from a JSON file. But honestly, I have no idea how to do it. When I search the internet with "hyperparameters config PyTorch" or "hyperparameters tunning PyTorch" can't find anything interesting. The search result is more about hyperparameters optimization, not config from JSON. Anyone has any idea how to do this (JSON file config) or do you know any useful tutorials that I can watch/read, please? That would be a great help!
Thank you in advance


Solution

  • Write your parameters to json file with proper name like following

    {
       "number_layers":1,
       "number_neurons":2,
       "activation_function":"relu",
       "training":{
          "learning_rate":0.01
       }
    }
    

    Then read the json file

    import json
    
    
    with open('xxx.json', 'r', encoding='utf-8') as f:
        config = json.loads(f.read())
    

    and access the parameter you want with

    config['number_layers']
    config['training']['learning_rate']