本文简要介绍python语言中 torch.as_tensor 的用法。
用法:
torch.as_tensor(data, dtype=None, device=None) → Tensordata(array_like) -张量的初始数据。可以是列表、元组、NumPy
ndarray、标量和其他类型。dtype(
torch.dtype, 可选的) -返回张量的所需数据类型。默认值:如果None,则从data推断数据类型。device(
torch.device, 可选的) -返回张量的所需设备。默认值:如果None,使用当前设备作为默认张量类型(参见torch.set_default_tensor_type())。device将是 CPU 张量类型的 CPU 和 CUDA 张量类型的当前 CUDA 设备。
将数据转换为
torch.Tensor。如果数据已经是具有相同dtype和device的Tensor,则不会执行复制,否则如果数据Tensor具有requires_grad=True,则将返回一个新的Tensor并保留计算图。同理,如果数据是对应dtype的ndarray,而device是cpu,则不进行拷贝。例子:
>>> a = numpy.array([1, 2, 3]) >>> t = torch.as_tensor(a) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([-1, 2, 3]) >>> a = numpy.array([1, 2, 3]) >>> t = torch.as_tensor(a, device=torch.device('cuda')) >>> t tensor([ 1, 2, 3]) >>> t[0] = -1 >>> a array([1, 2, 3])
参数:
相关用法
- Python PyTorch as_strided用法及代码示例
- Python PyTorch assert_close用法及代码示例
- Python PyTorch async_execution用法及代码示例
- Python PyTorch asin用法及代码示例
- Python PyTorch asinh用法及代码示例
- Python PyTorch argsort用法及代码示例
- Python PyTorch addmm用法及代码示例
- Python PyTorch addmv用法及代码示例
- Python PyTorch apply_effects_tensor用法及代码示例
- Python PyTorch angle用法及代码示例
- Python PyTorch all_reduce用法及代码示例
- Python PyTorch atanh用法及代码示例
- Python PyTorch annotate用法及代码示例
- Python PyTorch argmax用法及代码示例
- Python PyTorch atan用法及代码示例
- Python PyTorch acos用法及代码示例
- Python PyTorch all_gather用法及代码示例
- Python PyTorch avg_pool1d用法及代码示例
- Python PyTorch allreduce_hook用法及代码示例
- Python PyTorch argmin用法及代码示例
- Python PyTorch any用法及代码示例
- Python PyTorch all_to_all用法及代码示例
- Python PyTorch add用法及代码示例
- Python PyTorch addcdiv用法及代码示例
- Python PyTorch acosh用法及代码示例
注:本文由纯净天空筛选整理自pytorch.org大神的英文原创作品 torch.as_tensor。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
