-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtorch_tcn.py
76 lines (63 loc) · 3.25 KB
/
torch_tcn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import torch
import torch.nn as nn
from torch.nn.utils import weight_norm
from torch_attention import Multihead_Attention
class Chomp1d(nn.Module):
"""
因果卷积
"""
def __init__(self, chomp_size):
# 表示对继承自父类的属性进行初始化
super(Chomp1d, self).__init__()
self.chomp_size = chomp_size
def forward(self, x):
return x[:, :, :-self.chomp_size].contiguous()
class TemporalBlock(nn.Module):
def __init__(self, n_inputs, n_outputs, kernel_size, stride, dilation, padding, dropout=0.2):
super(TemporalBlock, self).__init__()
# 定义第一个扩散卷积层
self.conv1 = weight_norm(nn.Conv1d(n_inputs, n_outputs, kernel_size, stride, padding, dilation))
# 根据第一个卷积层的输出与padding大小实现因果卷积
self.chomp1 = Chomp1d(padding)
self.relu1 = nn.ReLU()
# 在先前输出结果上添加激活函数与dropout 完成第一个卷积
self.dropout1 = nn.Dropout2d(dropout)
self.conv2 = weight_norm(nn.Conv1d(n_outputs, n_outputs, kernel_size, stride, padding, dilation))
# padding保证了输入序列与输出序列的长度相等,但卷积前的通道数与卷积后的通道数不一定一样
self.chomp2 = Chomp1d(padding)
self.relu2 = nn.ReLU()
self.dropout2 = nn.Dropout2d(dropout)
# 将卷积模块的所有组件通过Sequential 方法依次堆叠
self.net = nn.Sequential(self.conv1, self.chomp1, self.relu1, self.dropout1,
self.conv2, self.chomp2, self.relu2, self.dropout2)
# 若卷积前后通道数不同,需要做逐元素的一维卷积
self.downsample = nn.Conv1d(n_inputs, n_outputs, 1) if n_inputs != n_outputs else None
self.relu = nn.ReLU()
self.init_weights()
def init_weights(self):
nn.init.kaiming_normal_(self.conv1.weight)
nn.init.kaiming_normal_(self.conv2.weight)
if self.downsample is not None:
nn.init.kaiming_normal_(self.downsample.weight)
def forward(self, x):
out = self.net(x)
res = x if self.downsample is None else self.downsample(x)
return self.relu(out + res)
class TemporalConvNet(nn.Module):
def __init__(self, num_inputs, num_channels, kernel_size=2, dropout=0.2):
super(TemporalConvNet, self).__init__()
layers = []
# num_channels 为各层卷积的输出通道数或卷积核数量 长度即需要执行的卷积层数量
num_levels = len(num_channels)
for i in range(num_levels):
# 扩展系数随层数指数增加
dilation_size = 2 ** i
# 从num_channels 中抽取每一个残差模块的输入通道数与输出通道数
in_channels = num_inputs if i == 0 else num_channels[i - 1]
out_channels = num_channels[i]
layers += [TemporalBlock(in_channels, out_channels, kernel_size, stride=1, dilation=dilation_size,
padding=(kernel_size - 1) * dilation_size, dropout=dropout)]
# layers += [Multihead_Attention(out_channels, num_heads=1, dropout=dropout)]
self.network = nn.Sequential(*layers)
def forward(self, x):
return self.network(x)