-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
127 lines (98 loc) · 3.88 KB
/
models.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
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
124
125
126
127
import torch
import torch.autograd as autograd
import torch.nn as nn
import torch.nn.functional as F
class Policy(nn.Module):
def __init__(self, num_inputs, num_outputs, hidden_size):
super(Policy, self).__init__()
self.affine1 = nn.Linear(num_inputs, hidden_size)
self.affine2 = nn.Linear(hidden_size, hidden_size)
self.action_mean = nn.Linear(hidden_size, num_outputs)
self.action_mean.weight.data.mul_(0.1)
self.action_mean.bias.data.mul_(0.0)
self.action_log_std = nn.Parameter(torch.zeros(1, num_outputs))
self.saved_actions = []
self.rewards = []
self.final_value = 0
def forward(self, x):
x = F.tanh(self.affine1(x))
x = F.tanh(self.affine2(x))
action_mean = self.action_mean(x)
action_log_std = self.action_log_std.expand_as(action_mean)
action_std = torch.exp(action_log_std)
return action_mean, action_log_std, action_std
class Value(nn.Module):
def __init__(self, num_inputs, hidden_size):
super(Value, self).__init__()
self.affine1 = nn.Linear(num_inputs, hidden_size)
self.affine2 = nn.Linear(hidden_size, hidden_size)
self.value_head = nn.Linear(hidden_size, 1)
self.value_head.weight.data.mul_(0.1)
self.value_head.bias.data.mul_(0.0)
def forward(self, x):
x = F.tanh(self.affine1(x))
x = F.tanh(self.affine2(x))
state_values = self.value_head(x)
return state_values
class Discriminator(nn.Module):
def __init__(self, num_inputs, hidden_size):
super(Discriminator, self).__init__()
self.linear1 = nn.Linear(num_inputs, hidden_size)
self.linear2 = nn.Linear(hidden_size, hidden_size)
self.linear3 = nn.Linear(hidden_size, 1)
self.linear3.weight.data.mul_(0.1)
self.linear3.bias.data.mul_(0.0)
def forward(self, x):
x = F.tanh(self.linear1(x))
x = F.tanh(self.linear2(x))
#prob = F.sigmoid(self.linear3(x))
output = self.linear3(x)
return output
class Generator(nn.Module):
def __init__(self, num_inputs, hidden_size, num_outputs):
super(Generator, self).__init__()
self.fc1 = nn.Linear(num_inputs, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, num_outputs)
def forward(self, x):
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
x = self.fc3(x)
return x
class Classifier(nn.Module):
def __init__(self, num_inputs, hidden_dim):
super(Classifier, self).__init__()
self.fc1 = nn.Linear(num_inputs, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, hidden_dim)
self.fc3 = nn.Linear(hidden_dim, 1)
self.d1 = nn.Dropout(0.5)
self.d2 = nn.Dropout(0.5)
self.fc3.weight.data.mul_(0.1)
self.fc3.bias.data.mul_(0.0)
def forward(self, x):
x = self.d1(torch.tanh(self.fc1(x)))
x = self.d2(torch.tanh(self.fc2(x)))
x = self.fc3(x)
return x
class ConfGenerator(nn.Module):
def __init__(self, num_inputs, hidden_size, num_outputs=1):
super().__init__()
self.fc1 = nn.Linear(num_inputs, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, num_outputs)
def forward(self, x):
x = torch.tanh(self.fc1(x))
x = torch.tanh(self.fc2(x))
x = torch.sigmoid(self.fc3(x))
return x
class ConfDiscriminator(nn.Module):
def __init__(self, num_inputs, hidden_size):
super().__init__()
self.fc1 = nn.Linear(num_inputs, hidden_size)
self.fc2 = nn.Linear(hidden_size, hidden_size)
self.fc3 = nn.Linear(hidden_size, 1)
def forward(self, x):
x = F.tanh(self.fc1(x))
x = F.tanh(self.fc2(x))
x = F.sigmoid(self.fc3(x))
return x