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.
CodePudding user response:
You may find the Keras tutorials:
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()
>>> Model: "model"
>>> __________________________________________________________________________________________________
>>> Layer (type) Output Shape Param # Connected to
>>> ==================================================================================================
>>> input_1 (InputLayer) [(None, 5, 5)] 0 []
>>>
>>> Mean (Lambda) (None, 5, 1) 0 ['input_1[0][0]']
>>>
>>> SubtractMean (Subtract) (None, 5, 5) 0 ['input_1[0][0]',
>>> 'Mean[0][0]']
>>>
>>> Conv1 (Conv1D) (None, 5, 5) 30 ['input_1[0][0]']
>>>
>>> Conv2 (Conv1D) (None, 5, 5) 25 ['SubtractMean[0][0]']
>>>
>>> AddConvolutions (Add) (None, 5, 5) 0 ['Conv1[0][0]',
>>> 'Conv2[0][0]']
>>>
>>> ==================================================================================================
>>> Total params: 55
>>> Trainable params: 55
>>> Non-trainable params: 0
>>> __________________________________________________________________________________________________