You should pass a numpy array to your convolutional layer through the set_weights method.
Remember that the weights of a convolutional layer are not only the weights of each individual filter, but also the bias. So if you want to set your weights you need to add an extra dimension.
For example, if you want to set a 1x3x3 filter with all weights zero except for the central element, you should make it:
w = np.asarray([
[[[
[0,0,0],
[0,2,0],
[0,0,0]
]]]
])
And then set it.
For a code you could run:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import numpy as np
np.random.seed(1234)
from keras.layers import Input
from keras.layers.convolutional import Convolution2D
from keras.models import Model
print("Building Model...")
inp = Input(shape=(1,None,None))
output = Convolution2D(1, 3, 3, border_mode='same', init='normal',bias=False)(inp)
model_network = Model(input=inp, output=output)
print("Weights before change:")
print (model_network.layers[1].get_weights())
w = np.asarray([
[[[
[0,0,0],
[0,2,0],
[0,0,0]
]]]
])
input_mat = np.asarray([
[[
[1.,2.,3.],
[4.,5.,6.],
[7.,8.,9.]
]]
])
model_network.layers[1].set_weights(w)
print("Weights after change:")
print(model_network.layers[1].get_weights())
print("Input:")
print(input_mat)
print("Output:")
print(model_network.predict(input_mat))
Try changing the central element in the convolutional fillter (2 in the example).
What the code does:
At first build a model.
inp = Input(shape=(1,None,None))
output = Convolution2D(1, 3, 3, border_mode='same', init='normal',bias=False)(inp)
model_network = Model(input=inp, output=output)
Print your original weights (initialized with normal distribution, init='normal' )
print (model_network.layers[1].get_weights())
Create your desired weight tensor w and some input input_mat
w = np.asarray([
[[[
[0,0,0],
[0,2,0],
[0,0,0]
]]]
])
input_mat = np.asarray([
[[
[1.,2.,3.],
[4.,5.,6.],
[7.,8.,9.]
]]
])
set your weights and print them
model_network.layers[1].set_weights(w)
print("Weights after change:")
print(model_network.layers[1].get_weights())
Finally, use it to generate output with predict (predict automatically compiles your model)
print(model_network.predict(input_mat))
Example Output:
Using Theano backend.
Building Model...
Weights before change:
[array([[[[ 0.02357176, -0.05954878, 0.07163535],
[-0.01563259, -0.03602944, 0.04435815],
[ 0.04297942, -0.03182618, 0.00078482]]]], dtype=float32)]
Weights after change:
[array([[[[ 0., 0., 0.],
[ 0., 2., 0.],
[ 0., 0., 0.]]]], dtype=float32)]
Input:
[[[[ 1. 2. 3.]
[ 4. 5. 6.]
[ 7. 8. 9.]]]]
Output:
[[[[ 2. 4. 6.]
[ 8. 10. 12.]
[ 14. 16. 18.]]]]