Tensor运算
1.算术运算
1.1 torch.abs()
将参数传递到torch.abs后返回输入参数的绝对值作为输出,输出参数必须是一个Tensor数据类型的变量。
import torch
x = torch.randn(3,3)
print(x)
y = torch.abs(x)
print(y)
tensor([[-1.0108, 0.3079, 0.4085],
[-0.3317, 0.1803, -0.4838],
[ 0.8184, -1.2334, 0.3056]])
tensor([[1.0108, 0.3079, 0.4085],
[0.3317, 0.1803, 0.4838],
[0.8184, 1.2334, 0.3056]])
y+=1
print(x)
tensor([[-1.0108, 0.3079, 0.4085],
[-0.3317, 0.1803, -0.4838],
[ 0.8184, -1.2334, 0.3056]])
注意:y跟x不共用内存,是一个新的张量
1.2 torch.add()
将参数传递到torch.add后返回输入参数的求和结果作为输出,输入参数既可以全部是Tensor数据类型的变量,也可以是一个Tensor数据类型的变量,另一个是标量。
torch.add(x,y)
tensor([[1.0000, 1.6158, 1.8169],
[1.0000, 1.3605, 1.0000],
[2.6368, 1.0000, 1.6112]])
torch.add(x,1) # 这里是广播机制
tensor([[-0.0108, 1.3079, 1.4085],
[ 0.6683, 1.1803, 0.5162],
[ 1.8184, -0.2334, 1.3056]])
张量的计算更ndarray类似。
1.3 torch.clamp()
对输入参数按照自定义的范围进行裁剪,最后将参数裁剪的结果作为输出。
使用变量中的每个元素分别和裁剪的上边界及裁剪的下边界的值进行比较,如果元素的值小于裁剪的下边界的值,该元素就被重写成裁剪的下边界的值;同理,如果元素的值大于裁剪的上边界的值,该元素就被重写成裁剪的上边界的值。处在两者之间的值不变
## 例子
x = torch.randn(3,5)
x
tensor([[ 1.4978, 0.3753, -0.2933, -0.3250, -0.7770],
[-0.5563, 0.3333, -0.6111, 0.1093, -0.3945],
[ 0.0146, -0.6303, -0.0441, -0.6296, 1.6048]])
torch.clamp(x,-0.1,0.1)
tensor([[ 0.1000, 0.1000, -0.1000, -0.1000, -0.1000],
[-0.1000, 0.1000, -0.1000, 0.1000, -0.1000],
[ 0.0146, -0.1000, -0.0441, -0.1000, 0.1000]])
1.4 torch.div()
求商,可以是Tensor数据类型,也可以是标量,要满足运算要求
x = torch.full((3,3),6)
x
tensor([[6, 6, 6],
[6, 6, 6],
[6, 6, 6]])
注:创建一个全6的tensor,
torch.full(size,6)
,size---->(a,b,c)或(a,)
y = torch.full((3,3),2)
y
tensor([[2, 2, 2],
[2, 2, 2],
[2, 2, 2]])
torch.div(x,y)
tensor([[3., 3., 3.],
[3., 3., 3.],
[3., 3., 3.]])
torch.div(x,6)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
1.5 torch.mul
将参数传递到 torch.mul
后返回输入参数求积的结果作为输出,参与运算的参数可以全部是Tensor数据类型的变量,也可以是Tensor数据类型(size要一致)的变量和标量的组合。但输入要满足运算规则。
x = torch.full((3,3),2)
y = torch.full((3,3),5)
torch.mul(x,y)
tensor([[10, 10, 10],
[10, 10, 10],
[10, 10, 10]])
torch.mul(x,10)
tensor([[20, 20, 20],
[20, 20, 20],
[20, 20, 20]])
1.6 torch.pow()
将参数传递到torch.pow后返回输入参数的求幂结果作为输出,要求与上面类似。
torch.pow(x,3)
tensor([[8, 8, 8],
[8, 8, 8],
[8, 8, 8]])
1.7 torch.mm()
将参数传递到 torch.mm
后返回输入参数的求积结果作为输出,不过这个求积的方式和之前的torch.mul
运算方式不太样,torch.mm运用矩阵之间的乘法规则进行计算。
所以被传入的参数会被当作矩阵进行处理,参数的维度自然也要满足矩阵乘法的前提条件,即前一个矩阵的行数必须和后一个矩阵的列数相等,否则不能进行计算。
x = torch.randn(2,3)
y = torch.randn(3,2)
torch.mm(x,y)
tensor([[-0.0942, -0.3811],
[ 0.3003, 0.7441]])
1.8 torch.mv()
将参数传递到torch.mv
后返回输入参数的求积结果作为输出,torch.mv运用矩阵与向量之间的乘法规则进行计算,被传入的参数中的第1个参数代表矩阵,第2个参数代表向量,顺序不能颠倒。
x = torch.randn(2,3)
a = torch.randn(3)
torch.mv(x,a)
tensor([-2.6871, -1.1238])
b = torch.randn(2)
b
tensor([ 0.7763, -0.6943])
torch.mv(x,b)# 错误,2*3的矩阵与2维列向量不能相乘
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
<ipython-input-30-fdc84b0c8e6c> in <module>
----> 1 torch.mv(x,b)
RuntimeError: size mismatch, get 2, 2x3,2
torch.cat((tuple),int)
x = torch.randn(2,1,3)
y = torch.randn(2,3,3)
torch.cat((x,y),1) ## 在连接时,其他维度要相同
tensor([[[ 0.2218, 0.2060, 0.9170],
[-0.1496, -0.4709, 0.1596],
[ 0.3973, 0.1973, -0.9979],
[ 1.0322, 0.4818, 0.2642]],
[[ 0.7882, -0.0108, 0.9894],
[-0.7900, 0.2578, 0.1692],
[ 0.6749, -0.5383, -1.0364],
[-1.5664, 1.5597, 0.8563]]])
torch中的运算,跟NumPy有很多相似的地方,要注意运算规则和广播机制