mavaras
mavaras

Reputation: 63

Translate Conv2D from PyTorch code to Tensorflow

I have the following PyTorch layer definition:

self.e_conv_1 = nn.Sequential(
    nn.ZeroPad2d((1, 2, 1, 2)),
    nn.Conv2d(in_channels=3, out_channels=64, kernel_size=(5, 5), stride=(2, 2)),
    nn.LeakyReLU(),
)

I want to have the same exact layer declaration in Tensorflow. How can I get to that?

self.e_conv_1 = tf.keras.Sequential([
  layers.Conv2D(64, kernel_size=(5, 5), activation=partial(tf.nn.leaky_relu, alpha=0.01), padding='same', strides=(1, 2))
])

Should it be something like this code above? I think that at least strides and padding isn't the same.

Thanks in advance to anyone who helps.

Upvotes: 0

Views: 2018

Answers (1)

Milad Yousefi
Milad Yousefi

Reputation: 354

I think you can use layers in this way according to tenssorflow documentation:

tf.keras.Sequential([
    layers.ZeroPadding2D(padding=((1,2), (1,2)))
    layers.Conv2D(64, kernel_size=(5, 5), activation=partial(tf.nn.leaky_relu, 
    alpha=0.01), padding='valid', strides=(2, 2))
])

the main difference is between torch zero padding and tensroflow zero padding arguments. in torch padding arguments are:

m = nn.ZeroPad2d((left, right, top, bottom))

in tensorflow:

tf.keras.layers.ZeroPadding2D(padding=((top,bottom),(left,right)))

Upvotes: 1

Related Questions