forked from BaratiLab/Graphene-RL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
86 lines (72 loc) · 2.69 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
# class ReplayMemory(object):
# def __init__(self, capacity):
# self.capacity = capacity
# self.memory = []
# self.position = 0
# def push(self, *args):
# """Saves a transition."""
# if len(self.memory) < self.capacity:
# self.memory.append(None)
# self.memory[self.position] = Transition(*args)
# self.position = (self.position + 1) % self.capacity
# def sample(self, batch_size):
# return random.sample(self.memory, batch_size)
# def __len__(self):
# return len(self.memory)
class StateEmbed(nn.Module):
def __init__(self, nb_states, nb_coords=1360, nb_fingerprint=1024, nb_candidate=20, nb_imgfeat=128):
super(StateEmbed, self).__init__()
self.nb_coords = nb_coords
self.nb_fingerprint = nb_fingerprint
self.nb_candidate = nb_candidate
self.nb_imgfeat = nb_imgfeat
self.coord_embed = nn.Sequential(
nn.Linear(nb_coords, 256),
nn.ReLU(inplace=True),
nn.Linear(256, 64),
nn.ReLU(inplace=True)
)
self.fp_embed = nn.Sequential(
nn.Linear(nb_fingerprint, 256),
nn.ReLU(inplace=True),
nn.Linear(256, 64),
nn.ReLU(inplace=True)
)
self.candidate_embed1 = nn.Embedding(608, 16)
self.candidate_embed2 = nn.Sequential(
nn.Linear(16*nb_candidate, 256),
nn.ReLU(inplace=True),
nn.Linear(256, 64),
nn.ReLU(inplace=True)
)
self.img_embed = nn.Sequential(
nn.Linear(nb_imgfeat, 64),
nn.ReLU(inplace=True)
)
def forward(self, x):
batch_size = x.shape[0]
emb1 = self.coord_embed(x[..., :self.nb_coords])
emb2 = self.fp_embed(x[..., self.nb_coords:self.nb_coords+self.nb_fingerprint])
cand_embed = self.candidate_embed1(x[..., -self.nb_candidate-self.nb_imgfeat:-self.nb_imgfeat].type(torch.LongTensor))
cand_embed = cand_embed.view(batch_size, -1)
emb3 = self.candidate_embed2(cand_embed)
emb4 = self.img_embed(x[..., -self.nb_imgfeat:])
return torch.cat((emb1, emb2, emb3, emb4), dim=len(x.shape)-1)
class DQN(nn.Module):
def __init__(self, in_dim, out_dim):
super(DQN, self).__init__()
self.model = nn.Sequential(
StateEmbed(in_dim),
nn.Linear(64*4, 64),
# nn.Linear(in_dim, 64),
nn.ReLU(),
nn.Linear(64, 32),
nn.ReLU(),
nn.Linear(32, out_dim)
)
def forward(self, x):
return self.model(x)