-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai_utils.py
178 lines (154 loc) · 5.8 KB
/
ai_utils.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import matplotlib.pyplot as plt
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
import numpy as np
from configuration_paramaters import RESULTS_DIR
#Dynamic chart generation class
class PlotDynamicUpdate():
active=True
def define_labels(self,labels, secondline=False):
self.x_label=labels[0]
self.y_label=labels[1]
self.title=labels[2]
self.second_y_label=labels[3]
self.secondLine=secondline
def on_launch(self):
#Set up plot
plt.ion()
self.previous_backend=plt.get_backend()
plt.switch_backend("TkAgg")
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot([],[], 'r-')
#Autoscale
self.ax.set_autoscaley_on(True)
self.ax.set_autoscalex_on(True)
#Other stuff
self.ax.grid()
self.ax.set_xlabel(self.x_label)
self.ax.set_ylabel(self.y_label)
self.ax.set_title(self.title)
if(self.secondLine):
self.ax.set_ylabel(self.y_label,color="tab:red")
self.ax.tick_params(axis='y', labelcolor="tab:red")
self.ax2=self.ax.twinx()
self.ax2.set_ylabel(self.second_y_label,color="tab:blue")
self.lines2=self.ax2.plot([],[], 'b-')
self.ax2.tick_params(axis='y', labelcolor="tab:blue")
def on_running(self, xdata, ydata, y2data):
if(self.active):
#Update data (with the new _and_ the old points)
self.lines.set_xdata(xdata)
self.lines.set_ydata(ydata)
#Need both of these in order to rescale
self.ax.relim()
self.ax.autoscale_view()
if(self.secondLine):
self.lines2[0].set_xdata(xdata)
self.lines2[0].set_ydata(y2data)
self.ax2.relim()
self.ax2.autoscale_view()
#We need to draw *and* flush
try:
self.figure.canvas.draw()
self.figure.canvas.flush_events()
except:
self.active=False
def on_close(self,rewards,losses=[]):
plt.ioff()
plt.close()
plt.switch_backend(self.previous_backend)
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot(range(1, len(rewards)+1),rewards, 'r-')
#Autoscale
self.ax.set_autoscaley_on(True)
self.ax.set_autoscalex_on(True)
#Other stuff
self.ax.grid()
self.ax.set_xlabel(self.x_label)
self.ax.set_ylabel(self.y_label)
self.ax.set_title(self.title)
if(self.secondLine):
self.ax.set_ylabel(self.y_label,color="tab:red")
self.ax.tick_params(axis='y', labelcolor="tab:red")
self.ax2=self.ax.twinx()
self.ax2.set_ylabel(self.second_y_label,color="tab:blue")
self.lines2=self.ax2.plot(range(1, len(losses)+1),losses, 'b-')
self.ax2.tick_params(axis='y', labelcolor="tab:blue")
plt.show()
def on_close_draw(self,rewards,losses=[]):
self.figure, self.ax = plt.subplots()
self.lines, = self.ax.plot(range(1, len(rewards)+1),rewards, 'r-')
#Autoscale
self.ax.set_autoscaley_on(True)
self.ax.set_autoscalex_on(True)
#Other stuff
self.ax.grid()
self.ax.set_xlabel(self.x_label)
self.ax.set_ylabel(self.y_label)
self.ax.set_title(self.title)
if(self.secondLine):
self.ax.set_ylabel(self.y_label,color="tab:red")
self.ax.tick_params(axis='y', labelcolor="tab:red")
self.ax2=self.ax.twinx()
self.ax2.set_ylabel(self.second_y_label,color="tab:blue")
self.lines2=self.ax2.plot(range(1, len(losses)+1),losses, 'b-')
self.ax2.tick_params(axis='y', labelcolor="tab:blue")
if not os.path.exists(RESULTS_DIR):
os.makedirs(RESULTS_DIR)
plt.savefig(RESULTS_DIR+"/"+self.title+'_results.png')
plt.close()
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class Average_movements:
def __init__(self, size):
self.list_of_rewards = []
self.size = size
def add(self, rewards):
if isinstance(rewards, list):
self.list_of_rewards += rewards
else:
self.list_of_rewards.append(rewards)
while len(self.list_of_rewards) > self.size:
del self.list_of_rewards[0]
def average(self):
return np.mean(self.list_of_rewards)
class CNN(nn.Module):
def __init__(self, cnn_layers, dnn_layers):
super(CNN, self).__init__()
self.convolution = nn.ModuleList(cnn_layers)
self.fullyconnected=nn.ModuleList(dnn_layers)
def count_neurons(self, image_dim):
x = Variable(torch.rand(1, *image_dim))
for layer in self.convolution:
x = layer(x)
size = x.data.view(1, -1).size(1)
return size
def forward(self, x):
for layer in self.convolution:
x = layer(x)
for layer in self.fullyconnected:
x = layer(x)
return x
# Selector class used to pick an output from a tensor
class SoftmaxSelector(nn.Module):
def __init__(self, T):
super(SoftmaxSelector, self).__init__()
self.T = T
def forward(self, outputs):
probs = F.softmax(outputs * self.T,dim=1)
actions = probs.multinomial(1)
return actions
# Puts together the Network and the Final action selector
class AI:
def __init__(self, nn, selector):
self.nn = nn
self.selector = selector
def __call__(self, inputs):
input = Variable(torch.from_numpy(np.array(inputs, dtype=np.float32)))
output = self.nn(input)
actions = self.selector(output)
return actions.data.numpy()