Skip to content

WARNING

该页面尚未翻译。 以下内容为英文原版。

tensorplay.nn.parameter

Classes

class Buffer [source]

python
Buffer(data=None, persistent=True)

Bases: TensorBase

A kind of Tensor that should not be considered a model parameter. For example, BatchNorm's running_mean is not a parameter, but is part of the module's state.

Buffers are ~tensorplay.Tensor subclasses, that have a very special property when used with Module s -- when they're assigned as Module attributes they are automatically added to the list of its buffers, and will appear e.g. in ~tensorplay.nn.Module.buffers iterator. Assigning a Tensor doesn't have such effect. One can still assign a Tensor as explicitly by using the ~tensorplay.nn.Module.register_buffer function.

Args

  • data (Tensor): buffer tensor.
  • persistent (bool, optional): whether the buffer is part of the module's state_dict. Default: True
Methods

__init__(self, data=None, persistent=True) [source]

init(self) -> None init(self, data: object, dtype: tensorplay.DType | None = None, device: tensorplay.Device | None = None, requires_grad: bool = False) -> None


cpu(self) [source]

Returns a copy of this object in CPU memory. If this object is already in CPU memory, then no copy is performed and the original object is returned.


cuda(self, device=None, non_blocking=False) [source]

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.


double(self) [source]


flatten(self, start_dim=0, end_dim=-1) [source]

Flattens a contiguous range of dims.


float(self) [source]


int(self) [source]


is_float(self) -> bool [source]

Check if tensor is floating point.


long(self) [source]


ndimension(self) -> int [source]

Alias for dim()


t(self) [source]

Returns the transpose of the tensor. Aliased to transpose(0, 1) to ensure correct autograd behavior (TransposeBackward).


unflatten(self, dim, sizes) [source]

Expands a dimension of the input tensor over multiple dimensions.


class Parameter [source]

python
Parameter(data=None, requires_grad=True)

Bases: TensorBase

A kind of Tensor that is to be considered a module parameter.

Parameters are ~tensorplay.Tensor subclasses, that have a very special property when used with Module s - when they're assigned as Module attributes they are automatically added to the list of its parameters, and will appear e.g. in ~Module.parameters iterator. Assigning a Tensor doesn't have such effect. This is because one might want to cache some temporary state, like last hidden state of the RNN, in the model. If there was no such class as Parameter, these temporaries would get registered too.

Args

  • data (Tensor): parameter tensor.
  • requires_grad (bool, optional): if the parameter requires gradient. Note that the tensorplay.no_grad() context does NOT affect the default behavior of Parameter creation--the Parameter will always have requires_grad=True unless given explicitly. Default: True
Methods

__init__(self, data=None, requires_grad=True) [source]

init(self) -> None init(self, data: object, dtype: tensorplay.DType | None = None, device: tensorplay.Device | None = None, requires_grad: bool = False) -> None


cpu(self) [source]

Returns a copy of this object in CPU memory. If this object is already in CPU memory, then no copy is performed and the original object is returned.


cuda(self, device=None, non_blocking=False) [source]

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.


double(self) [source]


flatten(self, start_dim=0, end_dim=-1) [source]

Flattens a contiguous range of dims.


float(self) [source]


int(self) [source]


is_float(self) -> bool [source]

Check if tensor is floating point.


long(self) [source]


ndimension(self) -> int [source]

Alias for dim()


t(self) [source]

Returns the transpose of the tensor. Aliased to transpose(0, 1) to ensure correct autograd behavior (TransposeBackward).


unflatten(self, dim, sizes) [source]

Expands a dimension of the input tensor over multiple dimensions.


class UninitializedBuffer [source]

python
UninitializedBuffer(requires_grad=False, device=None, dtype=None, persistent=True)

Bases: TensorBase

A buffer that is not initialized.

Uninitialized Buffer is a a special case of tensorplay.Tensor where the shape of the data is still unknown.

Unlike a tensorplay.Tensor, uninitialized parameters hold no data and attempting to access some properties, like their shape, will throw a runtime error. The only operations that can be performed on a uninitialized parameter are changing its datatype, moving it to a different device and converting it to a regular tensorplay.Tensor.

The default device or dtype to use when the buffer is materialized can be set during construction using e.g. device='cuda'.

Methods

__init__(self, requires_grad=False, device=None, dtype=None, persistent=True) [source]

init(self) -> None init(self, data: object, dtype: tensorplay.DType | None = None, device: tensorplay.Device | None = None, requires_grad: bool = False) -> None


cpu(self) [source]

Returns a copy of this object in CPU memory. If this object is already in CPU memory, then no copy is performed and the original object is returned.


cuda(self, device=None, non_blocking=False) [source]

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.


double(self) [source]


flatten(self, start_dim=0, end_dim=-1) [source]

Flattens a contiguous range of dims.


float(self) [source]


int(self) [source]


is_float(self) -> bool [source]

Check if tensor is floating point.


long(self) [source]


materialize(self, shape, device=None, dtype=None) [source]


ndimension(self) -> int [source]

Alias for dim()


share_memory_(self) [source]

share_memory_(self) -> object


t(self) [source]

Returns the transpose of the tensor. Aliased to transpose(0, 1) to ensure correct autograd behavior (TransposeBackward).


unflatten(self, dim, sizes) [source]

Expands a dimension of the input tensor over multiple dimensions.


class UninitializedParameter [source]

python
UninitializedParameter(requires_grad=True, device=None, dtype=None)

Bases: Parameter

A parameter that is not initialized.

Uninitialized Parameters are a special case of tensorplay.nn.Parameter where the shape of the data is still unknown.

Unlike a tensorplay.nn.Parameter, uninitialized parameters hold no data and attempting to access some properties, like their shape, will throw a runtime error. The only operations that can be performed on a uninitialized parameter are changing its datatype, moving it to a different device and converting it to a regular tensorplay.nn.Parameter.

The default device or dtype to use when the parameter is materialized can be set during construction using e.g. device='cuda'.

Methods

__init__(self, requires_grad=True, device=None, dtype=None) [source]

init(self) -> None init(self, data: object, dtype: tensorplay.DType | None = None, device: tensorplay.Device | None = None, requires_grad: bool = False) -> None


cpu(self) [source]

Returns a copy of this object in CPU memory. If this object is already in CPU memory, then no copy is performed and the original object is returned.


cuda(self, device=None, non_blocking=False) [source]

Returns a copy of this object in CUDA memory. If this object is already in CUDA memory and on the correct device, then no copy is performed and the original object is returned.


double(self) [source]


flatten(self, start_dim=0, end_dim=-1) [source]

Flattens a contiguous range of dims.


float(self) [source]


int(self) [source]


is_float(self) -> bool [source]

Check if tensor is floating point.


long(self) [source]


materialize(self, shape, device=None, dtype=None) [source]


ndimension(self) -> int [source]

Alias for dim()


share_memory_(self) [source]

share_memory_(self) -> object


t(self) [source]

Returns the transpose of the tensor. Aliased to transpose(0, 1) to ensure correct autograd behavior (TransposeBackward).


unflatten(self, dim, sizes) [source]

Expands a dimension of the input tensor over multiple dimensions.


class UninitializedTensorMixin [source]

python
UninitializedTensorMixin()
Methods

materialize(self, shape, device=None, dtype=None) [source]

Create a Parameter or Tensor with the same properties of the uninitialized one.

Given a shape, it materializes a parameter in the same device and with the same dtype as the current one or the specified ones in the arguments.

Args

shape : (tuple): the shape for the materialized tensor.
  • device (tensorplay.device): the desired device of the parameters and buffers in this module. Optional.
  • dtype (tensorplay.dtype): the desired floating point type of the floating point parameters and buffers in this module. Optional.

share_memory_(self) [source]


Functions

is_lazy() [source]

python
is_lazy(param: Any) -> bool

Returns whether param is an UninitializedParameter or UninitializedBuffer.

Args

  • param (Any): the input to check.

基于 Apache 2.0 许可发布。

📚DeepWiki