class_docstr: "
  This class implement the second order optimizer - KFAC, which uses Kronecker factor products of inputs and the gradients to
  get the approximate inverse fisher matrix, which is used to update the model parameters. Presently this optimizer works only
  on liner and 2D convolution layers. If you want to know more details about KFAC, please check the paper [1]_ and [2]_.
  
  References:
  -----------
  [1] Martens, James, and Roger Grosse. Optimizing Neural Networks with Kronecker-Factored Approximate Curvature.
  arXiv:1503.05671, arXiv, 7 June 2020. arXiv.org, http://arxiv.org/abs/1503.05671.
  [2] Grosse, Roger, and James Martens. A Kronecker-Factored Approximate Fisher Matrix for Convolution Layers.
  arXiv:1602.01407, arXiv, 23 May 2016. arXiv.org, http://arxiv.org/abs/1602.01407.
class_docstr_tok: ["``","This","class","implement","the","second","order","optimizer","-","KFAC",",","which","uses","Kronecker","factor","products","of","inputs","and","the","gradients","to","get","the","approximate","inverse","fisher","matrix",",","which","is","used","to","update","the","model","parameters",".","Presently","this","optimizer","works","only","on","liner","and","2D","convolution","layers",".","If","you","want","to","know","more","details","about","KFAC",",","please","check","the","paper","[","1","]","_","and","[","2","]","_.","References",":","--","--","--","--","--","-","[","1","]","Martens",",","James",",","and","Roger","Grosse",".","Optimizing","Neural","Networks","with","Kronecker-Factored","Approximate","Curvature",".","arXiv:1503.05671",",","arXiv",",","7","June","2020.","arXiv.org",",","http",":","\/\/arxiv.org\/abs\/1503.05671",".","[","2","]","Grosse",",","Roger",",","and","James","Martens",".","A","Kronecker-Factored","Approximate","Fisher","Matrix","for","Convolution","Layers",".","arXiv:1602.01407",",","arXiv",",","23","May","2016.","arXiv.org",",","http",":","\/\/arxiv.org\/abs\/1602.01407","."]
docstr: Extract patches of a given size from the input tensor given. Used in calculating
  the matrices for the kronecker product in the case of 2d Convolutions.
  
  Parameters:
  -----------
  x: torch.Tensor
      The input feature maps. with the size of (batch_size, in_c, h, w)
  kernel_size: Tuple[int, ...]
      the kernel size of the conv filter.
  stride: Tuple[int, ...]
      the stride of conv operation.
  padding: Union[int, str, Tuple[int, ...]]
      number of paddings. be a tuple of two elements
  
  Return:
  -------
  torch.Tensor:
      Extracted patches with shape (batch_size, out_h, out_w, in_c*kh*kw)
docstr_tok: ["Extract","patches","of","a","given","size","from","the","input","tensor","given",".","Used","in","calculating","the","matrices","for","the","kronecker","product","in","the","case","of","2d","Convolutions",".","Parameters",":","--","--","--","--","--","-","x",":","torch.Tensor","The","input","feature","maps",".","with","the","size","of","(","batch_size",",","in_c",",","h",",","w",")","kernel_size",":","Tuple","[","int",",","...","]","the","kernel","size","of","the","conv","filter",".","stride",":","Tuple","[","int",",","...","]","the","stride","of","conv","operation",".","padding",":","Union","[","int",",","str",",","Tuple","[","int",",","...","]","]","number","of","paddings",".","be","a","tuple","of","two","elements","Return",":","--","--","--","-","torch.Tensor",":","Extracted","patches","with","shape","(","batch_size",",","out_h",",","out_w",",","in_c","*","kh","*","kw",")"]
code: def _extract_patches(
      self,
      x: torch.Tensor,
      kernel_size: Tuple[int, ...],
      stride: Tuple[int, ...],
      padding: Union[int, str, Tuple[int, ...]],
  ) -> torch.Tensor:
      """
      Extract patches of a given size from the input tensor given. Used in calculating
      the matrices for the kronecker product in the case of 2d Convolutions.
  
      Parameters:
      -----------
      x: torch.Tensor
          The input feature maps. with the size of (batch_size, in_c, h, w)
      kernel_size: Tuple[int, ...]
          the kernel size of the conv filter.
      stride: Tuple[int, ...]
          the stride of conv operation.
      padding: Union[int, str, Tuple[int, ...]]
          number of paddings. be a tuple of two elements
  
      Return:
      -------
      torch.Tensor:
          Extracted patches with shape (batch_size, out_h, out_w, in_c*kh*kw)
      """
      if isinstance(padding, tuple):
          if padding[0] + padding[1] > 0:
              x = torch.nn.functional.pad(
                  x, (padding[1], padding[1], padding[0], padding[0])
              ).data  # Actually check dims
      elif isinstance(padding, int):
          if padding > 0:
              x = torch.nn.functional.pad(
                  x, (padding, padding, padding, padding)
              ).data
      elif isinstance(padding, str):
          if padding == "VALID":
              pad = int((kernel_size[0] - 1) / 2)
              x = torch.nn.functional.pad(x, (pad, pad, pad, pad)).data
  
      x = x.unfold(2, kernel_size[0], stride[0])
      x = x.unfold(3, kernel_size[1], stride[1])
      x = x.transpose_(1, 2).transpose_(2, 3).contiguous()
      x = x.view(
          x.size(0),
          x.size(1),
          x.size(2),
          x.size(3) * x.size(4) * x.size(5),
      )
      return x
  
file_path: deepchem/deepchem/models/torch_models/kfac_optimizer.py
imports: import math
  from typing import Optional, Callable, Dict, Tuple, List, Union
label_desc: Use image node_id 3 for calling the KFACOptimizer obj's underlying member method code with example usage: obj._extract_patches(x, kernel_size, stride, padding) and returns: x