Tensorflow implementation of Pytorch code: adding convolutional layers
I'd like to implement this PyTorch code in Tensorflow, but am a newbie, and am looking for some assistance/resources.
The code in Pytorch combines two convolutions in forward propagation:
class PytorchLayer(nn.Module):
    def __init__(self, in_features, out_features):
        super(PytorchLayer, self).__init__()
        self.in_features = in_features
        self.out_features = out_features
        self.layer1 = nn.Conv1d(in_features, out_features, 1)
        self.layer2 = nn.Conv1d(in_features, out_features, 1, bias=False)

    def forward(self, x):
        return self.layer1(x) + self.layer2(x - x.mean(dim=2, keepdim=True))

How can I do this in tensorflow?
I understand that I can do a 1D Convolution likeso:
tf.keras.layers.Conv1D(in_features, kernel_size = 1, strides=1)


I also understand that I can create a feedforward network like so:
tf.keras.Sequential([tf.keras.layers.Conv1D(in_features, kernel_size = 1, strides=1)])

However, in tensorflow, how do I implement this line from the Pytorch code, that transforms the convolutions:
self.layer1(x) + self.layer2(x - x.mean(dim=2, keepdim=True))

Apologies for the amateur question. I searched for a long time, but couldn't see a similar post to mine.

You may find the Keras tutorials:

The Functional API
Making new layers and models via subclassing

informative for this task. Using the Keras functional model API, this might look something like:
out_features = 5  # Arbitrary for the example

layer1 = tf.keras.layers.Conv1D(
      out_features, kernel_size=1, strides=1, name='Conv1')
layer2 = tf.keras.layers.Conv1D(
      out_features, kernel_size=1, strides=1, use_bias=False, name='Conv2')
subtract = tf.keras.layers.Subtract(name='SubtractMean')
mean = tf.keras.layers.Lambda(
      lambda t: tf.reduce_mean(t, axis=2, keepdims=True), name='Mean')

# Connect the layers in a model.
x = tf.keras.Input(shape=(5,5))
average_x = mean(x)
normalized_x = subtract([x, average_x])
y = tf.keras.layers.Add(name='AddConvolutions')([layer1(x), layer2(normalized_x)])

m = tf.keras.Model(inputs=x, outputs=y)
m.summary()

&gt;&gt;&gt; Model: &quot;model&quot;
&gt;&gt;&gt; __________________________________________________________________________________________________
&gt;&gt;&gt;  Layer (type)                   Output Shape         Param #     Connected to                     
&gt;&gt;&gt; ==================================================================================================
&gt;&gt;&gt;  input_1 (InputLayer)           [(None, 5, 5)]       0           []                               
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt;  Mean (Lambda)                  (None, 5, 1)         0           ['input_1[0][0]']                
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt;  SubtractMean (Subtract)        (None, 5, 5)         0           ['input_1[0][0]',                
&gt;&gt;&gt;                                                                   'Mean[0][0]']                   
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt;  Conv1 (Conv1D)                 (None, 5, 5)         30          ['input_1[0][0]']                
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt;  Conv2 (Conv1D)                 (None, 5, 5)         25          ['SubtractMean[0][0]']           
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt;  AddConvolutions (Add)          (None, 5, 5)         0           ['Conv1[0][0]',                  
&gt;&gt;&gt;                                                                   'Conv2[0][0]']                  
&gt;&gt;&gt;                                                                                                   
&gt;&gt;&gt; ==================================================================================================
&gt;&gt;&gt; Total params: 55
&gt;&gt;&gt; Trainable params: 55
&gt;&gt;&gt; Non-trainable params: 0
&gt;&gt;&gt; __________________________________________________________________________________________________

