# Tensor attributes Source: https://www.tensorplay.cn/docs/tensor_attributes.html A tensor carries metadata alongside its data: a data type, a device, and a shape. This page covers the two that describe what an element is and where it lives. Shape is described in [Size](/docs/size.html). ## Data type A tensor’s data type (dtype) states how each element is stored and what range of values it can hold. The common dtypes are exposed as constants: ``` import tensorplay as tp print(tp.float32) # the 32-bit floating-point dtype print(tp.int64) # the 64-bit integer dtype print(tp.bool) # a boolean dtype ``` Pass a dtype to a creation function or Tensor to control it: ``` x = tp.tensor([1, 2, 3], dtype=tp.float32) print(x.dtype) # tensorplay.float32 ``` There are integer, floating-point, and complex dtypes (tensorplay.complex64, tensorplay.complex128). Operations that mix dtypes promote according to TensorPlay’s type-promotion rules, so a float32 tensor times an int64 tensor yields a float32 tensor. | tensorplay.dtype | | | --- | --- | | tensorplay.get_default_dtype | Get the current default floating point tensorplay.dtype. | | tensorplay.set_default_dtype | Sets the default floating point dtype to d. | ## Device A tensor lives on a device — cpu, or a GPU such as cuda. The device determines where the data is physically stored and which kernels can run on it: ``` x = tp.tensor([1.0, 2.0]) print(x.device) # cpu ``` Use .to(device) to move a tensor (and model.to(device) to move a model) to another device. See the [CUDA](/docs/cuda.html) page for working with accelerators. | tensorplay.device | | | --- | --- | | tensorplay.get_default_device | Gets the default tensorplay.Tensor to be allocated on device | | tensorplay.set_default_device | Sets the default tensorplay.Tensor to be allocated on device. | ## Defaults set_default_dtype and set_default_device change the dtype and device used for tensors created without an explicit one, and get_default_dtype / get_default_device read them back. ## Where to go next - [Tensors](/docs/tensorplay.html) — the full tensor creation and math API. - [Type information](/docs/type_info.html) — finfo and iinfo, the per-dtype limits.