PyTorch相关运算

一、四则运算

import torch
 
a = torch.tensor([
    [0,1],
    [2,3]
])
 
b = torch.tensor([
    [5, 10]
])
 
# 加
print("torch.all(torch.eq(a+b, torch.add(a,b))):",
      torch.all(torch.eq(a+b, torch.add(a,b))))
print("a+b:\n{}\n".format(a+b))
 
# 减
print("torch.all(torch.eq(a-b, torch.sub(a,b))):",
      torch.all(torch.eq(a-b, torch.sub(a,b))))
print("a-b:\n{}\n".format(a-b))
 
# 乘(是点乘,不同于矩阵乘法)
print("torch.all(torch.eq(a*b, torch.mul(a,b))):",
      torch.all(torch.eq(a*b, torch.mul(a,b))))
print("a*b:\n{}\n".format(a*b))
 
# 除
print("torch.all(torch.eq(a/b, torch.div(a,b))):",
      torch.all(torch.eq(a/b, torch.div(a,b))))
print("a/b:\n{}\n".format(a/b))

运行结果如下:

PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
torch.all(torch.eq(a+b, torch.add(a,b))): tensor(True)
a+b:
tensor([[ 5, 11],
        [ 7, 13]])
 
torch.all(torch.eq(a-b, torch.sub(a,b))): tensor(True)
a-b:
tensor([[-5, -9],
        [-3, -7]])
 
torch.all(torch.eq(a*b, torch.mul(a,b))): tensor(True)
a*b:
tensor([[ 0, 10],
        [10, 30]])
 
torch.all(torch.eq(a/b, torch.div(a,b))): tensor(True)
a/b:
tensor([[0.0000, 0.1000],
        [0.4000, 0.3000]])
注意事项:

 a * b,要求两个矩阵维度完全一致,即两个矩阵对应元素相乘,输出的维度也和原矩阵维度相同:

import torch
a = torch.randn(3,4)
b = torch.randn(3,4)
print(a)
print(b)
c = a * b
print(c.size())
print(c)

运行结果:

PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
tensor([[-1.3945,  1.6582,  0.7624,  0.3704],
        [-1.0848, -1.1854, -1.4984, -0.3506],
        [-0.2800, -0.2005,  1.3975,  0.4640]])
tensor([[ 0.3081, -0.1954,  1.3569,  0.8477],
        [ 1.4227, -0.1329, -2.3404,  0.0841],
        [ 0.6497, -0.8493, -0.0081,  0.3146]])
torch.Size([3, 4])
tensor([[-0.4296, -0.3240,  1.0346,  0.3139],
        [-1.5435,  0.1575,  3.5067, -0.0295],
        [-0.1819,  0.1703, -0.0113,  0.1460]])

二、matmul(a, b)函数

 torch.matmul(a,b),matmul可以进行张量乘法,输入可以是高维,当输入是多维时,把多出的一维作为batch提出来,其他部分做矩阵乘法:

import torch
a = torch.randn(2,3,4)
b = torch.randn(4,3)
print(a)
print(b)
c = torch.matmul(a,b)
print(c.size())
print(c)

运行结果:

PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
tensor([[[ 1.3968,  1.3581, -0.3681, -0.5124],
         [-2.3154,  0.2749,  0.4301, -1.1227],
         [ 1.2104,  0.9626,  0.6384, -0.3354]],
 
        [[ 0.8319, -1.4187, -2.1479,  0.9941],
         [-0.5659, -0.8840,  0.4688, -0.0346],
         [ 0.8974, -1.7360, -1.9097,  0.8564]]])
tensor([[-1.2399, -0.2424,  0.4228],
        [-1.2637, -1.1116, -1.2583],
        [ 0.2614,  0.8575, -0.4167],
        [ 0.3284, -1.6039, -0.0701]])
torch.Size([2, 3, 3])
tensor([[[-3.7126, -1.3421, -0.9291],
         [ 2.2671,  2.4252, -1.4253],
         [-2.6603, -0.2781, -0.9420]],
 
        [[ 0.5263, -2.0609,  2.9623],
         [ 1.9300,  1.5773,  0.6802],
         [ 0.8631, -1.2990,  3.2996]]])

三、幂运算

幂运算主要演示了平方以及平方根,代码如下:

import torch
 
a = torch.tensor([
    [10, 20],
    [30, 40]
])
#平方
print("a.pow(2):\n{}\n".format(a.pow(2)))
print("a**2:\n{}\n".format(a**2))
#平方根
print("a.pow(0.5):\n{}\n".format(a.pow(0.5)))
print("a.sqrt():\n{}\n".format(a.sqrt()))
# 平方根的倒数
print("a.rsqrt():\n{}\n".format(a.rsqrt()))
print("1/(a**0.5):\n{}\n".format(1/(a**0.5)))

运行结果:

PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
a.pow(2):
tensor([[ 100,  400],
        [ 900, 1600]])
 
a**2:
tensor([[ 100,  400],
        [ 900, 1600]])
 
a.pow(0.5):
tensor([[3.1623, 4.4721],
        [5.4772, 6.3246]])
 
a.sqrt():
tensor([[3.1623, 4.4721],
        [5.4772, 6.3246]])
 
a.rsqrt():
tensor([[0.3162, 0.2236],
        [0.1826, 0.1581]])
 
1/(a**0.5):
tensor([[0.3162, 0.2236],
        [0.1826, 0.1581]])

四、指对运算

import torch
 
a = torch.tensor([
    [10, 8],
    [10, 5]
])
# e^x
a_exp = torch.exp(a)
print("torch.exp(a):\n{}\n".format(a_exp))
# log(x)
# 以x为底:logx
# 以10为底:log10
print("torch.log10(a):\n{}\n".format(torch.log10(a)))
 

运行结果:

PS E:\pytorch> & E:/Python/python.exe e:/pytorch/tensor.py
torch.exp(a):
tensor([[22026.4648,  2980.9580], 
        [22026.4648,   148.4132]])
 
torch.log10(a):
tensor([[1.0000, 0.9031],
        [1.0000, 0.6990]])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值