Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This may be a basic question, but what is the recommended way to use Keras when I need to do some modifications to it? #374

Closed
KyotoSunshine opened this issue Jul 10, 2015 · 5 comments

Comments

@KyotoSunshine
Copy link
Contributor

For example, suppose I want to use keras, but I want to try a different weight initialization method, which is not included in keras. How am I supposed to use Keras in this case (without modifying the source code of keras itself) ?

@mthrok
Copy link
Contributor

mthrok commented Jul 10, 2015

You can define your custom function as Theano function and pass it to model.

See my answer in #369 for objective function case. I think it's same for initialization function.

@KyotoSunshine
Copy link
Contributor Author

Is it really same? Here is the code that gets executed on the value of init parameter:

    self.init = initializations.get(init)

It seems that the value must be a string rather than a function, isn't it?

@KyotoSunshine
Copy link
Contributor Author

I did it like the following, please let me know if there is a better method!

def custom_initialization(shape):
    fan_in, fan_out = keras.initializations.get_fans(shape)
    return keras.utils.theano_utils.sharedX(myfunction(shape, fan_in,_fan_out))

keras.initializations.custom_initialization = custom_initialization
...
model.add(Dense(784, 128, init='custom_initialization'))

@mthrok
Copy link
Contributor

mthrok commented Jul 10, 2015

Did you see the inside of the initializations.get ? https://github.com/fchollet/keras/blob/master/keras/utils/generic_utils.py#L7
It checks if the given argument is string and, if it's string, it looks for a pre-defined function, and if not, look for global variable table and use the identifier you give.

So this works and I verified it.

import theano
import theano.tensor as T
from keras.utils.theano_utils import sharedX

def custom_initialization(shape, scale=0.05):
    '''just another normal'''
    return sharedX(np.random.randn(*shape) * scale)

model = Sequential()
model.add(Convolution2D(32, 1, 3, 3, border_mode='full', init=custom_initialization))

@KyotoSunshine
Copy link
Contributor Author

Ah, I see. Looks very nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants