Skip to content

Setting weights in Conv2d #22

@hawbecker

Description

@hawbecker

This tutorial has been tremendously helpful - thank you for creating it! I'm running through notebook 6 and had been getting errors when setting the weights. It seems in this version of Conv2d there is no weights argument.

ValueError: Unrecognized keyword arguments passed to Conv2D: {'weights': [array([[[[ 0]],

        [[-1]],

        [[ 0]]],


       [[[-1]],

        [[ 5]],

        [[-1]]],


       [[[ 0]],

        [[-1]],

        [[ 0]]]])]}

I was able to set the weights by using the following code

kernel_weights = np.array([[ 0, -1, 0],[ -1,5,-1],[0,-1,0]])

# Tensorflow needs to initialize the kernel:
kernel_init = tf.constant_initializer(kernel_weights)

#define conv with specific weights
conv = tf.keras.layers.Conv2D(filters=1,kernel_size=3,input_shape=(260,260,1),
                              kernel_initializer=kernel_init,use_bias=False)

I just wanted to post this here in case anybody else had issues with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions