import torch
from torch import nn
torch.device('cpu'), torch.cuda.device('cuda'), torch.cuda.device('cuda:1')
查询GPU个数
torch.cuda.device_count()
这两个函数允许我们在请求的GPU不存在的情况下运行代码
deftry_gpu(i=0):"""如果存在,则返回gpu(i),否则返回cpu()。"""if torch.cuda.device_count()>= i +1:return torch.device(f'cuda:{i}')return torch.device('cpu')deftry_all_gpus():"""返回所有可用的GPU,如果没有GPU,则返回[cpu(),]。"""
devices =[
torch.device(f'cuda:{i}')for i inrange(torch.cuda.device_count())]return devices if devices else[torch.device('cpu')]
try_gpu(), try_gpu(10), try_all_gpus()
查询张量所存储的设备
x = torch.tensor([1,2,3])
x.device # 观察是在cpu还时gpu上
X = torch.ones(2,3, device=try_gpu())# 指定存储在GPU上
Y = torch.rand(2,3, device=try_gpu(1))# 第二个GPU上创建一个随机张量
Z = X.cuda(1)# 将X的数据从0copy到1GPUprint(X)print(Z)# 要计算X + Y,我们需要决定在哪里执行这个操作
Y + Z # 现在数据在同一个GPU上(Z和Y都在),我们可以将它们相加
神经网络与GPU
net = nn.Sequential(nn.Linear(3,1))
net = net.to(device=try_gpu())
net(X)# 需要copy数据、权重、输入进入GPU
卷积神经网络
卷积
定义
∫
0
t
f
(
x
)
g
(
t
−
x
)
d
x
\int_0^t f(x)g(t-x) dx
∫0tf(x)g(t−x)dx
傅立叶变换的定义
F
(
w
)
=
∫
−
∞
+
∞
f
(
t
)
∗
e
−
i
w
t
d
t
F(w)=\int_{-\infty}^{+\infty}f(t)*e^{-iwt}dt
F(w)=∫−∞+∞f(t)∗e−iwtdt
F
(
f
)
=
∫
−
∞
+
∞
f
(
t
)
∗
e
−
i
2
Π
t
d
t
F(f)=\int_{-\infty}^{+\infty}f(t)*e^{-i2\Pi t}dt
F(f)=∫−∞+∞f(t)∗e−i2Πtdt
那么还原一个原始特征我们就可以理解为傅立叶变换得到的特征值乘模型相加即可得到原始信息
模
式
w
:
e
i
w
t
模式_w:e^{iwt}
模式w:eiwt
特征
值
w
:
F
(
w
)
=
∣
F
(
w
)
∣
∗
e
i
φ
−
提供幅度和相位信息
特征值_w:F(w)=|F(w)|*e^{i\varphi}-提供幅度和相位信息
特征值w:F(w)=∣F(w)∣∗eiφ−提供幅度和相位信息
f
(
t
)
=
1
2
Π
∫
−
∞
+
∞
F
(
w
)
e
i
w
t
d
w
−
还原信息
f(t)=\frac{1}{2\Pi}\int_{-\infty}^{+\infty}F(w)e^{iwt}dw-还原信息
f(t)=2Π1∫−∞+∞F(w)eiwtdw−还原信息
加窗—Gabor变换
F
(
n
,
s
)
=
∫
−
∞
+
∞
f
(
t
)
d
n
,
s
(
t
)
d
t
F(n, s) = \int_{-\infty}^{+\infty}f(t)d_{n,s}(t)dt
F(n,s)=∫−∞+∞f(t)dn,s(t)dt
d
n
,
s
⃗
=
g
(
t
−
s
)
∗
e
i
n
t
\vec{d_{n,s}}=g(t-s)*e^{int}
dn,s=g(t−s)∗eint
g
a
(
t
−
s
)
=
1
2
Π
a
e
−
(
t
−
s
)
2
4
a
∗
2
g_a(t-s) = \frac{1}{2\sqrt{\Pi a}}e^{-\frac{(t-s)^2}{4a}}*2
ga(t−s)=2Πa1e−4a(t−s)2∗2
n
就是模式特征,
s
就是窗口位置
n就是模式特征,s就是窗口位置
n就是模式特征,s就是窗口位置