Skip to content

dtypes

default_dtype

default_dtype(dtype: dtype) -> collections.abc.Generator[None]

Context manager to temporarily change the default dtype of torch.

Parameters:

Name Type Description Default
dtype dtype

The dtype to temporarily set as the default. All dtypes supported by torch.set_default_dtype are valid.

required

Yields:

Type Description
collections.abc.Generator[None]

None.

Examples:

New tensors created within the context will have the new default dtype (unless explicitly overridden by the dtype argument).

>>> torch.tensor([1.0]).dtype
torch.float32
>>> with default_dtype(torch.float64):
...     torch.tensor([1.0]).dtype
torch.float64

The default dtype is restored after the context exits.

>>> torch.get_default_dtype()
torch.float32
>>> with default_dtype(torch.float64):
...     torch.get_default_dtype()
torch.float64
>>> torch.get_default_dtype()
torch.float32