-
Notifications
You must be signed in to change notification settings - Fork 5
/
ga_model.py
116 lines (96 loc) · 3.51 KB
/
ga_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import mkl
mkl.set_num_threads(1)
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.nn.functional as F
import random
import numpy as np
def step(env, *args):
state, a, b, c = env.step(*args)
state = convert_state(state)
return state, a, b, c
def reset(env):
return convert_state(env.reset())
def convert_state(state):
import cv2
return cv2.resize(cv2.cvtColor(state, cv2.COLOR_RGB2GRAY), (64, 64)) / 255.0
class Model(nn.Module):
def __init__(self, rng_state):
super().__init__()
# TODO: padding?
self.conv1 = nn.Conv2d(4, 32, (8, 8), 4)
self.conv2 = nn.Conv2d(32, 64, (4, 4), 2)
self.conv3 = nn.Conv2d(64, 64, (3, 3), 1)
self.dense = nn.Linear(4*4*64, 512)
self.out = nn.Linear(512, 18)
self.rng_state = rng_state
torch.manual_seed(rng_state)
self.evolve_states = []
self.add_tensors = {}
for name, tensor in self.named_parameters():
if tensor.size() not in self.add_tensors:
self.add_tensors[tensor.size()] = torch.Tensor(tensor.size())
if 'weight' in name:
nn.init.kaiming_normal(tensor)
else:
tensor.data.zero_()
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.relu(self.conv2(x))
x = F.relu(self.conv3(x))
x = x.view(1, -1)
x = F.relu(self.dense(x))
return self.out(x)
def evolve(self, sigma, rng_state):
torch.manual_seed(rng_state)
self.evolve_states.append((sigma, rng_state))
for name, tensor in sorted(self.named_parameters()):
to_add = self.add_tensors[tensor.size()]
to_add.normal_(0.0, sigma)
tensor.data.add_(to_add)
def compress(self):
return CompressedModel(self.rng_state, self.evolve_states)
def uncompress_model(model):
start_rng, other_rng = model.start_rng, model.other_rng
m = Model(start_rng)
for sigma, rng in other_rng:
m.evolve(sigma, rng)
return m
def random_state():
return random.randint(0, 2**31-1)
class CompressedModel:
def __init__(self, start_rng=None, other_rng=None):
self.start_rng = start_rng if start_rng is not None else random_state()
self.other_rng = other_rng if other_rng is not None else []
def evolve(self, sigma, rng_state=None):
self.other_rng.append((sigma, rng_state if rng_state is not None else random_state()))
def evaluate_model(env, model, max_eval=20000, max_noop=30):
import gym
env = gym.make(env)
model = uncompress_model(model)
noops = random.randint(0, max_noop)
cur_states = [reset(env)] * 4
total_reward = 0
for _ in range(noops):
cur_states.pop(0)
new_state, reward, is_done, _ = step(env, 0)
total_reward += reward
if is_done:
return total_reward
cur_states.append(new_state)
total_frames = 0
model.eval()
for _ in range(max_eval):
total_frames += 4
values = model(Variable(torch.Tensor([cur_states])))[0]
action = np.argmax(values.data.numpy()[:env.action_space.n])
print(action)
new_state, reward, is_done, _ = step(env, action)
total_reward += reward
if is_done:
break
cur_states.pop(0)
cur_states.append(new_state)
print('\t', total_reward)
return total_reward, total_frames