学习笔记,仅供参考,有错必究
文章目录
- 基本操作
- 加法
- 减法/乘法/除法
- 求和、最大、最小
PyTorch基础
基本操作
# 导入常用的包
import torch
import numpy as np
加法
a = torch.randint(1, 5, (2, 3))
b = torch.randint(1, 5, (2, 3))
a
b
tensor([[4, 1, 4],
[3, 3, 2]])
tensor([[3, 4, 2],
[1, 3, 2]])
# 加法的3种方式
a + b
torch.add(a, b)
# a与b相加,并将返回值赋值给a
a.add_(b) # 注意:任何使张量会发生变化的操作都有一个后缀"_",例如: a.add_(), a.sub_()
a
tensor([[7, 5, 6],
[4, 6, 4]])
tensor([[7, 5, 6],
[4, 6, 4]])
tensor([[7, 5, 6],
[4, 6, 4]])
tensor([[7, 5, 6],
[4, 6, 4]])
# 构造一个tensor,并将a + b的结果输出到tensor中
r = torch.zeros(2, 3)
torch.add(a, b, out=r)
tensor([[10., 9., 8.],
[ 5., 9., 6.]])
r
tensor([[10., 9., 8.],
[ 5., 9., 6.]])
减法/乘法/除法
# 减法
a - b
tensor([[4, 1, 4],
[3, 3, 2]])
# 乘法(对应元素相乘)
a * b
tensor([[21, 20, 12],
[ 4, 18, 8]])
# 除法(对应元素相除)
a/b
tensor([[2.3333, 1.2500, 3.0000],
[4.0000, 2.0000, 2.0000]])
# 取余
a%b
tensor([[1, 1, 0],
[0, 0, 0]])
# 取整
a // b
D:\software\Anaconda3\lib\site-packages\ipykernel_launcher.py:2: UserWarning: __floordiv__ is deprecated, and its behavior will change in a future version of pytorch. It currently rounds toward 0 (like the 'trunc' function NOT 'floor'). This results in incorrect rounding for negative values. To keep the current behavior, use torch.div(a, b, rounding_mode='trunc'), or for actual floor division, use torch.div(a, b, rounding_mode='floor').
tensor([[2, 1, 3],
[4, 2, 2]])
# 矩阵乘法(必须是张量中数据类型相同,才可以相乘)
a = torch.tensor([[1, 0, 2],
[0, 1, 1]], dtype = float)
b = torch.ones(3, 5, dtype = float)
a
b
a.dtype, b.dtype
torch.matmul(a, b) # 2*3 * 3*5 => 2*5
tensor([[1., 0., 2.],
[0., 1., 1.]], dtype=torch.float64)
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=torch.float64)
(torch.float64, torch.float64)
tensor([[3., 3., 3., 3., 3.],
[2., 2., 2., 2., 2.]], dtype=torch.float64)
求和、最大、最小
# 张量求和、求平均、求中位数、求最大、求最小
a = torch.rand(2, 3)
a
torch.sum(a)
torch.mean(a)
torch.median(a)
torch.max(a)
torch.min(a)
tensor([[0.6162, 0.4631, 0.9147],
[0.1778, 0.2920, 0.7270]])
tensor(3.1909)
tensor(0.5318)
tensor(0.4631)
tensor(0.9147)
tensor(0.1778)
# 最大值所处位置、最小值所处位置
torch.argmin(a)
torch.argmax(a)
tensor(3)
tensor(2)
# 对张量中的每个值进行开方/平方
torch.sqrt(a)
a ** 2
tensor([[0.7850, 0.6805, 0.9564],
[0.4217, 0.5404, 0.8526]])
tensor([[0.3797, 0.2145, 0.8367],
[0.0316, 0.0853, 0.5285]])
数据的索引
a = torch.arange(2, 14) # 生成数据
print(a)
tensor([ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])
tensor(5)
print(a[3])
print(a[1:3])
print(a[5:-2])
print(a[6:])
tensor(5)
tensor([3, 4])
tensor([ 7, 8, 9, 10, 11])
tensor([ 8, 9, 10, 11, 12, 13])
idx = [1, 5, 3, 7, 7]
a[idx]
tensor([3, 7, 5, 9, 9])
for t in a:
print(t)
tensor(2)
tensor(3)
tensor(4)
tensor(5)
tensor(6)
tensor(7)
tensor(8)
tensor(9)
tensor(10)
tensor(11)
tensor(12)
tensor(13)
求导
x = torch.ones((2, 2), requires_grad=True) # 定义2*2的张量,设置为允许计算梯度
x
tensor([[1., 1.],
[1., 1.]], requires_grad=True)
y = x + 1
y
tensor([[2., 2.],
[2., 2.]], grad_fn=<AddBackward0>)
z = (y**2) * 3
z
tensor([[12., 12.],
[12., 12.]], grad_fn=<MulBackward0>)
out_mean = z.mean()
print(out_mean)
tensor(12., grad_fn=<MeanBackward0>)
# 计算 out_mean的梯度(求导)
out_mean.backward()
# out_mean对x进行求导,则x的导数为3
x.grad
tensor([[3., 3.],
[3., 3.]])