-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyNet.py
More file actions
123 lines (107 loc) · 4.31 KB
/
MyNet.py
File metadata and controls
123 lines (107 loc) · 4.31 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# coding = utf-8
import torch.nn as nn
import torch
import torch.nn.functional as F
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class MyGRUNet(nn.Module):
def __init__(self, input_size, hidden_size, seq_len, output_size, num_layers):
super().__init__()
self.backbone = nn.GRU(input_size, hidden_size, num_layers, batch_first=True) # utilize the GRU model in torch.nn
self.fc = nn.Linear(seq_len*hidden_size, output_size)
def forward(self, x):
# x is input, size (batch, seq, feature)
x, _ = self.backbone(x) # shape (batch, seq, hidden_size)
b, s, h = x.shape
x = x.reshape(b, s*h)
x = self.fc(x)
return x
class MyLSTMNet(nn.Module):
def __init__(self, input_size, hidden_size, seq_len, output_size, num_layers):
super().__init__()
self.backbone = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True) # utilize the GRU model in torch.nn
self.fc = nn.Linear(seq_len*hidden_size, output_size)
def forward(self, x):
# x is input, size (batch, seq, feature)
x, _ = self.backbone(x) # shape (batch, seq, hidden_size)
b, s, h = x.shape
x = x.reshape(b, s*h)
x = self.fc(x)
return x
class MyFCNet(nn.Module):
def __init__(self, input_size, hidden_size, seq_len, output_size):
super().__init__()
self.backbone = nn.Linear(input_size, hidden_size)
self.fc = nn.Linear(seq_len*hidden_size, output_size)
def forward(self, x):
# x is input, size (batch, seq, feature)
x, _ = self.backbone(x) # shape (batch, seq, hidden_size)
b, s, h = x.shape
x = x.reshape(b, s*h)
x = self.fc(x)
return x
class MyTransformer(nn.Module):
def __init__(self, d_model, nhead, seq_len, output_size):
super().__init__()
self.backbone = nn.TransformerEncoderLayer(d_model=d_model, nhead=nhead, batch_first=True)
self.fc = nn.Linear(seq_len, output_size)
def forward(self, x):
x = x.transpose(1, 2)
x = self.backbone(x)
x = self.fc(x)
return x
class MyBiLSTMNet(nn.Module):
def __init__(self, input_size, hidden_size, seq_len, output_size, num_layers):
super().__init__()
self.backbone = nn.LSTM(input_size, hidden_size, num_layers, bidirectional=True, batch_first=True) # utilize the GRU model in torch.nn
self.fc = nn.Linear(seq_len*hidden_size*2, output_size)
def forward(self, x):
# x is input, size (batch, seq, feature)
x, _ = self.backbone(x) # shape (batch, seq, hidden_size)
b, s, h = x.shape
x = x.reshape(b, s*h)
x = self.fc(x)
return x
class Actor(nn.Module):
def __init__(self, output_size):
super(Actor, self).__init__()
self.feature_net = nn.Linear(2, 1)
# self.dropout = nn.Dropout(p=0.6)
self.hidden1 = nn.Linear(30, 256)
self.hidden2 = nn.Linear(256, 256)
self.actor = nn.Linear(256, output_size)
self.action_index_list = []
self.prob_list = []
self.log_prob_list = []
self.reward_list = []
self.state_list = []
self.value_list = []
def forward(self, x):
x = F.relu(x)
x = self.feature_net(x) # shape: [batch, 30, 1]
# x = self.dropout(x)
x = x.transpose(1, 2) # shape: [batch, 1, 30]
x = F.relu(x)
x = self.hidden1(x) # shape: [batch, 1, 256]
res = x
x = self.hidden2(x)
x = x + res
x = F.relu(x)
x = self.actor(x) # vector [batch, 1, 4060]
b, s, h = x.shape
action_scores = x.view(b*s, h)
return F.softmax(action_scores, dim=1)
class Critic(nn.Module):
def __init__(self):
super(Critic, self).__init__()
self.feature_net = nn.Linear(2, 1)
# self.dropout = nn.Dropout(p=0.6)
self.critic = nn.Linear(30, 1)
def forward(self, x):
x = F.relu(x) # shape: [batch, 30, 2]
x = self.feature_net(x) # shape: [batch, 30, 1]
# x = self.dropout(x)
b, s, h = x.shape
x = x.view(b, s*h)
x = F.relu(x)
x = self.critic(x) # scalar, shape:[batch, 1, 1]
return x