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
| import torch
import torch.nn as nn
class NeuralVAD(nn.Module):
def __init__(self, input_dim: int = 40):
super().__init__()
# 特征提取
self.feature_extractor = nn.Sequential(
nn.Conv1d(1, 32, kernel_size=3, padding=1),
nn.ReLU(),
nn.Conv1d(32, 64, kernel_size=3, padding=1),
nn.ReLU(),
nn.MaxPool1d(2)
)
# RNN层
self.lstm = nn.LSTM(
input_size=64,
hidden_size=128,
num_layers=2,
batch_first=True,
bidirectional=True
)
# 分类头
self.classifier = nn.Sequential(
nn.Linear(256, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 2) # 语音/非语音
)
def forward(self, x):
# x shape: (batch, time, features)
x = x.transpose(1, 2) # (batch, features, time)
# 特征提取
features = self.feature_extractor(x.unsqueeze(1))
features = features.transpose(1, 2) # (batch, time, features)
# RNN处理
lstm_out, _ = self.lstm(features)
# 分类
logits = self.classifier(lstm_out)
return logits
|