Keras Conv1D use_bias='False' doesn't work

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:
print(layer.name)
print(model.get_layer(‘conv1d’).get_weights())

print(model.trainable_variables)
print(model.summary())

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.

@niuyy9026,

Welcome to the Tensorflow Forum!

I have executed the code using Colab and TF 2.10 and it is working as expected.

Model: "model"
_________________________________________________________________
 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.

Thank you!

Hi @ chunduriv,
Thanks for your response. I just found a weird situation. If I import layers like the below:
import keras
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.

@niuyy9026,

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

Thank you!