Hi, I need to use Conv1d layer without bias.
I am using tensorflow version is 2.10.0 and keras version is 2.10.0
Here is my code:
input_shape =(1, 2)
input_layer = Input(shape = input_shape)
x = Conv1D(2 ,kernel_size=1, padding=‘same’, use_bias=‘False’, bias_initializer=None, activation=None)(input_layer)
output = x
model = Model(inputs=[input_layer], outputs=[output])
for layer in model.layers:
I set use_bias=‘False’, bias_initializer=None for the Conv1D layer.
If you run this code, you would see that the bias is still a trainable variable. And if you check the number of trainable parameters, the number is 6. If bias is not added to the Conv1D layer, the number of trainable parameters should be 4.
Could you help me to check, what’s wrong with this code? Thanks a lot.
Welcome to the Tensorflow Forum!
I have executed the code using Colab and TF 2.10 and it is working as expected.
Layer (type) Output Shape Param #
input_1 (InputLayer) [(None, 1, 2)] 0
conv1d (Conv1D) (None, 1, 2) 4
Total params: 4
Trainable params: 4
Non-trainable params: 0
Please find the gist for reference.
Hi @ chunduriv,
Thanks for your response. I just found a weird situation. If I import layers like the below:
from keras.layers import Input, Conv1D
from keras.models import Model
Then run the code again, it doesn’t work well.
But as long as I import layers like you suggest:
from tensorflow.keras.layers import Input, Conv1d
from tensorflow.keras.models import Model
Then it works well.
Could you have a look at this? Really appreciate it.
To use keras, you must have the Tensorflow package installed, according to Installing Keras.
Once Tensorflow is installed, just import keras as shown below
from tensorflow import keras