-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.py
More file actions
57 lines (51 loc) · 1.66 KB
/
model.py
File metadata and controls
57 lines (51 loc) · 1.66 KB
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
import torch.nn as nn
import torch
class BiLSTM_Attention(nn.Module):
def __init__(self, input_dim=768, hidden_dim=128):
super().__init__()
self.bilstm = nn.LSTM(
input_size=input_dim,
hidden_size=hidden_dim,
num_layers=2,
bidirectional=True,
dropout=0.5
)
self.attention = nn.Sequential(
nn.Linear(hidden_dim*2, hidden_dim),
nn.Tanh(),
nn.Linear(hidden_dim, 1)
)
def forward(self, x):
# x: (seq_len, batch, input_dim)
outputs, _ = self.bilstm(x)
weights = torch.softmax(self.attention(outputs), dim=0)
return torch.sum(weights * outputs, dim=0)
class GRU_Model(nn.Module):
def __init__(self, input_dim=256, hidden_dim=256):
super().__init__()
self.gru = nn.GRU(
input_size=input_dim,
hidden_size=hidden_dim,
num_layers=2,
dropout=0.5
)
def forward(self, x):
_, h_n = self.gru(x)
return h_n[-1] # 取最后一层隐藏状态
class FusionModel(nn.Module):
def __init__(self):
super().__init__()
self.text_model = BiLSTM_Attention()
self.audio_model = GRU_Model()
self.fc = nn.Sequential(
nn.Linear(256+256, 128),
nn.ReLU(),
nn.Dropout(0.5),
nn.Linear(128, 1),
nn.Sigmoid()
)
def forward(self, audio_input, text_input):
audio_feat = self.audio_model(audio_input)
text_feat = self.text_model(text_input)
fused = torch.cat([audio_feat, text_feat], dim=1)
return self.fc(fused)