-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNetworks.py
99 lines (76 loc) · 3.34 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
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
import torch as T
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
shared_CNN = None
device = T.device('cuda' if T.cuda.is_available() else 'cpu')
class Actor(nn.Module):
def __init__(self, alpha=0.0001, input_dims=[8], fc1_dims=400, fc2_dims=300, n_actions=2):
super(Actor, self).__init__()
self.alpha = alpha
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
f1 = 1 / np.sqrt(self.fc1.weight.data.size()[0])
T.nn.init.uniform_(self.fc1.weight.data, -f1, f1)
T.nn.init.uniform_(self.fc1.bias.data, -f1, f1)
self.bn1 = nn.LayerNorm(self.fc1_dims)
self.fc2 = nn.Linear(self.fc1_dims, self.fc2_dims)
f2 = 1 / np.sqrt(self.fc2.weight.data.size()[0])
T.nn.init.uniform_(self.fc2.weight.data, -f2, f2)
T.nn.init.uniform_(self.fc2.bias.data, -f2, f2)
self.bn2 = nn.LayerNorm(self.fc2_dims)
f3 = 0.003
self.mu = nn.Linear(self.fc2_dims, self.n_actions)
T.nn.init.uniform_(self.mu.weight.data, -f3, f3)
T.nn.init.uniform_(self.mu.bias.data, -f3, f3)
self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
self.optimizer = optim.Adam(self.parameters(), lr=self.alpha)
self.to(self.device)
def forward(self, state):
x = self.fc1(state)
x = self.bn1(x)
x = F.relu(x)
x = self.fc2(x)
x = self.bn2(x)
x = T.tanh(self.mu(x))
return x
class Critic(nn.Module):
def __init__(self, beta=0.001, input_dims=[8], fc1_dims=400, fc2_dims=300, n_actions=2):
super(Critic, self).__init__()
self.beta = beta
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
f1 = 1 / np.sqrt(self.fc1.weight.data.size()[0])
T.nn.init.uniform_(self.fc1.weight.data, -f1, f1)
T.nn.init.uniform_(self.fc1.bias.data, -f1, f1)
self.bn1 = nn.LayerNorm(self.fc1_dims)
self.fc2 = nn.Linear(self.fc1_dims, self.fc2_dims)
f2 = 1 / np.sqrt(self.fc2.weight.data.size()[0])
T.nn.init.uniform_(self.fc2.weight.data, -f2, f2)
T.nn.init.uniform_(self.fc2.bias.data, -f2, f2)
self.bn2 = nn.LayerNorm(self.fc2_dims)
self.action_value = nn.Linear(self.n_actions, fc2_dims)
f3 = 0.0003
self.q = nn.Linear(self.fc2_dims, 1)
T.nn.init.uniform_(self.q.weight.data, -f3, f3)
T.nn.init.uniform_(self.q.bias.data, -f3, f3)
self.device = T.device('cuda:0' if T.cuda.is_available() else 'cpu')
self.optimizer = optim.Adam(self.parameters(), lr=self.beta)
self.to(self.device)
def forward(self, state, action):
state_value = self.fc1(state)
state_value = self.bn1(state_value)
state_value = F.relu(state_value)
state_value = self.fc2(state_value)
state_value = self.bn2(state_value)
action_value = F.relu(self.action_value(action))
state_action_value = F.relu(T.add(state_value, action_value))
state_action_value = self.q(state_action_value)
return state_action_value