YouTip LogoYouTip

Pytorch Torch Nn Convtranspose2D

[![Image 1: PyTorch torch.nn Reference Manual](https://example.com/images/up.gif) PyTorch torch.nn Reference Manual](https://example.com/pytorch/pytorch-torch-nn-ref.html)\\n\\n* * *\\n\\n`torch.nn.ConvTranspose2d` is a 2D transposed convolution in PyTorch, also known as deconvolution or upsampling convolution.\\n\\nIt is used to upsample feature maps and is a key component in generation networks and segmentation networks.\\n\\n### Function Definition\\n\\ntorch.nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride=1, padding=0, output_padding=0, groups=1, bias=True, dilation=1)\\n### Parameters\\n\\n* `output_padding`: Additional output padding\\n\\n* * *\\n\\n## Usage Examples\\n\\n### Example 1: Basic Usage\\n\\n## Instance\\n\\nimport torch\\n\\nimport torch.nn as nn\\n\\n# Transposed Convolution: Upsampling\\n\\n deconv = nn.ConvTranspose2d(in_channels=64, out_channels=64, kernel_size=2, stride=2)\\n\\nx = torch.randn(1,64,16,16)\\n\\n output = deconv(x)\\n\\nprint("Input:", x.shape,"-> Output:", output.shape)\\n\\n### Example 2: Generation Network\\n\\n## Instance\\n\\nimport torch\\n\\nimport torch.nn as nn\\n\\n# Simplified DCGAN Generator\\n\\nclass Generator(nn.Module):\\n\\ndef __init__ (self, latent_dim=100):\\n\\nsuper(Generator,self). __init__ ()\\n\\nself.fc= nn.Linear(latent_dim,512 * 4 * 4)\\n\\nself.deconv= nn.Sequential(\\n\\n nn.ConvTranspose2d(512,256,4,2,1),# 4->8\\n\\n nn.BatchNorm2d(256),\\n\\n nn.ReLU(),\\n\\n nn.ConvTranspose2d(256,128,4,2,1),# 8->16\\n\\n nn.BatchNorm2d(128),\\n\\n nn.ReLU(),\\n\\n nn.ConvTranspose2d(128,64,4,2,1),# 16->32\\n\\n nn.BatchNorm2d(64),\\n\\n nn.ReLU(),\\n\\n nn.ConvTranspose2d(64,3,4,2,1),# 32->64\\n\\n nn.Tanh()\\n\\n)\\n\\ndef forward(self, x):\\n\\n x =self.fc(x).view(-1,512,4,4)\\n\\nreturn self.deconv(x)\\n\\ngen = Generator()\\n\\n z = torch.randn(1,100)\\n\\n img = gen(z)\\n\\nprint("Noise:", z.shape,"-> Images:", img.shape)\\n\\n### Example 3: Segmentation Network Upsampling\\n\\n## Instance\\n\\nimport torch\\n\\nimport torch.nn as nn\\n\\n# U-Net Decoder Section\\n\\n decoder = nn.Sequential(\\n\\n nn.ConvTranspose2d(256,128, kernel_size=2, stride=2),\\n\\n nn.Conv2d(128,128,3, padding=1),\\n\\n nn.ReLU(),\\n\\n nn.ConvTranspose2d(128,64, kernel_size=2, stride=2),\\n\\n nn.Conv2d(64,64,3, padding=1),\\n\\n nn.ReLU()\\n\\n)\\n\\nx = torch.randn(1,256,8,8)\\n\\n output = decoder(x)\\n\\nprint("Input:", x.shape,"-> Output:", output.shape)\\n\\n### Example 4: Output Calculation for stride=2\\n\\n## Instance\\n\\nimport torch\\n\\nimport torch.nn as nn\\n\\n# Different Configurations\\n\\n configs =[\\n\\n(1,1,0),# stride=1, padding=0\\n\\n(2,1,0),# stride=2, padding=0\\n\\n(2,1,1),# stride=2, padding=1\\n\\n]\\n\\nx = torch.randn(1,64,4,4)\\n\\nfor stride, k, p in configs:\\n\\n deconv = nn.ConvTranspose2d(64,64, kernel_size=k, stride=stride, padding=p)\\n\\n out = deconv(x)\\n\\nprint(f"k={k}, s={stride}, p={p}: {x.shape} -> {out.shape}")\\n\\n* * *\\n\\n## Usage Scenarios\\n\\n* **Generation Networks**: GAN, VAE\\n* **Semantic Segmentation**: U-Net\\n* **Upsampling**: Alternative to pooling\\n\\n> Note: Transposed convolution is not the inverse operation of convolution, but just a method of upsampling.\\n\\n* * *\\n\\n[![Image 2: PyTorch torch.nn Reference Manual](https://example.com/images/up.gif) PyTorch torch.nn Reference Manual](https://example.com/pytorch/pytorch-torch-nn-ref.html)
← Pytorch Torch Nn DropoutPytorch Torch Nn Bilinear β†’