Skip to content

Instantly share code, notes, and snippets.

@sicongzhao
Last active April 25, 2020 05:18
Show Gist options
  • Save sicongzhao/9e998b5138376550d7d5ae3662574704 to your computer and use it in GitHub Desktop.
Save sicongzhao/9e998b5138376550d7d5ae3662574704 to your computer and use it in GitHub Desktop.
Demystify Transposed Convolutional Layers
import torch
from torch import nn
# Create 2x2 input tensor
input_data = torch.tensor([[[[1.,2.],[3.,4.]]]])
# Create a TCL layer with kernel_size=3
transConv1 = nn.ConvTranspose2d(1, 1, 3, bias=False)
# Set kernel weights to be 1
transConv1.weight.data = torch.ones(1,1,3,3)
# Calculate the output
transConv1(input_data)
# Output
# tensor([[[[ 1., 3., 3., 2.],
# [ 4., 10., 10., 6.],
# [ 4., 10., 10., 6.],
# [ 3., 7., 7., 4.]]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment