class_docstr: ## Routing Algorithm
  
  This is the routing mechanism described in the paper.
  You can use multiple routing layers in your models.
  
  This combines calculating $\mathbf{s}_j$ for this layer and
  the routing algorithm described in *Procedure 1*.
class_docstr_tok: ["#","#","Routing","Algorithm","This","is","the","routing","mechanism","described","in","the","paper",".","You","can","use","multiple","routing","layers","in","your","models",".","This","combines","calculating","$","\\mathbf","{","s","}","_j","$","for","this","layer","and","the","routing","algorithm","described","in","*","Procedure","1","*","."]
docstr: `in_caps` is the number of capsules, and `in_d` is the number of features per capsule from the layer below.
  `out_caps` and `out_d` are the same for this layer.
  
  `iterations` is the number of routing iterations, symbolized by $r$ in the paper.
docstr_tok: ["`","in_caps","`","is","the","number","of","capsules",",","and","`","in_d","`","is","the","number","of","features","per","capsule","from","the","layer","below",".","`","out_caps","`","and","`","out_d","`","are","the","same","for","this","layer",".","`","iterations","`","is","the","number","of","routing","iterations",",","symbolized","by","$","r","$","in","the","paper","."]
code: def __init__(
      self,
      in_caps: int,
      out_caps: int,
      in_d: int,
      out_d: int,
      iterations: int,
  ):
      """
      `in_caps` is the number of capsules, and `in_d` is the number of features per capsule from the layer below.
      `out_caps` and `out_d` are the same for this layer.
  
      `iterations` is the number of routing iterations, symbolized by $r$ in the paper.
      """
      super().__init__()
      self.in_caps = in_caps
      self.out_caps = out_caps
      self.iterations = iterations
      self.softmax = nn.Softmax(dim=1)
      self.squash = Squash()
  
      # This is the weight matrix $\mathbf{W}_{ij}$. It maps each capsule in the
      # lower layer to each capsule in this layer
      self.weight = nn.Parameter(
          torch.randn(in_caps, out_caps, in_d, out_d),
          requires_grad=True,
      )
  
file_path: annotated_deep_learning_paper_implementations/labml_nn/capsule_networks/__init__.py
imports: import torch.nn
  import torch.nn.functional
  import torch.utils.data
  from labml_helpers.module import Module
label_desc: Use image node_id 1 to create a new Router object from inherited base classes: Module with example: obj = Router(in_caps, out_caps, in_d, out_d, iterations)