For a structured documentation of the used topologies and parameters when developing a neural network with KERAS I want to use ConfigFiles in JSON format where all relevant information about chosen parameters etc. are stored.
(File excerpt from JSON Config file)
{ "Keras_Model_Informations":{"Activation_functions": ["relu", "relu"],"Optimizer": "Adam","Epochs": 50, etc...} }
These parameters are loaded from the JSON file into Python and are then available as variables.
with open ("Keras_Model_TEST/Dataset_JSON_Config.json",'r') as f: data=json.load(f)Optimizer=data['Keras_Model_Informations']['Optimizer']>>>print(Optimizer)>>>print(type(Optimizer))Adam<class 'str'>
Now my question. When calling the optimizer class I want to specify the corresponding optimizer that implements the Adam algorithm in this example.Normally this call is done as follows (I want to specify the learning rate as well):
model.compile(optimizer=keras.optimizers.Adam(lr=Learning_rate))
In my case I want to pass the optimizer as a variable, because it should be dependent on the corresponding config file. I have already made several attempts (here below an example of an attempt) but so far unfortunately unsuccessfully.
model.compile(optimizer=keras.optimizers.Optimizer(lr=Learning_rate))
I would like to avoid If Statements that execute for each possible algorithm a corresponding line in which a separate assignment is made (as shown below) because the code is already very extensive and I can imagine that there is a more elegant and simpler solution.
if Optimizer == "Adam": model.compile( optimizer=keras.optimizers.Adam(lr=Learning_rate))elif Optimizer == "SGD:" model.compile( optimizer=keras.optimizers.SGD(lr=Learning_rate))etc. ...
Thanks for your answers and your help.