forked from huawei-noah/HEBO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
networks.py
54 lines (47 loc) · 1.48 KB
/
networks.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
from model.base import BaseModel
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from torch.nn import Module
from einops.layers.torch import Rearrange, Repeat
class EncoderCDR3(Module):
def __init__(self):
super(EncoderCDR3, self).__init__()
self.encoder = nn.Sequntial(
Rearrange('b n d -> b (n d)'),
nn.Linear(220, 300),
nn.BatchNorm1d(),
nn.ReLU(inplace=True),
nn.Linear(300, 100),
nn.BatchNorm1d(),
nn.ReLU(inplace=True),
nn.Linear(100, 40),
nn.BatchNorm1d(),
nn.ReLU(inplace=True),
)
self.q_mu = nn.Linear(40, 40)
self.q_logsigma = nn.Linear(40, 40)
def cat_to_onehot(self, x):
x_onehot = torch.zeros(x.shape[0], 20).to(x.device)
x_onehot.scatter_(1, x, 1.0)
return x_onehot
def forward(self, x):
x = self.cat_to_onehot(x)
return self.encoder(x)
class DecoderCDR3(Module):
def __init__(self):
super(DecoderCDR3, self).__init__()
self.decoder = nn.Sequntial(
nn.Linear(40, 100),
nn.BatchNorm1d(),
nn.ReLU(inplace=True),
nn.Linear(100, 300),
nn.BatchNorm1d(),
nn.ReLU(inplace=True),
nn.Linear(300, 220),
nn.BatchNorm1d(),
Rearrange('b (n d) -> b n d', n=11, d=20),
)
def forward(self, x):
return self.decoder(x)