You must pass the numpy array to your convolutional layer using the set_weights method.
Remember that the weight of the convolutional layer is not only the weight of each individual filter, but also the offset . Therefore, if you want to set the weight, you need to add an extra dimension.
For example, if you want to install a 1x3x3 filter with all weights of zero, except for the central element, you should do this:
w = np.asarray([ [[[ [0,0,0], [0,2,0], [0,0,0] ]]] ])
And then install it.
For the 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 placeholder (2 in the example).
What the code does:
Build the model first.
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 the original weights (initialized by normal distribution, init = 'normal')
print (model_network.layers[1].get_weights())
Create the 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.] ]] ])
install the scales 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 prediction output (prediction will automatically compile your model)
print(model_network.predict(input_mat))
Result:
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.]]]]