code: def __init__(
      self,
      input_dim: int,
      layers: Sequence[Sequence[int]],
      direction: str,
      do_weight_norm: bool = True,
      dropout: float = 0.0,
  ) -> None:
      super().__init__()
  
      self.dropout = dropout
      self._convolutions = torch.nn.ModuleList()
      last_dim = input_dim
      for k, layer in enumerate(layers):
          # We run two convolutions for each block -- one for the
          # output and one for the gates -- do them at once, and
          # we'll worry about slicing them in forward
          if len(layer) == 2:
              # no dilation
              conv = torch.nn.Conv1d(
                  last_dim,
                  layer[1] * 2,
                  layer[0],
                  stride=1,
                  padding=layer[0] - 1,
                  bias=True,
              )
          elif len(layer) == 3:
              # a dilation
              assert layer[0] == 2, "only support kernel = 2 for now"
              conv = torch.nn.Conv1d(
                  last_dim,
                  layer[1] * 2,
                  layer[0],
                  stride=1,
                  padding=layer[2],
                  dilation=layer[2],
                  bias=True,
              )
          else:
              raise ValueError("each layer must have length 2 or 3")
  
          # from Convolutional Sequence to Sequence Learning
          if k == 0:
              conv_dropout = dropout
          else:
              # no dropout
              conv_dropout = 0.0
          std = math.sqrt(
              (4 * (1.0 - conv_dropout)) / (layer[0] * last_dim)
          )
  
          conv.weight.data.normal_(0, std=std)
          conv.bias.data.zero_()
  
          if do_weight_norm:
              # conv.weight.shape == (out_channels, in_channels, kernel width)
              # in fairseq, conv.weight.shape == ([width, in, out])
              #   for ConvTBC.  In ConvTBC, weight norm is applied as
              #   nn.utils.weight_norm(m, dim=2) over the output dimension.
              # so for regular 1D convs we need to apply over dimension=0
              conv = torch.nn.utils.weight_norm(
                  conv, name="weight", dim=0
              )
  
          self._convolutions.append(conv)
          last_dim = layer[1]
  
      assert last_dim == input_dim
  
      if direction not in ("forward", "backward"):
          raise ConfigurationError(f"invalid direction: {direction}")
      self._direction = direction
  
file_path: allennlp/allennlp/modules/seq2seq_encoders/gated_cnn_encoder.py
imports: from typing import Sequence, List
  import math
  import torch
  from allennlp.common.checks import ConfigurationError
  from allennlp.modules.seq2seq_encoders.seq2seq_encoder import Seq2SeqEncoder
label_desc: Use image node_id 1 to create a new ResidualBlock object from inherited base classes: torch.nn with example: obj = ResidualBlock(input_dim, layers, direction, do_weight_norm, dropout)