-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
106 lines (84 loc) · 3.79 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import os
class CriticNetwork(nn.Module):
def __init__(self, alpha, input_dims, fc1_dims, fc2_dims, n_actions, model_dir):
super(CriticNetwork, self).__init__()
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.model_dir = model_dir
# Define and randomly initialize the weights of the network
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
f1 = 1. / np.sqrt(self.fc1.weight.data.size()[0])
nn.init.uniform_(self.fc1.weight.data, -f1, f1)
self.ln1 = 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])
nn.init.uniform_(self.fc2.weight.data, -f2, f2)
self.ln2 = nn.LayerNorm(self.fc2_dims)
self.fca = nn.Linear(self.n_actions, self.fc2_dims)
q = 0.003
self.q = nn.Linear(self.fc2_dims, 1)
nn.init.uniform_(self.q.weight.data, -q, q)
self.optimizer = torch.optim.Adam(self.parameters(), lr=alpha, weight_decay=0.01)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.to(self.device)
def forward(self, state, action):
state_value = self.fc1(state)
state_value = self.ln1(state_value)
state_value = F.relu(state_value)
state_value = self.fc2(state_value)
state_value = self.ln2(state_value)
action_value = self.fca(action)
state_action_value = F.relu(torch.add(state_value, action_value))
q = self.q(state_action_value)
return q
def save_checkpoint(self, model_file):
print('... saving checkpoint ...')
torch.save(self.state_dict(), os.path.join(self.model_dir, model_file))
def load_checkpoint(self, model_file):
print('... loading checkpoint ...')
self.load_state_dict(torch.load(os.path.join(self.model_dir, model_file)))
class ActorNetwork(nn.Module):
def __init__(self, alpha, input_dims, fc1_dims, fc2_dims, n_actions, model_dir):
super(ActorNetwork, self).__init__()
self.input_dims = input_dims
self.fc1_dims = fc1_dims
self.fc2_dims = fc2_dims
self.n_actions = n_actions
self.model_dir = model_dir
self.fc1 = nn.Linear(*self.input_dims, self.fc1_dims)
f1 = 1./np.sqrt(self.fc1.weight.data.size()[0])
nn.init.uniform_(self.fc1.weight.data, -f1, f1)
nn.init.uniform_(self.fc1.bias.data, -f1, f1)
self.ln1 = 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])
nn.init.uniform_(self.fc2.weight.data, -f2, f2)
nn.init.uniform_(self.fc2.bias.data, -f2, f2)
self.ln2 = nn.LayerNorm(self.fc2_dims)
f3 = 0.003
self.mu = nn.Linear(self.fc2_dims, self.n_actions)
nn.init.uniform_(self.mu.weight.data, -f3, f3)
self.optimizer = torch.optim.Adam(self.parameters(), lr=alpha, weight_decay=0.01)
self.device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
self.to(self.device)
def forward(self, state):
x = self.fc1(state)
x = self.ln1(x)
x = F.relu(x)
x = self.fc2(x)
x = self.ln2(x)
x = F.relu(x)
mu = torch.tanh(self.mu(x))
return mu
def save_checkpoint(self, model_file):
print('... saving checkpoint ...')
torch.save(self.state_dict(), os.path.join(self.model_dir, model_file))
def load_checkpoint(self, model_file):
print('... loading checkpoint ...')
self.load_state_dict(torch.load(os.path.join(self.model_dir, model_file)))