简体   繁体   中英

Python: how to organize the code structure to implement multiple options

I am new to Python programming. I wrote a python script for LSTM for modelling sequence data by using Keras. Overall, there are two options now that I need to manually adjust for each run, which are learning rate and model.

I want to test four different learning rates (ie, 1e-2, 1e-3, 1e-4 and 1e-5) and four different models (ie, model1, mode2, model3 and model4). It should be noted that these models are those provided by Keras, eg, LSTM, SimpleRNN or GRU. I use model1, model2..., just for illustration.

At the moment, my code is structured as follows:

#pseudo-codes
learningRate = 1e-2 #tunable parameter
model = Sequential()
model.add(model1(hidden_uints)) #other inputs within the model is omitted
#model1 is to be changed to model2, model3 and model4 later

model.complie(optimizer = sgd(lr = learningRate))

model.fit(xtrain,ytrain)

Every time the training is done, I will adjust the learning rate and model3 for another run. I feel there should be a more appropriate code structure to implement these options (4*4 = 16 runs) all at once.

You can try something like this:

learningRates = [1e-2, 1e-3, 1e-4, 1e-5]
models = [model1(), model2(), model3(), model4()]

run_opts = [(lrnRate, mdl) for lrnRate in learningRates for mdl in models]

for run_opt in run_opts:

    learningRate, model_num = run_opt
    model = Sequential()
    model.add(model_num)
    model.compile(optimizer = sgd(lr = learningRate))

    model.fit(xtrain, ytrain)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM