-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·263 lines (218 loc) · 8.57 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/usr/bin/python
# coding: utf-8
from pprint import pprint
from pylab import *
from collections import defaultdict
from lib.function_recognition import FunctionRecogniter
from lib.function_data import function_data
from lib.plotter import Plotter
def predict_example(fd, recogniter):
"""
全ての関数から得たデータをnetwrokに入力, 関数を予測する.
最終的に, 関数毎の平均正解率と入力に対する正解率を表示する.
neuronの選択性が得られているかを表示.
"""
plotter = Plotter()
result = defaultdict(list)
plotter.initialize({
'xy_value':{
'ylim': [0,100],
'sub_title': ['value']},
'likelihood':{
'ylim': [0,1],
'sub_title': fd.function_list.keys()},
}, movable=False)
for ftype in fd.function_list.keys():
print ftype
data = fd.get_data(ftype)
for x, y in data:
input_data = {
'xy_value': [x, y],
'x_value': x,
'y_value': y,
'ftype': None
}
inferences = recogniter.run(input_data, learn=False)
# print
input_data['ftype'] = ftype
recogniter.print_inferences(input_data, inferences)
# for result summary
tmp = inferences[ "classifier_" + recogniter.selectivity]['likelihoodsDict'][ftype]
result[ftype].append(tmp)
# for plot
plotter.write(title="xy_value", x_value={'value': x}, y_value={'value': y})
plotter.write(title="likelihood", y_value=inferences[ "classifier_" + recogniter.selectivity]['likelihoodsDict'])
plotter.show(save_dir='./docs/images/multi_layer/', file_name='2layer-'+ftype+'.png')
plotter.reset()
# write result summary
import numpy
print '### result'
for title , data in result.items():
print title , " : ",
print numpy.mean(data)
# print evaluation summary
for name in recogniter.dest_resgion_data.keys():
print '### ', name
recogniter.evaluation[name].print_summary()
def predict_example_2(fd, recogniter):
"""
データはランダムに選択された関数から取得してnetwrokに入力.
関数を確率を表示する.
"""
plotter_2 = Plotter()
result = defaultdict(list)
plotter_2.initialize({
'xy_value':{
'ylim': [0,100],
'sub_title': ['value']},
'likelihood':{
'ylim': [0,1],
'sub_title': fd.function_list.keys()},
}, movable=True)
#for ftype in fd.function_list.keys():
print '################"'
for idx in range(100):
ftype = fd.romdom_choice()
data = fd.get_data(ftype)
for x, y in data:
input_data = {
'xy_value': [x, y],
'x_value': x,
'y_value': y,
'ftype': None
}
inferences = recogniter.run(input_data, learn=False)
# print
input_data['ftype'] = ftype
recogniter.print_inferences(input_data, inferences)
tmp = inferences[ "classifier_" + recogniter.selectivity]['likelihoodsDict']
plotter_2.write_draw(title='xy_value', x_value={'value': x + 100 * idx}, y_value={'value': y})
plotter_2.write_draw(title='likelihood', x_value=dict([(sub, x + 100 * idx) for sub in tmp.keys()]), y_value=tmp)
def predict_example_3(fd, recogniter):
"""
各層の統計的特徴を比較する.
1. 各層のclassifier結果のgraph表示.
2.
"""
plotter = Plotter()
result = defaultdict(lambda: defaultdict(list))
plotter.initialize({
# 'selectivity_center':{
# 'ylim': [0,100],
# 'sub_title': recogniter.dest_resgion_data.keys() },
# 'selectivity_outside':{
# 'ylim': [0,100],
# 'sub_title': recogniter.dest_resgion_data.keys() },
'xy_value':{
'ylim': [0,100],
'sub_title': ['value']},
'likelihood':{
'ylim': [0,1],
'sub_title': recogniter.dest_resgion_data.keys() },
}, movable=False)
for ftype in fd.function_list.keys():
print ftype
data = fd.get_data(ftype)
for x, y in data:
input_data = {
'xy_value': [x, y],
'x_value': x,
'y_value': y,
'ftype': None
}
inferences = recogniter.run(input_data, learn=False)
# print
input_data['ftype'] = ftype
recogniter.print_inferences(input_data, inferences)
# for result summary
for name in recogniter.dest_resgion_data.keys():
tmp = inferences[ "classifier_" + name ]['likelihoodsDict'][ftype]
result[name][ftype].append(tmp)
# for plot
plotter.write(title="xy_value", x_value={'value': x}, y_value={'value': y})
tmp = {}
for name in recogniter.dest_resgion_data.keys():
class_name = "classifier_" + name
tmp[name] = inferences[class_name]['likelihoodsDict'][ftype]
plotter.write(title="likelihood", y_value=tmp)
# # for plot
# x_tmp = {}
# y_tmp = {}
# for name in recogniter.dest_resgion_data.keys():
# x_tmp[name] = recogniter.evaluation[name].get_selectivity()[ftype]['x']
# y_tmp[name] = recogniter.evaluation[name].get_selectivity()[ftype]['y']
# plotter.add(title="selectivity_center", x_values=x_tmp, y_values=y_tmp)
#
# x_tmp2 = {}
# y_tmp2 = {}
# for name in recogniter.dest_resgion_data.keys():
# x_tmp2[name] = recogniter.evaluation_2[name].get_selectivity()[ftype]['x']
# y_tmp2[name] = recogniter.evaluation_2[name].get_selectivity()[ftype]['y']
# plotter.add(title="selectivity_outside", x_values=x_tmp2, y_values=y_tmp2)
plotter.show(save_dir='./docs/images/multi_layer/', file_name='each-layer-'+ftype+'.png')
plotter.reset()
# write result summary
import numpy
print '### result'
for name, datas in result.items():
print '#### ', name
for title ,data in datas.items():
print title , " : ",
print numpy.mean(data)
# print evaluation summary
for name in recogniter.dest_resgion_data.keys():
print '### ', name
recogniter.evaluation[name].print_summary()
def main():
fd = function_data()
recogniter = FunctionRecogniter()
# トレーニング
# learn_layer = ['region1', 'region2', 'region3']
#for learn_layer in [['region1', 'region2'], ['region1', 'region2']]:
for learn_layer in [['region1'], ['region2'],]:
for i in range(25):
print i,
for num, ftype in enumerate(fd.function_list.keys()):
data = fd.get_data(ftype)
for x, y in data:
input_data = {
'xy_value': [x, y],
'x_value': x,
'y_value': y,
'ftype': ftype
}
inferences = recogniter.run(input_data, learn=True, learn_layer=learn_layer)
# print
recogniter.print_inferences(input_data, inferences)
recogniter.reset()
# 予測
predict_example(fd, recogniter)
#predict_example_2(fd, recogniter)
predict_example_3(fd, recogniter)
# # 予測2, fixed-sin
# import numpy
# print
# print "fiexed-sin"
# fd.function_list['sin'] = lambda x: numpy.sin(x * 2 * numpy.pi/fd.max_x) * 50 + 50
# data = fd.get_data('sin')
# for x, y in data:
# input_data = {
# 'xy_value': [x, y]
# }
# inferences = recogniter.predict(input_data)
# print 'sin', inferences['best']['value'], inferences['best']['prob'], inferences["anomaly"]
#
#
# # 予測3, fixed-sin
# print
# print "fiexed-sin"
# fd.function_list['sin'] = lambda x: numpy.sin(x * 4 * numpy.pi/fd.max_x) * 30 + 50
# data = fd.get_data('sin')
# for x, y in data:
# input_data = {
# 'xy_value': [x, y]
# }
# inferences = recogniter.predict(input_data)
# print 'sin', inferences['best']['value'], inferences['best']['prob'], inferences["anomaly"]
if __name__ == "__main__":
main()