a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
add 加法运算
import torch
# 加法运算的几种方式
print('a + b\n', a + b)
print('a.add(b)\n', a.add(b))
print('torch.add(a, b)\n', torch.add(a, b))
# 仅这种方式会改变a的值,即把结果保存到a中
print('a.add_(b)\n', a.add_(b))
print(a)
结果:
tensor([[0.9993, 0.9160, 0.4045],
[0.6335, 0.2120, 0.6178]])
tensor([[0.8361, 0.6041, 0.1181],
[0.5700, 0.5781, 0.6998]])
a + b
tensor([[1.8354, 1.5201, 0.5226],
[1.2035, 0.7901, 1.3176]])
a.add(b)
tensor([[1.8354, 1.5201, 0.5226],
[1.2035, 0.7901, 1.3176]])
torch.add(a, b)
tensor([[1.8354, 1.5201, 0.5226],
[1.2035, 0.7901, 1.3176]])
a.add_(b)
tensor([[1.8354, 1.5201, 0.5226],
[1.2035, 0.7901, 1.3176]])
tensor([[1.8354, 1.5201, 0.5226],
[1.2035, 0.7901, 1.3176]])
sub 减法:
import torch
a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('减法操作')
print('a\n', a)
print('a-b\n', a-b)
print('a.sub(b)\n', a.sub(b))
print('torch.sub(a, b)\n', torch.sub(a, b))
print('a.sub_(b)\n', a.sub_(b))
print(a)
结果:
tensor([[0.8540, 0.5175, 0.9498],
[0.8799, 0.8251, 0.7269]])
tensor([[0.2576, 0.6812, 0.9487],
[0.1363, 0.7312, 0.4687]])
减法操作
a
tensor([[0.8540, 0.5175, 0.9498],
[0.8799, 0.8251, 0.7269]])
a-b
tensor([[ 0.5964, -0.1637, 0.0011],
[ 0.7437, 0.0939, 0.2582]])
a.sub(b)
tensor([[ 0.5964, -0.1637, 0.0011],
[ 0.7437, 0.0939, 0.2582]])
torch.sub(a, b)
tensor([[ 0.5964, -0.1637, 0.0011],
[ 0.7437, 0.0939, 0.2582]])
a.sub_(b)
tensor([[ 0.5964, -0.1637, 0.0011],
[ 0.7437, 0.0939, 0.2582]])
tensor([[ 0.5964, -0.1637, 0.0011],
[ 0.7437, 0.0939, 0.2582]])
mul 乘法:
import torch
a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('乘法: 对应位置上的元素相乘')
print('a\n', a)
print('a*b\n', a*b)
print('torch.mul(a, b)\n', torch.mul(a, b))
print('a.mul(b)\n', a.mul(b))
print('a.mul_(b)\n', a.mul_(b))
print('a', a)
结果:
tensor([[0.6888, 0.6880, 0.8884],
[0.9059, 0.8689, 0.5366]])
tensor([[0.5977, 0.1775, 0.9614],
[0.4969, 0.8030, 0.6663]])
乘法: 对应位置上的元素相乘
a
tensor([[0.6888, 0.6880, 0.8884],
[0.9059, 0.8689, 0.5366]])
a*b
tensor([[0.4117, 0.1221, 0.8541],
[0.4501, 0.6977, 0.3575]])
torch.mul(a, b)
tensor([[0.4117, 0.1221, 0.8541],
[0.4501, 0.6977, 0.3575]])
a.mul(b)
tensor([[0.4117, 0.1221, 0.8541],
[0.4501, 0.6977, 0.3575]])
a.mul_(b)
tensor([[0.4117, 0.1221, 0.8541],
[0.4501, 0.6977, 0.3575]])
a tensor([[0.4117, 0.1221, 0.8541],
[0.4501, 0.6977, 0.3575]])
div 除法
import torch
a = torch.rand(2, 3)
b = torch.rand(2, 3)
print(a)
print(b)
print('除法')
print('a\n', a)
print('a/b\n', a/b)
print('torch.div(a, b)\n', torch.div(a, b))
print('a.div(b)\n', a.div(b))
print('a.div_(b)\n', a.div_(b))
print('a', a)
结果:
tensor([[0.3184, 0.7036, 0.5756],
[0.5146, 0.4399, 0.1367]])
tensor([[0.9408, 0.1757, 0.7494],
[0.0198, 0.7022, 0.8607]])
除法
a
tensor([[0.3184, 0.7036, 0.5756],
[0.5146, 0.4399, 0.1367]])
a/b
tensor([[ 0.3385, 4.0043, 0.7680],
[25.9892, 0.6265, 0.1588]])
torch.div(a, b)
tensor([[ 0.3385, 4.0043, 0.7680],
[25.9892, 0.6265, 0.1588]])
a.div(b)
tensor([[ 0.3385, 4.0043, 0.7680],
[25.9892, 0.6265, 0.1588]])
a.div_(b)
tensor([[ 0.3385, 4.0043, 0.7680],
[25.9892, 0.6265, 0.1588]])
a tensor([[ 0.3385, 4.0043, 0.7680],
[25.9892, 0.6265, 0.1588]])
@、matmul、mm矩阵运算
import torch
# 矩阵运算 matmul
a = torch.ones(2, 1)
b = torch.ones(1, 2)
print('a @ b\n', a @ b)
print('a.matmul(b)\n', a.matmul(b))
print('torch.matmul(a, b)\n', torch.matmul(a, b))
print('torch.mm(a, b)\n', torch.mm(a, b))
print('a.mm(b)\n', a.mm(b))
结果:
a @ b
tensor([[1., 1.],
[1., 1.]])
a.matmul(b)
tensor([[1., 1.],
[1., 1.]])
torch.matmul(a, b)
tensor([[1., 1.],
[1., 1.]])
torch.mm(a, b)
tensor([[1., 1.],
[1., 1.]])
a.mm(b)
tensor([[1., 1.],
[1., 1.]])
matmul高维矩阵运算
import torch
# 高维运算的时候要注意前边的维度一定要一样,后边的两个维度要和二维的一样即:
# 第一个的最后一个维度的数字,要和第二个的倒数第二个的数字一样
a = torch.ones(1, 2, 3, 4)
b = torch.ones(1, 2, 4, 3)
print(torch.matmul(a, b))
print(torch.matmul(a, b).shape)
结果:
tensor([[[[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.]],
[[4., 4., 4.],
[4., 4., 4.],
[4., 4., 4.]]]])
torch.Size([1, 2, 3, 3])
pow指数运算
import torch
print('指数运算:即a中的每个元素的n次方 ')
a = torch.tensor([1, 2])
print('a\n', a)
print('torch.pow(a, 3)\n', torch.pow(a, 3))
print('a.pow(3)\n', a.pow(3))
print('a**3\n', a**3)
print('a.pow_(3)\n', a.pow_(3))
print('a\n', a)
结果:
指数运算:即a中的每个元素的n次方
a
tensor([1, 2])
torch.pow(a, 3)
tensor([1, 8])
a.pow(3)
tensor([1, 8])
a**3
tensor([1, 8])
a.pow_(3)
tensor([1, 8])
a
tensor([1, 8])
exp的运算
import torch
print('e的运算:即 e的n次方 ')
# 注意必须要是float类型的否则会报错
a = torch.tensor([1, 2], dtype=torch.float32)
print('a\n', a)
print('torch.exp(a)\n', torch.exp(a))
print('torch.exp_(a)\n', torch.exp_(a))
a = torch.tensor([1, 2] , dtype=torch.float32)
print('a.exp()\n', a.exp())
print('a.exp_()', a.exp_())
print('a\n', a)
结果:
e的运算:即 e的n次方
a
tensor([1., 2.])
torch.exp(a)
tensor([2.7183, 7.3891])
torch.exp_(a)
tensor([2.7183, 7.3891])
a.exp()
tensor([2.7183, 7.3891])
a.exp_() tensor([2.7183, 7.3891])
a
tensor([2.7183, 7.3891])
log运算
import torch
print('log运算: log以1为底的')
a = torch.tensor([1, 2], dtype=torch.float32)
print('a\n', a)
print('torch.log(a)\n', torch.log(a))
print('torch.log_(a)', torch.log_(a))
a = torch.tensor([1, 2], dtype=torch.float32)
print('a.log()\n', a.log())
print('a.log_()\n', a.log_())
print('a\n', a)
结果:
log运算: log以1为底的
a
tensor([1., 2.])
torch.log(a)
tensor([0.0000, 0.6931])
torch.log_(a) tensor([0.0000, 0.6931])
a.log()
tensor([0.0000, 0.6931])
a.log_()
tensor([0.0000, 0.6931])
a
tensor([0.0000, 0.6931])
sqrt开平方
import torch
print('开平方')
a = torch.tensor([4, 5], dtype=torch.float32)
print('a\n', a)
print('torch.sqrt(a)\n', torch.sqrt(a))
print('torch.sqrt_(a)\n', torch.sqrt_(a))
print('a\n', a)
a = torch.tensor([4, 5], dtype=torch.float32)
print('a.sqrt()\n', a.sqrt())
print('a.sqrt_()\n', a.sqrt_())
print('a\n', a)
结果:
开平方
a
tensor([4., 5.])
torch.sqrt(a)
tensor([2.0000, 2.2361])
torch.sqrt_(a)
tensor([2.0000, 2.2361])
a
tensor([2.0000, 2.2361])
a.sqrt()
tensor([2.0000, 2.2361])
a.sqrt_()
tensor([2.0000, 2.2361])
a
tensor([2.0000, 2.2361])