1. Instances of the torch.Tensor class
Pytorch中tensor是torch.Tensor类的实例

2. Tensor attributes
torch.Tensor 有以下属性:
- torch.dtype:tensor数据格式
- torch.device:CPU/GPU
- torch.layout:tensor如何在内存中存储
Tensors contain data of a uniform type (dtype).
Tensor computations between tensors depend on the dtype and the device.
(1) tensor之间计算时需要dtype一致

(2) tensor之间计算时需要device一致

3. Creating tensors using data
These are the primary ways of creating tensor objects (instances of the torch.Tensor class), with data (array-like) in PyTorch
创建tensor对象的四种方法:
- torch.Tensor(data)
- torch.tensor(data)
- torch.as_tensor(data)
- torch.from_numpy(data)

这四种方法有什么区别
-
torch.Tensor()大写T:constructor of the torch.Tensor class -
torch.tensor()小写t:factory function that constructs torch.Tensor objects and returns them to the caller -
torch.as_tensor():factory function -
torch.from_numpy():factory function
改变data数据值时,torch.Tensor()和torch.tensor()不会改变,torch.as_tensor()和torch.from_numpy()会随之改变

发生这种情况是因为torch.Tensor()和torch.tensor()复制了它们的输入数据,而torch.as_tensor()和torch.from_numpy()与原始输入对象共享了它们在内存中的输入数据。共享数据比复制数据更有效,并且使用更少的内存。

torch.from_numpy()函数仅接受numpy.ndarrays,而torch.as_tensor()函数则接受包括其他PyTorch tensors在内的各种数组对象。
4. Best options for creating tensors in PyTorch
torch.tensor()和torch.as_tensor()是Pytorch中构建tensors的最好选择
- torch.tensor() call is the sort of go-to call
- torch.as_tensor() should be employed when tuning our code for performance.
Some things to keep in mind about memory sharing (it works where it can)
- Since
numpy.ndarrayobjects are allocated on the CPU, theas_tensor()function must copy the data from the CPU to the GPU when a GPU is being used.
由于numpy.ndarray对象是在CPU上分配的,因此在使用GPU时,as_tensor()函数必须将数据从CPU复制到GPU. - The memory sharing of
as_tensor()doesn’t work with built-in Python data structures like lists.
as_tensor()的内存共享不适用于内置Python数据结构(如列表) - The
as_tensor()call requires developer knowledge of the sharing feature. This is necessary so we don’t inadvertently make an unwanted change in the underlying data without realizing the change impacts multiple objects. - The
as_tensor()performance improvement will be greater if there are a lot of back and forth operations between numpy.ndarray objects and tensor objects. However, if there is just a single load operation, there shouldn’t be much impact from a performance perspective.