<jupyter_start><jupyter_text>CapsNet Architecture```                            Loss                              ↑                    ┌─────────┴─────────┐      Labels → Margin Loss      Reconstruction Loss                    ↑                   ↑                  Length             Decoder                    ↑                   ↑              Digit Capsules ────Mask────┘               ↖↑↗ ↖↑↗ ↖↑↗             Primary Capsules                    ↑             Convolutional layer                    ↑                     Input Images```<jupyter_code>import numpy as np

import torch
import torch.nn as nn
import torch.nn.functional as F<jupyter_output><empty_output><jupyter_text>Start with some sample input image (28×28 pixels, 1 color channel = grayscale). Like MNIST...- Pytorch Conv Layer input size $(N,C_{in},H,W)$ and output size $(N,C_{out},H_{out},W_{out})$ here, N =  of sample, C =  of channel, H = height, W = width<jupyter_code>input_images = np.random.rand(5, 1, 28, 28)
input_images.shape<jupyter_output><empty_output><jupyter_text>Define the first Convolution layer with parameter mentioned in the paper.<jupyter_code>conv_layer = nn.Conv2d(in_channels=1, out_channels=256, 
                  kernel_size=9, stride=1, padding=0)
conv_layer.weight.data.size()<jupyter_output><empty_output><jupyter_text>Now lets input the images in first conv layer<jupyter_code>conv1(torch.from_numpy(sample_data).float()).size()<jupyter_output><empty_output><jupyter_text>- Calculate output of conv layer $ output \ height = \dfrac{height - kernel\_size + 2 * padding}{stride} + 1$ $ output \ width = \dfrac{width - kernel\_size + 2 * padding}{stride} + 1$<jupyter_code>primary_caps_n_maps = 32
primary_n_caps = primary_caps_n_maps * 6 * 6  # 1152 primary capsules
primary_caps_n_dims = 8
primary_conv = nn.Conv2d(in_channels=256, 
                         out_channels=primary_caps_n_maps * primary_caps_n_dims, 
                         kernel_size=9, stride=2, padding=0)
#primary_conv_out = nn.ReLU(primary_conv(conv1_out))
conv1(torch.from_numpy(sample_data).float()).size()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11542100283822138, 'fraction_numerical': 0.019867549668874173, 'mean_word_length': 2.640275387263339, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '634741', 'license': 'Apache-2.0', 'n_tokens_mistral': 707, 'n_tokens_neox': 658, 'n_words': 146, 'path': 'understanding capsnet.ipynb', 'repo_name': 'sakhawathsumit/CapsNet.pytorch', 'size': '2114'}