2
votes

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 (i.e., 1e-2, 1e-3, 1e-4 and 1e-5) and four different models (i.e., model1, mode2, model3 and model4). It should be noted that these models are those provided by Keras, e.g., 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.

1

1 Answers

3
votes

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)