-
Notifications
You must be signed in to change notification settings - Fork 4
/
actor_critic_structure.py
136 lines (123 loc) · 4.9 KB
/
actor_critic_structure.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
128
129
130
131
132
133
134
135
136
import torch.nn as nn
import torch.optim as optim
import gym
import torch
from dqn import ReplayBuffer
from torch.distributions import Categorical, Normal
from torch.nn.functional import mse_loss
import numpy as np
from torch.optim.lr_scheduler import StepLR
from torch.optim.lr_scheduler import ReduceLROnPlateau
# In[]:
# TODO :
# 2. Fixing target
class Critic(nn.Module):
def __init__(self, input_size, output_size=1, hidden_size=12):
super(Critic, self).__init__()
self.layer1 = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU()
)
self.layer2 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
self.layer3 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
self.output_layer = nn.Linear(hidden_size, output_size)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.output_layer(out)
return out
class QCritic(nn.Module):
def __init__(self, input_size, output_size, hidden_size=12):
super(QCritic, self).__init__()
self.layer1 = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU()
)
self.layer2 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
self.layer3 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
self.output_layer = nn.Linear(hidden_size, output_size)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.output_layer(out)
return out
class Actor(nn.Module):
def __init__(self, input_size, output_size, hidden_size=12, continuous=False):
super(Actor, self).__init__()
self.continuous = continuous
self.layer1 = nn.Sequential(
nn.Linear(input_size, hidden_size),
nn.ReLU()
)
self.layer2 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
self.layer3 = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU()
)
if continuous:
# if its continuous action space then done use a softmax at the last layer
self.output_layer = nn.Sequential(
nn.Linear(hidden_size, output_size),
nn.Sigmoid() # sigmoid is needed to bring the output between 0 to 1, later in forward function we'll
# transform this to -1 to 1
)
else:
# else use a softmax
self.output_layer = nn.Sequential(
nn.Linear(hidden_size, output_size),
# TODO : Try out log here if any numerical instability occurs
nn.Softmax(dim=-1)
)
def forward(self, x):
out = self.layer1(x)
out = self.layer2(out)
out = self.layer3(out)
out = self.output_layer(out)
# transform the output between -1 to 1 for continuous action spaces
if self.continuous:
out = 2*out - 1
return out
def select_action(self, current_state):
"""
selects an action as per some decided exploration
:param current_state: the current state
:return: the chosen action and the log probility to act as the gradient
"""
if not self.continuous:
# if its not continuous action space then use epsilon greedy selection
probs = self(current_state) # probs is the probability of each of the discrete actions possible
# No gaussian exploration can be performed since the actions are discrete and not continuous
# gaussian would make sense and feasibility only when actions are continuous
m = Categorical(probs)
action = m.sample()
return action, m.log_prob(action)
else:
# use gaussian or other form of exploration in continuous action space
action = self(current_state) # action is the action predicted for this current_state
# now time to explore, so sample from a gaussian distribution centered at action
# TODO : This scale can be controlled, its the variance around the mean action
m = Normal(loc=action, scale=torch.Tensor([0.1]))
explored_action = m.sample()
# keep sampling new actions until it is within -1 to +1.
while not (explored_action <= +1 and explored_action >= -1):
explored_action = m.sample()
# Note that the log prob should be at the original action, not at the exploration since the gradient used
# will be the gradient of actor's prediction, not of actor's exploration
return explored_action, m.log_prob(action)