forked from dgriff777/rl_a3c_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player_util.py
executable file
·78 lines (73 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
from __future__ import division
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
def action_train(self):
value, logit, (self.hx, self.cx) = self.model((Variable(
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, Variable(action))
state, self.reward, self.done, self.info = self.env.step(
action.cpu().numpy())
self.state = torch.from_numpy(state).float()
if self.gpu_id >= 0:
with torch.cuda.device(self.gpu_id):
self.state = self.state.cuda()
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 = Variable(
torch.zeros(1, 512).cuda())
self.hx = Variable(
torch.zeros(1, 512).cuda())
else:
self.cx = Variable(torch.zeros(1, 512))
self.hx = Variable(torch.zeros(1, 512))
else:
self.cx = Variable(self.cx.data)
self.hx = Variable(self.hx.data)
value, logit, (self.hx, self.cx) = self.model((Variable(
self.state.unsqueeze(0)), (self.hx, self.cx)))
prob = F.softmax(logit, dim=1)
action = prob.max(1)[1].data.cpu().numpy()
state, self.reward, self.done, self.info = self.env.step(action[0])
self.state = torch.from_numpy(state).float()
if self.gpu_id >= 0:
with torch.cuda.device(self.gpu_id):
self.state = self.state.cuda()
self.eps_len += 1
return self
def clear_actions(self):
self.values = []
self.log_probs = []
self.rewards = []
self.entropies = []
return self