知乎专栏 |
Scalar(标量):指一个数值
Vector(向量):指一维数组
Matrix(矩阵):指二维数组
Tensor(张量):指大于二维的数组,即多位数组
import torch tensor = torch.tensor([5, 8, 3, 7, 2, 4, 1, 0, 6, 9]) print(tensor)
import torch tensor = torch.eye(2, 3) print(tensor) print(tensor.size()) print(tensor.size(0)) print(tensor.size(1))
tensor([[1., 0., 0.], [0., 1., 0.]]) torch.Size([2, 3]) 2 3
创建一个 3x3 的 Tensor
import torch x = torch.zeros(3, 3) print(x)
输出结果
tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.]])
import torch x = [1, 2, 3, 4, 5] print(torch.is_tensor(x)) x1 = torch.rand(1, 2) print(torch.is_tensor(x1))
输出结果
False True
import torch x = torch.eye(3, 3) print(x) print(torch.numel(x))
输出结果
tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]) 9
import numpy import torch array = numpy.array([1, 2, 3, 4, 5]) print(array) tensor = torch.from_numpy(array); print(tensor)
输出结果
[1 2 3 4 5] tensor([1, 2, 3, 4, 5])
tensor = torch.linspace(1, 9, 9) print(tensor)
tensor([1., 2., 3., 4., 5., 6., 7., 8., 9.])
返回值在0~1之间
tensor = torch.rand(10) print(tensor)
tensor([0.8865, 0.3672, 0.7740, 0.6729, 0.8264, 0.0811, 0.4243, 0.7992, 0.6984, 0.1110])
返回均值0,方差为1
tensor = torch.randn(10) print(tensor)
tensor([-0.3631, 0.9288, 0.5677, 0.5674, 0.2578, -0.4731, -0.3581, -0.6288, -1.1815, -0.1885])
import torch tensor = torch.randperm(10)
输出结果
tensor([5, 8, 3, 6, 0, 7, 1, 4, 2, 9])
import torch tensor = torch.randint(1, 10, (3, 3)) print(tensor)
输出结果
tensor([[6, 1, 9], [4, 3, 6], [4, 8, 4]])
arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) -> Tensor
参数
start (数字) – 起始值。默认值:0。
end (数字) – 结束值
step (数字) – 每对相邻数字之间的步长。默认值:1。
关键字参数
out (张量, 可选) – 输出张量。
dtype (torch.dtype,可选) – 据类型。默认值:如果为 None,则使用全局默认值(请参阅 torch.set_default_dtype())。如果未提供 dtype,则从其他输入参数推断数据类型。如果 start、end 或 stop 中的任何一个是浮点数,则 dtype 推断为默认 dtype,请参阅 get_default_dtype()。否则,dtype 推断为 torch.int64。
layout (torch.layout,可选) – 布局。默认值:torch.strided。
device (torch.device,可选) – 目标设备。默认值:如果为None,则使用默认张量类型当前的设备(参见 torch.set_default_device())。对于 CPU 张量类型,device 将为 CPU;对于 CUDA 张量类型,device 将为当前的 CUDA 设备。
requires_grad (bool, 可选) – autograd 是否应记录返回张量上的操作。默认值:False。
>>> torch.arange(5) # 默认以 0 为起点 tensor([ 0, 1, 2, 3, 4]) >>> torch.arange(1, 4) # 默认间隔为 1 tensor([ 1, 2, 3]) >>> torch.arange(1, 2.5, 0.5) # 指定间隔 0.5 tensor([ 1.0000, 1.5000, 2.0000])
import torch from torch.utils.data import Dataset, DataLoader # data = torch.arange(15) print(data)
tensor([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
import torch tensor = torch.arange(1, 10, 1) print(tensor) tensor = torch.arange(1, 10, 2) print(tensor) tensor = torch.arange(1, 10, 3) print(tensor)
输出结果
tensor([1, 2, 3, 4, 5, 6, 7, 8, 9]) tensor([1, 3, 5, 7, 9]) tensor([1, 4, 7])
data = torch.arange(15).reshape(5,3) print(data)
tensor([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11], [12, 13, 14]])
import torch tensor = torch.randint(1, 10, (3, 3)) print(tensor) min = torch.argmin(tensor, dim=0) print(min) max = torch.argmax(tensor, dim=0) print(max)
输出结果
tensor([[5, 1, 4], [7, 7, 8], [2, 2, 9]]) tensor([2, 0, 0]) tensor([1, 1, 2])
最小值返回索引 tensor([2, 0, 0]),2 表示列第三个,0表示第二列第一个,最后一个0表示第三列第一个,最终可以获得 2,1,4 三个数据
最大值返回索引 tensor([1, 1, 2]),对应数据 7,7,9
垂直连续,追加数据
import torch tensor1 = torch.randint(1, 10, (3, 3)) print(tensor1) tensor2 = torch.randint(1, 10, (3, 3)) print(tensor2) tensor = torch.cat((tensor1, tensor2)) print(tensor)
输出结果
tensor([[1, 7, 5], [8, 6, 8], [6, 9, 2]]) tensor([[6, 5, 6], [4, 2, 2], [5, 9, 3]]) tensor([[1, 7, 5], [8, 6, 8], [6, 9, 2], [6, 5, 6], [4, 2, 2], [5, 9, 3]])
水平连接,扩展数据
import torch tensor1 = torch.randint(1, 10, (3, 3)) print(tensor1) tensor2 = torch.randint(1, 10, (3, 3)) print(tensor2) tensor = torch.cat((tensor1, tensor2), dim=1) print(tensor)
输出结果
tensor([[6, 1, 6], [9, 4, 9], [5, 7, 5]]) tensor([[5, 9, 9], [2, 3, 8], [5, 2, 8]]) tensor([[6, 1, 6, 5, 9, 9], [9, 4, 9, 2, 3, 8], [5, 7, 5, 5, 2, 8]])
import torch tensor = torch.randint(1, 10, (3, 3)) print(tensor) # 将 tensor 切成 3 份 tensor1 = torch.chunk(tensor, 3) print(tensor1) # 基于 Y 轴将 tensor 切成 3 份 tensor2 = torch.chunk(tensor, 3, dim=1) print(tensor2)
输出结果
tensor([[7, 5, 6], [3, 5, 5], [2, 1, 8]]) (tensor([[7, 5, 6]]), tensor([[3, 5, 5]]), tensor([[2, 1, 8]])) (tensor([[7], [3], [2]]), tensor([[5], [5], [1]]), tensor([[6], [5], [8]]))
import torch x = torch.randn(6, 6) print(x) index = torch.tensor([1, 3, 5]) tensor = torch.index_select(input=x, dim=0, index=index) print(tensor)
输出结果
tensor([[ 0.2502, 0.0171, 0.0468, -1.1985, 0.1249, -0.2472], [-1.0763, -0.9561, 0.7500, 0.0903, -0.2404, 0.7940], [-0.2090, -1.1881, 0.6415, -1.8224, -1.2121, 0.0428], [-1.3796, -0.3021, -0.0945, 0.1808, -0.1802, -1.6069], [-1.0586, 0.0227, -0.4314, -0.4522, -0.6734, 0.0220], [-0.3171, -0.9069, 1.9850, 0.9845, -0.5600, -1.3951]]) tensor([[-1.0763, -0.9561, 0.7500, 0.0903, -0.2404, 0.7940], [-1.3796, -0.3021, -0.0945, 0.1808, -0.1802, -1.6069], [-0.3171, -0.9069, 1.9850, 0.9845, -0.5600, -1.3951]])
import torch tensor = torch.tensor([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) print(tensor) print(torch.split(tensor, 3))
tensor([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) (tensor([1, 2, 3]), tensor([4, 5, 6]), tensor([7, 8, 9]), tensor([10]))
行列转换
import torch tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(tensor) print(tensor.t()) print(tensor.transpose(1, 0))
输出结果
tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) tensor([[1, 4, 7], [2, 5, 8], [3, 6, 9]]) tensor([[1, 4, 7], [2, 5, 8], [3, 6, 9]])
import torch tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) print(tensor) print(torch.add(tensor, 1)) print(torch.mul(tensor, 2))
import torch a=torch.tensor([[1,2,3],[4,5,6]]) b=torch.tensor([[2,3,4],[5,6,7]]) c=torch.mul(a,b) print('a:',a) print('b:',b) print('c:',c)