Kiran Baktha
Kiran Baktha

Reputation: 667

How to change Keras optimizer code

I am really new to Keras so forgive me if my query is a bit silly. I installed Keras in my system using the default methods and it works fine. I want to add a new optimizer to Keras so that I can easily mention "optimizer = mynewone " under the model.compile function. How do I go about changing the " optimizer.py " code in Keras and ensuring that the change is reflected on 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 "rmsprop" keywords with "rmsprops" and saved the file. So I thought I must have the updated optimizers.py in my system. But when I go back to my original file and run model.compile it throws the same error.

Any help would be really appreciated. Thanks in advance.

Upvotes: 6

Views: 6251

Answers (2)

Dr. Snoopy
Dr. Snoopy

Reputation: 56367

I think your approach is complicated and it doesn't have to be. Let's say you implement your own optimizer by subclassing keras.optimizers.Optimizer:

class MyOptimizer(Optimizer):
    optimizer functions here.

Then to instantiate it 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 the optimizer parameter of model.compile and that's it, Keras will now use your optimizer.

Upvotes: 2

Nassim Ben
Nassim Ben

Reputation: 11553

Are you sure that it is a new optimizer that you want? Not a custom objective function? Objectives can be custom it's easy to define, optimizers are trickier.

There is already a huge number of optimizers with a lot of parameters. However if you really want to go down that road I would advise you to go to tensorflow! Then you will be able to use this in Keras

It's all I can do for you, but maybe there is another way that I don't know of.

Upvotes: 0

Related Questions