hydra in jupyter notebook with working directory management

65 Views Asked by At

I'm using hydra in jupyter notebook but find that I can only get access to the configuration file and initiate objects. When I run this code:

initialize(version_base=None, config_path="config")
cfg = compose(config_name="config.yaml")
print(cfg.hydra.run.dir)

and my config file is

train_nn:
    _target_: train_nn.Train_NN

    # Variables for loading
    loading_saved_model_S: False     # Indicator for loading S
    loading_saved_model_V_u: False   # Indicator for loading V_u
    loading_saved_model_V_v: False   # Indicator for loading V_v

    # Network structure
    nn_width: 50            # Network width per layer
    nn_num_layers: 4        # Number of layers

    # Training parameters
    lr: 1e-4              # Learning rate for main
    lr_init: 1e-3         # Learning rate for initialization
    epochs_init: 10001     # Epochs for initialization
    epochs: 100001        # Epochs for main
    # Variables for Environment
    rho: 0.05             # Discount rate
    delta: 0.2            # Match exit rate
    beta: 0.5             # Surplus share
    b: 0.5                # Unemployment benefit
    xi: 2.0               # Elasticity parameter
    kappa: 5.4            # Matching function parameter
    init_kappa: 5.4       # init kappa for homotopy training
    kappa_iter: 1        # iteration times for kappa
    nu: 0.5               # Matching function parameter
    lam_L: 0.8            # Jump rate from low to high
    lam_H: 0.8            # Jump rate from high to low (same as lam_L in this case)
    z_0: 1.0 
    dz: 0.2 
    nx: 4                # Number of worker types
    ny: 5                # Number of firm types
    prod_type: 'EXP'      # Production function type
    alpha_type: 'continuous'  # Alpha function type

seed: 777
save_freq: 10000      # save once every save_freq loops
sample_low: 256       # sample number
sample_high: 512
lr_low: 1e-5
lr_high: 2e-5

hydra:
    run:
        dir: outputs/nx:${train_nn.nx}-ny:${train_nn.ny}-b:${train_nn.b}-rho_${train_nn.rho}-delta:${train_nn.delta}-xi:${train_nn.xi}-beta:${train_nn.beta}-lambda:${train_nn.lam_L}-nu:${train_nn.nu}-kappa:${train_nn.kappa}-dz:${train_nn.dz}-${train_nn.prod_type}/${now:%Y-%m-%d_%H-%M-%S}

config_name: ${hydra:job.name}

I get this error:

ConfigAttributeError: Key 'hydra' is not in struct
    full_key: hydra
    object_type=dict

I think this may be because in jupyter notebook we can't use more superior function embedded in @hydry.main(). But how can I manage working directory in jupyter notebook just like that using @hydra.main() in .py file?

0

There are 0 best solutions below