-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodel.py
70 lines (59 loc) · 3.28 KB
/
model.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
import torch
from torch import nn
from haipipe.core.al.config import Config
from torch.utils.data import Dataset, DataLoader, TensorDataset
from torch.autograd import Variable
import numpy as np
torch.set_printoptions(threshold=10)
config = Config()
class ScoreNN(nn.Module):
def __init__(self, dataset_input_dim, dataset_output_dim, pipeline_input_dim, seq_hidden_size, output_dim, prim_nums, seq_embedding_dim, seq_num_layers):
super(ScoreNN, self).__init__()
self.prim_nums = prim_nums
self.dataset_nn = nn.Sequential(
nn.Linear(dataset_input_dim, int(dataset_input_dim/2)),
nn.BatchNorm1d(int(dataset_input_dim/2)),
nn.LeakyReLU(),
nn.Linear(int(dataset_input_dim/2), int(dataset_input_dim/2)),
nn.BatchNorm1d(int(dataset_input_dim/2)),
nn.LeakyReLU(),
nn.Linear(int(dataset_input_dim/2), dataset_output_dim),
nn.BatchNorm1d(dataset_output_dim),
nn.LeakyReLU(),
nn.Linear(dataset_output_dim, dataset_output_dim),
nn.BatchNorm1d(dataset_output_dim),
nn.LeakyReLU(),
)
self.seq_embedding = nn.Embedding(prim_nums, seq_embedding_dim)
self.seq_lstm = nn.LSTM(input_size=seq_embedding_dim, hidden_size=seq_hidden_size,num_layers=seq_num_layers,
bias=True,batch_first=False,dropout=0.5,bidirectional=False)
inp_size = dataset_output_dim + seq_hidden_size*config.seq_len
self.end_mlp = nn.Sequential(
nn.Linear(inp_size,int(inp_size / 2)),
nn.LeakyReLU(),
nn.Linear(int(inp_size / 2), output_dim),
nn.LeakyReLU(),
nn.Linear(output_dim, output_dim),
nn.LeakyReLU(),
nn.Linear(output_dim, 10),
nn.LeakyReLU(),
nn.Linear(10, 1),
nn.BatchNorm1d(1, track_running_stats = False),
nn.Tanh(),
)
def forward(self, dataset_feature, pipeline_feature):
dataset_emb = self.dataset_nn(dataset_feature)
seq_embed_feature = self.seq_embedding(pipeline_feature) # (batch_size , seq_len , seq_embedding_dim)
seq_embed_feature = seq_embed_feature.permute(1,0,2) # (seq_len , batch_size , seq_embedding_dim)
seq_hidden_feature,(h_1,c_1) = self.seq_lstm(seq_embed_feature) # (6 , batch_size , seq_hidden_size)
seq_hidden_feature = seq_hidden_feature.permute(1,0,2) # (batch_size , 6 , seq_hidden_size)
seq_hidden_feature = torch.flatten(seq_hidden_feature, start_dim=1) # (batch_size , 6*seq_hidden_size)
input_feature = torch.cat((dataset_emb, seq_hidden_feature), 1)
return self.end_mlp(input_feature)
def latent(self, pipeline_feature):
seq_embed_feature = self.seq_embedding(pipeline_feature) # (batch_size , seq_len , seq_embedding_dim)
seq_embed_feature = seq_embed_feature.permute(1,0,2) # (seq_len , batch_size , seq_embedding_dim)
seq_hidden_feature,(h_1,c_1) = self.seq_lstm(seq_embed_feature) # (6 , batch_size , seq_hidden_size)
seq_hidden_feature = seq_hidden_feature.permute(1,0,2) # (batch_size , 6 , seq_hidden_size)
seq_hidden_feature = torch.flatten(seq_hidden_feature, start_dim=1) # (batch_size , 6*seq_hidden_size)
return seq_hidden_feature