class_docstr: Openai transformer, as per https://blog.openai.com/language-unsupervised/.
  Default parameters are the ones for their pretrained model.
  
  Parameters
  ----------
  vocab_size: ``int`` (optional, default: 40990)
      The size of the vocabulary, including the positional encodings.
  n_ctx: ``int`` (optional, default: 512)
      The number of positional encodings.
  embedding_dim: ``int`` (optional, default: 768)
      The dimension of the output embeddings.
  num_heads: ``int`` (optional, default: 12)
      How many "heads" the attention has.
  num_layers: ``int`` (optional, default: 12)
      How many layers of "blocks" the transformer has.
  embedding_dropout_probability: ``float`` (optional, default: 0.1)
      Dropout for the embedding.
  attention_dropout_probability: ``float`` (optional, default: 0.1)
      Dropout for attention.
  residual_dropout_probability: ``float`` (optional, default: 0.1)
      Dropout for residual
  activation_function: ``str`` (optional, default: ``'gelu'``)
      Activation function for the multi-layer perceptron.
  model_path: ``str`` (optional, default: ``None``)
      A tar.gz file containing serialized model weights. If supplied,
      the weights will be loaded from that file.
  requires_grad: ``bool`` (optional, default: ``False``)
      If true, the transformer will be fine-tuneable.
class_docstr_tok: ["Openai","transformer",",","as","per","https",":","\/\/blog.openai.com\/language-unsupervised\/",".","Default","parameters","are","the","ones","for","their","pretrained","model",".","Parameters","--","--","--","--","--","vocab_size",":","``","int","``","(","optional",",","default",":","40990",")","The","size","of","the","vocabulary",",","including","the","positional","encodings",".","n_ctx",":","``","int","``","(","optional",",","default",":","512",")","The","number","of","positional","encodings",".","embedding_dim",":","``","int","``","(","optional",",","default",":","768",")","The","dimension","of","the","output","embeddings",".","num_heads",":","``","int","``","(","optional",",","default",":","12",")","How","many","``","heads","''","the","attention","has",".","num_layers",":","``","int","``","(","optional",",","default",":","12",")","How","many","layers","of","``","blocks","''","the","transformer","has",".","embedding_dropout_probability",":","``","float","``","(","optional",",","default",":","0.1",")","Dropout","for","the","embedding",".","attention_dropout_probability",":","``","float","``","(","optional",",","default",":","0.1",")","Dropout","for","attention",".","residual_dropout_probability",":","``","float","``","(","optional",",","default",":","0.1",")","Dropout","for","residual","activation_function",":","``","str","``","(","optional",",","default",":","``","'gelu","'","``",")","Activation","function","for","the","multi-layer","perceptron",".","model_path",":","``","str","``","(","optional",",","default",":","``","None","``",")","A","tar.gz","file","containing","serialized","model","weights",".","If","supplied",",","the","weights","will","be","loaded","from","that","file",".","requires_grad",":","``","bool","``","(","optional",",","default",":","``","False","``",")","If","true",",","the","transformer","will","be","fine-tuneable","."]
code: def load_weights(
      self,
      transformer_model_path,
      n_ctx=-1,
      n_special=-1,
      n_transfer=12,
      n_embd=768,
      names=_PARAMETER_NAMES,
  ):
      # pylint: disable=dangerous-default-value
  
      logger.info("loading weights from {transformer_model_path}")
      # if `file_path` is a URL, redirect to the cache
      transformer_model_path = cached_path(transformer_model_path)
  
      with tarfile.open(transformer_model_path) as tmp:
          num_params_files = len(
              [
                  member
                  for member in tmp.getmembers()
                  if member.name.endswith(".npy")
              ]
          )
          shapesfile = tmp.extractfile("model/params_shapes.json")
          if shapesfile:
              shapes = json.loads(shapesfile.read())
          else:
              raise ConfigurationError(
                  "unable to find model/params_shapes.json in the archive"
              )
  
          # numpy can't read from a tarfile directly, so we need a workaround
          # https://github.com/numpy/numpy/issues/7989#issuecomment-341656702
          init_params = []
          for n in range(num_params_files):
              array_file = io.BytesIO()
              array_file.write(
                  tmp.extractfile("model/params_{n}.npy").read()
              )
              array_file.seek(0)
              # each np.load is a (11653478,) numpy array
              init_params.append(np.load(array_file))
  
      # init_params is a list of 10 arrays of size (11653578,)
      # shapes are [[512, 768], [40478, 768], [1, 768, 2304], [2304], ...  # 146 elts
      # products are [512 * 768, 40478 * 768, ...]
      # offsets is [512 * 768, 512 * 768 + 40478 * 768, ...]
      offsets = np.cumsum([np.prod(shape) for shape in shapes])
  
      # split into the 146 subarrays corresponding to shapes
      init_params = np.split(np.concatenate(init_params, 0), offsets)[
          :-1
      ]
  
      # reshape
      init_params = [
          param.reshape(shape)
          for param, shape in izip(init_params, shapes)
      ]
  
      # truncate if necessary
      if n_ctx > 0:
          # positional embeddings?
          # init_params[0] is (512, 768) = (max_chars, embedding_dim)
          init_params[0] = init_params[0][:n_ctx]
  
      # combine init_params[1] and init_params[0]
      if n_special > 0:
          # init_params[1] is (40478, 768)
          # special is (n_special, 768)
          # init_params[0] is (512, 768)
          # result is (40990 + n_special, 768)
          init_params[0] = np.concatenate(
              [
                  init_params[1],
                  (np.random.randn(n_special, n_embd) * 0.02).astype(
                      np.float32
                  ),
                  init_params[0],
              ],
              0,
          )
      else:
          # result is (40990, 768)
          init_params[0] = np.concatenate(
              [init_params[1], init_params[0]], 0
          )
      del init_params[1]
  
      # number of dimensions to transfer, 12 per layer, plus one extra
      if n_transfer == -1:
          n_transfer = 0
      else:
          n_transfer = 1 + n_transfer * 12
  
      # squeeze?
      init_params = [arr.squeeze() for arr in init_params]
  
      # embedding.weight is (vocab_size, embedding_dim)
      # make sure init_params[0] has the same shape
      try:
          assert self.embed.weight.shape == init_params[0].shape
      except AssertionError as e:
          e.args += (self.embed.weight.shape, init_params[0].shape)
          raise
  
      # and then assign it
      self.embed.weight.data = torch.from_numpy(init_params[0])
  
      # for each (name, array) pair to transfer over
      for name, ip in izip(
          names[1:n_transfer], init_params[1:n_transfer]
      ):
          # "model/h0/attn/c_attn/w:0"
          name = name[6:]  # "h0/attn/c_attn/w:0"
          assert name[-2:] == ":0"
          name = name[:-2]  # "h0/attn/c_attn/w"
          name_parts = name.split("/")  # ['h0', 'attn', 'c_attn', 'w']
  
          pointer = self
          for m_name in name_parts:
              if re.fullmatch(r"[A-Za-z]+\d+", m_name):
                  l = re.split(r"(\d+)", m_name)  # ['h', '0', '']
              else:
                  l = [m_name]  # ['attn']
              pointer = getattr(pointer, l[0])
              if len(l) >= 2:
                  num = int(l[1])
                  pointer = pointer[num]
          try:
              assert pointer.shape == ip.shape
          except AssertionError as e:
              e.args += (pointer.shape, ip.shape)
              raise
  
          pointer.data = torch.from_numpy(ip)
  
imports: from __future__ import with_statement
  from __future__ import division
  from __future__ import absolute_import
  import copy
  import io
  import json
  import logging
  import math
  import re
  import tarfile
  import numpy
  import torch
  from torch.nn import Parameter
  from allennlp.common.checks import ConfigurationError
  from allennlp.common.file_utils import cached_path
  from allennlp.common.from_params import FromParams
  from io import open
label_desc: Use image node_id 3 for calling the OpenaiTransformer obj's underlying member method code with example usage: obj.load_weights(transformer_model_path, n_ctx, n_special, n_transfer, n_embd, names) without return types