How to initialize a convolution layer with an arbitrary kernel?

2,118 views
Skip to first unread message

alireza.e...@gmail.com

unread,
Jun 20, 2017, 12:56:45 PM6/20/17
to Keras-users
Hello all,

I want to initialize the convolution layer by a specific kernel which is not defined in Keras. For instance, if I define the below function to initialize the kernel:

def init_f(shape):
    ker=np.zeros((shape,shape))
    ker[int(np.floor(shape/2)),int(np.floor(shape/2))]=1
    return ker

And the convolution layer is designed as follows:
model.add(Conv2D(filters=32, kernel_size=(3,3),
                          kernel_initializer=init_f(3)))

I get the error: 'Could not interpret initializer identifier:'. I have followed the similar issue at:
https://groups.google.com/forum/#!topic/keras-users/J46pplO64-8
But I could not adapt it to my code.
Could you please help me to define the arbitrary kernel in Keras?

Thank you in advance.
     

Matias Valdenegro

unread,
Jun 20, 2017, 5:53:07 PM6/20/17
to keras...@googlegroups.com
Hi,

You need to pass a *function*, not a function call, which passes the return
value and then keras of course doesn't "understand" it. The following should
work:

model.add(Conv2D(filters=32, kernel_size=(3,3),
kernel_initializer=init_f))

Alireza Esmaeilzehi

unread,
Jun 20, 2017, 6:24:26 PM6/20/17
to Keras-users
Hi Matias,

Thank you so much for your response. I did the same modification and also set the shape=3 inside the function argument and I got the error: 'tuple' object cannot be interpreted as an index. Also, I changed the function as follows:
def init_f():
    shape=3

    ker=np.zeros((shape,shape))
    ker[int(np.floor(shape/2)),int(np.floor(shape/2))]=1
    return ker
However, I got the error: init_f() takes no arguments (1 given). Could you please correct me if I have made any mistake.

François Chollet

unread,
Jun 20, 2017, 7:14:52 PM6/20/17
to Alireza Esmaeilzehi, Keras-users
You can initialize the layer with the default initializer, then set the weight values manually via `layer.set_weights([numpy_kernel, numpy_bias])`.

--
You received this message because you are subscribed to the Google Groups "Keras-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to keras-users+unsubscribe@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/keras-users/f64fbbc3-df1b-4193-97f5-c18ce773e633%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Alireza Esmaeilzehi

unread,
Jun 21, 2017, 10:03:17 AM6/21/17
to François Chollet, Keras-users
Thanks a lot Francois,

I did the below modifications and it works:

layer0=Conv2D(filters=32, kernel_size=(3,3),
                          kernel_initializer='normal')
model.add(layer0)
layer0.set_weights([w0, np.zeros(32,)])

Now, I am doubtful that is the layer0 with the new weights(w0) added to the model or not? Also I got error, when I transfered 'layer0.set_weights' to the before of 'model.add'.

Thanks

François Chollet

unread,
Jun 21, 2017, 1:01:49 PM6/21/17
to Alireza Esmaeilzehi, Keras-users
Yes, what you did works.

Alireza Esmaeilzehi

unread,
Jun 21, 2017, 3:00:23 PM6/21/17
to François Chollet, Keras-users
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages