-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoccer_model.py
69 lines (47 loc) · 1.97 KB
/
soccer_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
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.distributions import Categorical
def layer_init(layer, w_scale=1.0):
nn.init.orthogonal_(layer.weight.data)
layer.weight.data.mul_(w_scale)
nn.init.constant_(layer.bias.data, 0)
return layer
class ActorModel(nn.Module):
def __init__(self, state_size, action_size, fc1_units=256, fc2_units=128):
super(ActorModel, self).__init__()
self.fc1 = layer_init( nn.Linear(state_size, fc1_units) )
self.fc2 = layer_init( nn.Linear(fc1_units, fc2_units) )
self.fc_action = layer_init( nn.Linear(fc2_units, action_size) )
def forward(self, state, action=None):
x = F.relu( self.fc1(state) )
x = F.relu( self.fc2(x) )
probs = F.softmax( self.fc_action(x), dim=1 )
dist = Categorical( probs )
if action is None:
action = dist.sample()
log_prob = dist.log_prob( action )
entropy = dist.entropy()
return action, log_prob, entropy
def load(self, checkpoint):
if os.path.isfile(checkpoint):
self.load_state_dict(torch.load(checkpoint))
def checkpoint(self, checkpoint):
torch.save(self.state_dict(), checkpoint)
class CriticModel(nn.Module):
def __init__(self, state_size, fc1_units=256, fc2_units=128):
super(CriticModel, self).__init__()
self.fc1 = layer_init( nn.Linear(state_size, fc1_units) )
self.fc2 = layer_init( nn.Linear(fc1_units, fc2_units) )
self.fc_critic = layer_init( nn.Linear(fc2_units, 1) )
def forward(self, state):
x = F.relu( self.fc1(state) )
x = F.relu( self.fc2(x) )
value = self.fc_critic(x)
return value
def load(self, checkpoint):
if os.path.isfile(checkpoint):
self.load_state_dict(torch.load(checkpoint))
def checkpoint(self, checkpoint):
torch.save(self.state_dict(), checkpoint)