-
Notifications
You must be signed in to change notification settings - Fork 119
/
player_util.py
executable file
·83 lines (76 loc) · 2.7 KB
/
player_util.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
from __future__ import division
import os
os.environ["OMP_NUM_THREADS"] = "1"
import torch
import torch.nn.functional as F
from torch.autograd import Variable
class Agent(object):
def __init__(self, model, env, args, state):
self.model = model
self.env = env
self.state = state
self.hx = None
self.cx = None
self.eps_len = 0
self.args = args
self.values = []
self.log_probs = []
self.rewards = []
self.entropies = []
self.done = True
self.info = None
self.reward = 0
self.gpu_id = -1
self.hidden_size = args.hidden_size
def action_train(self):
value, logit, self.hx, self.cx = self.model(
self.state.unsqueeze(0), self.hx, self.cx
)
prob = F.softmax(logit, dim=1)
log_prob = F.log_softmax(logit, dim=1)
entropy = -(log_prob * prob).sum(1)
self.entropies.append(entropy)
action = prob.multinomial(1).data
log_prob = log_prob.gather(1, action)
state, self.reward, self.done, self.info = self.env.step(
action.item())
if self.gpu_id >= 0:
with torch.cuda.device(self.gpu_id):
self.state = torch.from_numpy(state).float().cuda()
else:
self.state = torch.from_numpy(state).float()
self.eps_len += 1
self.reward = max(min(self.reward, 1), -1)
self.values.append(value)
self.log_probs.append(log_prob)
self.rewards.append(self.reward)
return self
def action_test(self):
with torch.no_grad():
if self.done:
if self.gpu_id >= 0:
with torch.cuda.device(self.gpu_id):
self.cx = torch.zeros(1, self.hidden_size).cuda()
self.hx = torch.zeros(1, self.hidden_size).cuda()
else:
self.cx = torch.zeros(1, self.hidden_size)
self.hx = torch.zeros(1, self.hidden_size)
value, logit, self.hx, self.cx = self.model(
self.state.unsqueeze(0), self.hx, self.cx
)
prob = F.softmax(logit, dim=1)
action = prob.cpu().numpy().argmax()
state, self.reward, self.done, self.info = self.env.step(action)
if self.gpu_id >= 0:
with torch.cuda.device(self.gpu_id):
self.state = torch.from_numpy(state).float().cuda()
else:
self.state = torch.from_numpy(state).float()
self.eps_len += 1
return self
def clear_actions(self):
self.values = []
self.log_probs = []
self.rewards = []
self.entropies = []
return self