-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
95 lines (81 loc) · 3.4 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
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
class ActorCritic(nn.Module):
def __init__(self, num_inputs, num_outputs, layer_norm=True):
super(ActorCritic, self).__init__()
self.actor_fc1 = nn.Linear(num_inputs, 64)
self.actor_fc2 = nn.Linear(64, 64)
self.actor_fc2_1 = nn.Linear(64, 64)
self.actor_fc3 = nn.Linear(64, num_outputs)
self.actor_logstd = nn.Parameter(torch.zeros(1, num_outputs))
self.critic_fc1 = nn.Linear(num_inputs, 64)
self.critic_fc2 = nn.Linear(64, 64)
self.critic_fc3 = nn.Linear(64, 1)
if layer_norm:
self.layer_norm(self.actor_fc1, std=1.0)
self.layer_norm(self.actor_fc2, std=1.0)
self.layer_norm(self.actor_fc2_1, std=1.0)
self.layer_norm(self.actor_fc3, std=0.01)
self.layer_norm(self.critic_fc1, std=1.0)
self.layer_norm(self.critic_fc2, std=1.0)
self.layer_norm(self.critic_fc3, std=1.0)
@staticmethod
def layer_norm(layer, std=1.0, bias_const=0.0):
torch.nn.init.orthogonal_(layer.weight, std)
torch.nn.init.constant_(layer.bias, bias_const)
def forward(self, states):
"""
run policy network (actor) as well as value network (critic)
:param states: a Tensor2 represents states
:return: 3 Tensor2
"""
action_mean, action_logstd = self._forward_actor(states)
critic_value = self._forward_critic(states)
return action_mean, action_logstd, critic_value
def _forward_actor(self, states):
x = F.leaky_relu(self.actor_fc1(states))
x = F.leaky_relu(self.actor_fc2(x))
x = F.leaky_relu(self.actor_fc2_1(x))
action_mean = self.actor_fc3(x)
action_logstd = self.actor_logstd.expand_as(action_mean)
assert action_logstd.mean() == 0
return action_mean, action_logstd
def _forward_critic(self, states):
x = torch.tanh(self.critic_fc1(states))
x = torch.tanh(self.critic_fc2(x))
critic_value = self.critic_fc3(x)
return critic_value
def select_action(self,
action_mean,
action_logstd,
return_logproba=True,
factor=1.0):
"""
given mean and std, sample an action from normal(mean, std)
also returns probability of the given chosen
"""
action_std = torch.exp(action_logstd) * factor
action = torch.normal(action_mean, action_std)
if return_logproba:
logproba = self._normal_logproba(action, action_mean,
action_logstd, action_std)
return action, logproba
@staticmethod
def _normal_logproba(x, mean, logstd, std=None):
if std is None:
std = torch.exp(logstd)
std_sq = std.pow(2)
logproba = -0.5 * math.log(
2 * math.pi) - logstd - (x - mean).pow(2) / (2 * std_sq)
return logproba.sum(1)
def get_logproba(self, states, actions):
"""
return probability of chosen the given actions under corresponding states of current network
:param states: Tensor
:param actions: Tensor
"""
action_mean, action_logstd = self._forward_actor(states)
logproba = self._normal_logproba(actions, action_mean, action_logstd)
return logproba