Search code examples
pythonpython-2.7tensorflowconvolution

Change filter on convolutional layer CNN - Python/TensorFlow


I have the following code block:

def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

And:

def new_conv_layer(input,              # The previous layer
                   use_pooling=True):  # Use 2x2 max-pooling

    shape = [3, 3, 1, 8]

    weights = new_weights(shape=shape)

    biases = new_biases(length=8)

    layer = tf.nn.conv2d(input=input,
                         filter=weights,
                         strides=[1, 1, 1, 1],
                         padding='SAME')

    layer += biases

    if use_pooling:
        layer = tf.nn.max_pool(value=layer,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    layer = tf.nn.relu(layer)

    # relu(max_pool(x)) == max_pool(relu(x)) we can
    # save 75% of the relu-operations by max-pooling first.

    return layer

So we can observe that the size of the filter is 3x3 and the number of filters is 8. And the filters are defined with random values.

What I need to do is to define all my 8 filters with fixed values, ie predetermined values, for example:

weigths = [
    [[0,  1, 0,],[0, -1, 0,],[0,  0, 0,],],
    [[0,  0, 1,],[0, -1, 0,],[0,  0, 0,],],
    [[0,  0, 0,],[0, -1, 1,],[0,  0, 0,],],
    [[0,  0, 0,],[0, -1, 0,],[0,  0, 1,],],
    [[0,  0, 0,],[0, -1, 0,],[0,  1, 0,],],
    [[0,  0, 0,],[0, -1, 0,],[1,  0, 0,],], 
    [[0,  0, 0,],[1, -1, 0,],[0,  0, 0,],],
    [[1,  0, 0,],[0, -1, 0,],[0,  0, 0,],]
]

I can not imagine, how could I do inside my code, this modification, does anyone have any idea how I could do this?

Thank you very much in advance!


Solution

  • If you want initialize weight by some predefined value you can use tf.constant_initializer. If you don't want train this weights, you can define them as tf.constant not tf.Variable

    def new_weights(init_vaue, is_const):
        if (is_const) :
            return tf.constant(init_vaue, name='weights')
        else:
            initializer = tf.constant_initializer(init_vaue)
            return tf.get_variable('weights', shape = init_vaue.shape, initializer=initializer)
    
    weights = np.ones([3,3,1,8], dtype=np.float)
    print(weights.shape)
    
    value = new_weights(weights, True)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        value_ = sess.run(value) 
        print(value_)