I am making a custom convolutional layer by subclassing a Keras Layer. I did this with a previous version of Tensorflow, and received no warnings:
import tensorflow as tf
class MyCustomLayer(tf.Module):
def __init__(self, in_channels,
filters,
kernel_size,
padding,
strides,
activation,
kernel_initializer,
bias_initializer,
use_bias):
super(MyCustomLayer, self).__init__()
self.filters = filters
self.kernel_size = kernel_size
self.activation = activation
self.padding = padding
self.kernel_initializer = kernel_initializer
self.bias_initializer = bias_initializer
self.strides = strides
self.use_bias = use_bias
self.in_channels = in_channels
self.w = tf.Variable(
initial_value=self.kernel_initializer(shape=(*self.kernel_size,
in_channels,
self.filters),
dtype='float32'), trainable=True)
if self.use_bias:
self.b = tf.Variable(
initial_value=self.bias_initializer(shape=(self.filters,),
dtype='float32'),
trainable=True)
def __call__(self, inputs, training=None):
x = tf.nn.conv2d(inputs,
filters=self.w,
strides=self.strides,
padding=self.padding)
if self.use_bias:
x = tf.add(x, self.b)
x = self.activation(x)
return x
x = tf.keras.Input(shape=(28, 28, 3))
y = MyCustomLayer(
in_channels=3,
filters=16,
kernel_size=(3, 3),
strides=(1, 1),
activation=tf.nn.relu,
padding='VALID',
kernel_initializer=tf.initializers.GlorotUniform(),
bias_initializer=tf.initializers.Zeros(),
use_bias=True)(x)
model = tf.keras.Model(inputs=x, outputs=y)
I'm getting this warning, with tf.__version__ == 2.4.1:
WARNING:tensorflow: The following Variables were used a Lambda layer's call (tf.compat.v1.nn.conv2d_12), but are not present in its tracked objects: <tf.Variable 'Variable:0' shape=(3, 3, 3, 16) dtype=float32> It is possible that this is intended behavior, but it is more likely an omission. This is a strong indication that this layer should be formulated as a subclassed Layer rather than a Lambda layer.
WARNING:tensorflow: The following Variables were used a Lambda layer's call (tf.math.add_2), but are not present in its tracked objects: <tf.Variable 'Variable:0' shape=(16,) dtype=float32> It is possible that this is intended behavior, but it is more likely an omission. This is a strong indication that this layer should be formulated as a subclassed Layer rather than a Lambda layer.
What does this mean? I am using a subclassed Layer.