-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgenerator.py
48 lines (38 loc) · 1.6 KB
/
generator.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
import torch
from torch import nn
from torch.nn import Parameter
import torch.nn.functional as F
class LinearTrans(nn.Module):
def __init__(self, args, modality='text'):
super(LinearTrans, self).__init__()
if modality == 'text':
in_dim, out_dim = args.dst_feature_dim_nheads[0] * 3, args.feature_dims[0]
elif modality == 'audio':
in_dim, out_dim = args.dst_feature_dim_nheads[0] * 3, args.feature_dims[1]
elif modality == 'vision':
in_dim, out_dim = args.dst_feature_dim_nheads[0] * 3, args.feature_dims[2]
self.linear = nn.Linear(in_dim, out_dim)
def forward(self, x):
return self.linear(x)
class Seq2Seq(nn.Module):
def __init__(self, args, modality='text'):
super(Seq2Seq, self).__init__()
if modality == 'text':
out_dim, in_dim = args.feature_dims[0], args.dst_feature_dim_nheads[0]*3
elif modality == 'audio':
out_dim, in_dim = args.feature_dims[1], args.dst_feature_dim_nheads[0]*3
elif modality == 'vision':
out_dim, in_dim = args.feature_dims[2], args.dst_feature_dim_nheads[0]*3
self.decoder = nn.LSTM(in_dim, out_dim, num_layers=2, batch_first=True)
def forward(self, x):
return self.decoder(x)
MODULE_MAP = {
'linear': LinearTrans,
}
class Generator(nn.Module):
def __init__(self, args, modality='text'):
super(Generator, self).__init__()
select_model = MODULE_MAP[args.generatorModule]
self.Model = select_model(args, modality=modality)
def forward(self, x):
return self.Model(x)