WAF_ML_Tutorial_Part2 icon indicating copy to clipboard operation
WAF_ML_Tutorial_Part2 copied to clipboard

Setting weights in Conv2d

Open hawbecker opened this issue 1 year ago • 0 comments

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.

hawbecker avatar Mar 25 '24 15:03 hawbecker