|
NEML2 2.0.0
|
Related reading: PyTorch tensor creation API
A neml2::Tensor can be created from a torch::Tensor by marking its batch dimension:
C++ @source:src1
@endsource
Output:
Python @source:src2
@endsource
Output:
A factory tensor creation function produces a new tensor. All factory functions adhere to the same schema:
where <TensorType> is the class name of the primitive tensor type listed here, and <function-name> is the name of the factory function which produces the new tensor. <function-specific-options> are positional arguments a particular factory function accepts. Refer to each tensor type's class documentation for the concrete signature. The last argument const TensorOptions & options configures the data type, device, and other "meta" properties of the produced tensor. The commonly used meta properties are
dtype: the data type of the elements stored in the tensor. Available options are kInt8, kInt16, kInt32, kInt64, kFloat32, and kFloat64. Support for unsigned integer types were added in recent versions of PyTorch.device: the compute device where the tensor will be allocated. Available options are kCPU and kCUDA. On MacOS, the device type torch::kMPS could be used but is not officially supported by NEML2.requires_grad: whether the tensor is part of a function graph used by automatic differentiation to track functional relationship. Available options are true and false.For example, the following code creates a statically (base) shaped, dense, single precision tensor of type SR2 filled with zeros, with batch shape \((5, 3)\), allocated on the CPU.