How to change Keras optimizer code

I'm really new to Keras, so forgive me if my request is a little stupid. I installed Keras on my system using the default methods and it works fine. I want to add a new optimizer in Keras so that I can easily specify "optimizer = mynewone" in the model.compile function. How can I change the optimizer.py code in Keras and make sure that this change is reflected in my Keras environment. Here is what I tried:

Suppose I change the optimizer name from rmsprop to rmsprops in the code, I get the following error:

model.compile(loss='binary_crossentropy', optimizer='rmsprops', metrics= ['accuracy']) Traceback (most recent call last): File "<ipython-input-33-40773d534448>", line 1, in <module> model.compile(loss='binary_crossentropy', optimizer='rmsprops', metrics=['accuracy']) File "/home/kiran/anaconda/lib/python3.5/site-packages/keras/models.py", line 589, in compile **kwargs) File "/home/kiran/anaconda/lib/python3.5/site-packages/keras/engine/training.py", line 469, in compile self.optimizer = optimizers.get(optimizer) File "/home/kiran/anaconda/lib/python3.5/site-packages/keras/optimizers.py", line 614, in get # Instantiate a Keras optimizer File "/home/kiran/anaconda/lib/python3.5/site-packages/keras/utils/generic_utils.py", line 16, in get_from_module str(identifier)) ValueError: Invalid optimizer: rmsprops 

Then, when I click on optimizers.py, I get the code developed by Keras in my environment. After that, in the code, I replaced all the keywords "rmsprop" with "rmsprops" and saved the file. So I thought I should update optimizer.py on my system. But when I go back to the original file and run the model.compile file, it gives the same error.

Any help would be really appreciated. Thanks in advance.

+5
source share
2 answers

I think your approach is complicated, and this is not necessary. Let's say you implement your own optimizer by subclassing keras.optimizers.Optimizer:

 class MyOptimizer(Optimizer): optimizer functions here. 

Then, to create an instance in your model, you can do this:

 myOpt = MyOptimizer() model.compile(loss='binary_crossentropy', optimizer=myOpt, metrics= ['accuracy']) 

Just pass an instance of your optimizer as an optimizer parameter to model.compile, and that it, Keras will now use your optimizer.

+1
source

Are you sure this is the new optimizer you need? Not a custom objective function? Goals can be easily customizable, optimizers more difficult.

Already there are a huge number of optimizers with many parameters. However, if you really want to go down this road, I would advise you to go to the tensor! Then you can use it in Keras

All that I can do for you, but maybe there is another way that I do not know about.

0
source

Source: https://habr.com/ru/post/1264410/


All Articles