TensorPlay
API symbolsset_default_dtype
Copy
View MarkdownDownload .md

tensorplay.set_default_dtype

tensorplay.set_default_dtype(d: DType, /) None[source]

Sets the default floating point dtype to d. Supports floating point dtype as inputs. Other dtypes will cause tensorplay to raise an exception.

When TensorPlay is initialized its default floating point dtype is float32, and the intent of set_default_dtype(float64) is to facilitate NumPy-like type inference. The default floating point dtype is used to:

  1. Implicitly determine the default complex dtype. When the default floating type is float16, the default complex dtype is complex32. For float32, the default complex dtype is complex64. For float64, it is complex128. For bfloat16, an exception will be raised because there is no corresponding complex type for bfloat16.

  2. Infer the dtype for tensors constructed using Python floats or complex Python numbers. See examples below.

  3. Determine the result of type promotion between bool and integer tensors and Python floats and complex Python numbers.

Parameters:

d (tensorplay.dtype) – the floating point dtype to make the default.

Example

>>> # xdoctest: +SKIP("Other tests may have changed the default type. Can we reset it?")
>>> # initial default for floating point is float32
>>> # Python floats are interpreted as float32
>>> tensorplay.tensor([1.2, 3]).dtype
tensorplay.float32
>>> # initial default for floating point is complex64
>>> # Complex Python numbers are interpreted as complex64
>>> tensorplay.tensor([1.2, 3j]).dtype
tensorplay.complex64
>>> tensorplay.set_default_dtype(tensorplay.float64)
>>> # Python floats are now interpreted as float64
>>> tensorplay.tensor([1.2, 3]).dtype  # a new floating point tensor
tensorplay.float64
>>> # Complex Python numbers are now interpreted as complex128
>>> tensorplay.tensor([1.2, 3j]).dtype  # a new complex tensor
tensorplay.complex128
>>> tensorplay.set_default_dtype(tensorplay.float16)
>>> # Python floats are now interpreted as float16
>>> tensorplay.tensor([1.2, 3]).dtype  # a new floating point tensor
tensorplay.float16
>>> # Complex Python numbers are now interpreted as complex128
>>> tensorplay.tensor([1.2, 3j]).dtype  # a new complex tensor
tensorplay.complex32
Ask DeepWiki