0

I have the following model fit using python:

history_1 = model.fit(x_train, y_train, batch_size=32, epochs=10, verbose=True, validation_split=.1)
loss_1, accuracy_1  = model.evaluate(x_test, y_test, verbose=False)

and then the next line of code is

history_2 = model.fit(x_train, y_train, batch_size=60, epochs=10, verbose=True, validation_split=.1)
loss_2, accuracy_2  = model.evaluate(x_test, y_test, verbose=False)

I would rather want to put this in a function, specifying different parameters and indicate a num = 1 or 2 and then that gets added to the output/object names

e.g.

def XXX(s = , e = , num = )

where s will be the batch size, e will be the epochs and num gets placed next to history_num, loss_num and accuracy_num

I come from a SAS background where we would have specified it as history_%eval(&num)

EDIT: if you trying to do like I did, then the following definitely worth a read http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html

Palnatoke
  • 9
  • 3
  • 2
    Creating names dynamically is really bad idea, use proper data structure like list or dict, etc. to store objects. – buran Mar 16 '20 at 18:18
  • Did you try [Gridsearch](https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html) in scikit-learn ? – Nadhir Mar 16 '20 at 18:25
  • @buran completely agree, went that route in the end. Was then just wondering if python can do something similar to SAS. – Palnatoke Mar 19 '20 at 16:52
  • If I understand correctly, accepted answer is not exactly what you want, because names are "hardcoded", not dynamic like your SAS example... Look at this http://stupidpythonideas.blogspot.com/2013/05/why-you-dont-want-to-dynamically-create.html – buran Mar 19 '20 at 17:07
  • basically; but this link you shared is better. 1) it shows how to create the variables dynamically and 2) why you shouldn't do it. Thanks. Nice read – Palnatoke Mar 20 '20 at 19:16

1 Answers1

0

you could use:

def my_func(s, e):

    history = model.fit(x_train, y_train, batch_size=s, epochs=e, verbose=True, validation_split=.1)
    loss, accuracy  = model.evaluate(x_test, y_test, verbose=False) 

    return history, loss, accuracy

history_1, loss_1, accuracy_1 = my_func(32, 10)
history_2, loss_2, accuracy_2 = my_func(60, 10)
kederrac
  • 16,819
  • 6
  • 32
  • 55