快速查询
toch.max某一个维度的最大值
toch.max(input,dim)
输入: input是tensor,dim是max函数索引的维度,0表示第一维的最大值,1表示第二维的最大值,-1表示最后一维。
可用以下图来表示
输出: 函数会返回两个tensor,第一个tensor是每行的最大值,softmax的输出中最大的是1,所以第一个tensor是全1的tensor;第二个tensor是每行最大值的索引。
_, predicted = torch.max(outputs.data, 1)
## 下划线表示不在乎返回的第一个tensor值。只想得到第二个tensor
x = torch.randn(3,3)
print(x)
max_value, max_idx = torch.max(x,dim=1)
print(max_value, max_idx)
output:
tensor([[ 0.0735, -1.4541, 1.3885],
[ 0.5028, -1.6075, 1.6409],
[ 1.4283, 1.1493, 0.3060]])
tensor([1.3885, 1.6409, 1.4283]) tensor([2, 2, 0])
torch.sum在某个维度值相加
torch.sum(input,dim)
表示在第dim+1个维度tensor值相加
torch.squeeze删掉所有维度为1的维度
torch.squeeze() 和torch.unsqueeze()
squeeze(a) 就是将a中所有为1的维度删掉。不为1的维度没有影响。
a.unsqueeze(N)或torch.unsqueeze(a,N) 就是在a中指定位置N加上一个维数为1的维度。
a = torch.rand(1,3,4)
print(a.size()) #torch.Size([1, 3, 4])
b = torch.squeeze(a)
print(b.size()) #torch.Size([3, 4])
c = torch.unsqueeze(b,0)
print(c.size()) #torch.Size([1, 3, 4])
torch.eye创建一个单位矩阵
torch.eye()
初始化一个单位矩阵
a = torch.eye(4,4)
tensor([[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.]])
torch.arange和torch.range
torch.arange(start,end,step)
torch.range(start,end,step)
其中step默认为1
不同点:
①arrange不包括end,range是包括end的。
②range默认数据类型是float32,arange如果全为整数则默认为int64,但是arange产生的数是带有小数点的话,数据类型会自动变成float32
x = torch.arange(0,10,2)
print(x)
print(x.dtype)
tensor([0, 2, 4, 6, 8])
torch.int64
x = torch.range(0,10,2)
print(x)
print(x.dtype)
tensor([ 0., 2., 4., 6., 8., 10.])
torch.float32
os.path.join()路径拼接函数
os.path.join()
函数:连接两个或多个路径名组件