class_docstr: Reshapes input Tensor, preserving the batch dimension.
  
  For example, given an input tensor with shape ``[B, H, W, C, D]``::
  
      >>> B, H, W, C, D = range(1, 6)
      >>> x = tf.ones([B, H, W, C, D])
  
  The default behavior when ``output_shape`` is ``(-1, D)`` is to flatten
  all dimensions between ``B`` and ``D``::
  
      >>> mod = snt.Reshape(output_shape=(-1, D))
      >>> assert mod(x).shape == [B, H*W*C, D]
  
  You can change the number of preserved leading dimensions via
  ``preserve_dims``::
  
      >>> mod = snt.Reshape(output_shape=(-1, D), preserve_dims=2)
      >>> assert mod(x).shape == [B, H, W*C, D]
  
      >>> mod = snt.Reshape(output_shape=(-1, D), preserve_dims=3)
      >>> assert mod(x).shape == [B, H, W, C, D]
  
      >>> mod = snt.Reshape(output_shape=(-1, D), preserve_dims=4)
      >>> assert mod(x).shape == [B, H, W, C, 1, D]
class_docstr_tok: ["Reshapes","input","Tensor",",","preserving","the","batch","dimension",".","For","example",",","given","an","input","tensor","with","shape","``","[","B",",","H",",","W",",","C",",","D","]","``",":",":",">",">",">","B",",","H",",","W",",","C",",","D","=","range","(","1",",","6",")",">",">",">","x","=","tf.ones","(","[","B",",","H",",","W",",","C",",","D","]",")","The","default","behavior","when","``","output_shape","``","is","``","(","-1",",","D",")","``","is","to","flatten","all","dimensions","between","``","B","``","and","``","D","``",":",":",">",">",">","mod","=","snt.Reshape","(","output_shape=","(","-1",",","D",")",")",">",">",">","assert","mod","(","x",")",".shape","==","[","B",",","H","*","W","*","C",",","D","]","You","can","change","the","number","of","preserved","leading","dimensions","via","``","preserve_dims","``",":",":",">",">",">","mod","=","snt.Reshape","(","output_shape=","(","-1",",","D",")",",","preserve_dims=2",")",">",">",">","assert","mod","(","x",")",".shape","==","[","B",",","H",",","W","*","C",",","D","]",">",">",">","mod","=","snt.Reshape","(","output_shape=","(","-1",",","D",")",",","preserve_dims=3",")",">",">",">","assert","mod","(","x",")",".shape","==","[","B",",","H",",","W",",","C",",","D","]",">",">",">","mod","=","snt.Reshape","(","output_shape=","(","-1",",","D",")",",","preserve_dims=4",")",">",">",">","assert","mod","(","x",")",".shape","==","[","B",",","H",",","W",",","C",",","1",",","D","]"]
docstr: Constructs a ``Reshape`` module.
  
  Args:
    output_shape: Shape to reshape the input tensor to while preserving its
      first ``preserve_dims` dimensions. When the special value -1 appears in
      ``output_shape`` the corresponding size is automatically inferred. Note
      that -1 can only appear once in ``output_shape``.
      To flatten all non-batch dimensions use :class:`Flatten`.
    preserve_dims: Number of leading dimensions that will not be reshaped.
    name: Name of the module.
  
  Raises:
    ValueError: If ``preserve_dims`` is not positive.
docstr_tok: ["Constructs","a","``","Reshape","``","module",".","Args",":","output_shape",":","Shape","to","reshape","the","input","tensor","to","while","preserving","its","first","``","preserve_dims","`","dimensions",".","When","the","special","value","-1","appears","in","``","output_shape","``","the","corresponding","size","is","automatically","inferred",".","Note","that","-1","can","only","appear","once","in","``","output_shape","``",".","To","flatten","all","non-batch","dimensions","use",":","class",":","`","Flatten","`",".","preserve_dims",":","Number","of","leading","dimensions","that","will","not","be","reshaped",".","name",":","Name","of","the","module",".","Raises",":","ValueError",":","If","``","preserve_dims","``","is","not","positive","."]
code: def __init__(
      self,
      output_shape: types.ShapeLike,
      preserve_dims: int = 1,
      name: Optional[str] = None,
  ):
      """Constructs a ``Reshape`` module.
  
      Args:
        output_shape: Shape to reshape the input tensor to while preserving its
          first ``preserve_dims` dimensions. When the special value -1 appears in
          ``output_shape`` the corresponding size is automatically inferred. Note
          that -1 can only appear once in ``output_shape``.
          To flatten all non-batch dimensions use :class:`Flatten`.
        preserve_dims: Number of leading dimensions that will not be reshaped.
        name: Name of the module.
  
      Raises:
        ValueError: If ``preserve_dims`` is not positive.
      """
      super().__init__(name=name)
  
      if preserve_dims <= 0:
          raise ValueError("Argument preserve_dims should be >= 1.")
  
      self._output_shape = output_shape
      self._preserve_dims = preserve_dims
  
file_path: sonnet/sonnet/src/reshape.py
imports: from typing import Optional, Sequence
  import numpy
  from sonnet.src import base
  from sonnet.src import once
  from sonnet.src import types
  import tensorflow
label_desc: Use image node_id 1 to create a new Reshape object from inherited base classes: base with example: obj = Reshape(output_shape, preserve_dims, name)