# tensorplay.as_tensor Source: https://www.tensorplay.cn/docs/generated/tensorplay.as_tensor.html ```python tensorplay.as_tensor(data: Any, dtype: Optional[dtype] = None, device: Optional[DeviceLikeType]) → Tensor ``` Converts [data](/docs/generated/tensorplay.data.html#tensorplay.data) into a tensor, sharing data and preserving autograd history if possible. If [data](/docs/generated/tensorplay.data.html#tensorplay.data) is already a tensor with the requested dtype and device then [data](/docs/generated/tensorplay.data.html#tensorplay.data) itself is returned, but if [data](/docs/generated/tensorplay.data.html#tensorplay.data) is a tensor with a different dtype or device then it’s copied as if using data.to(dtype=dtype, device=device). If [data](/docs/generated/tensorplay.data.html#tensorplay.data) is a NumPy array (an ndarray) with the same dtype and device then a tensor is constructed using [tensorplay.from_numpy()](/docs/generated/tensorplay.from_numpy.html#tensorplay.from_numpy). If [data](/docs/generated/tensorplay.data.html#tensorplay.data) is a CuPy array, the returned tensor will be located on the same device as the CuPy array unless specifically overwritten by [device](/docs/generated/tensorplay.Device.html#tensorplay.Device) or a default device. The device of the CuPy array is inferred from the pointer of the array using cudaPointerGetAttributes unless [device](/docs/generated/tensorplay.Device.html#tensorplay.Device) is provided with an explicit device index. > **See also** > > tensorplay.tensor() never shares its data and creates a new “leaf tensor” (see Autograd mechanics). Parameters: - data (array_like) – Initial data for the tensor. Can be a list, tuple, NumPy ndarray, scalar, and other types. - dtype ([tensorplay.dtype](/docs/generated/tensorplay.DType.html#tensorplay.DType), optional) – the desired data type of returned tensor. Default: if None, infers data type from [data](/docs/generated/tensorplay.data.html#tensorplay.data). - device ([tensorplay.device](/docs/generated/tensorplay.Device.html#tensorplay.Device), optional) – the device of the constructed tensor. If None and data is a tensor then the device of data is used. If None and data is not a tensor then the result tensor is constructed on the current device. Example: ``` >>> a = numpy.array([1, 2, 3]) >>> t = tensorplay.as_tensor(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3]) >>> a = numpy.array([1, 2, 3]) >>> t = tensorplay.as_tensor(a, device=tensorplay.device('cuda')) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([1, 2, 3]) ```