-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbrainfuse.py
194 lines (171 loc) · 7.6 KB
/
brainfuse.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
# file processed by 2to3
from __future__ import print_function, absolute_import
from builtins import map, filter, range
import numpy
from numpy import *
import os
from fann2 import libfann
__all__ = ['libfann', 'brainfuse', 'activateNets', 'activateNetsFile', 'activateMergeNets']
class brainfuse(libfann.neural_net):
def __init__(self,filename,**kw):
libfann.neural_net.__init__(self)
self.filename=filename
self.scale_mean_in=None
self.scale_mean_out=None
self.scale_deviation_in=None
self.scale_deviation_out=None
self.inputNames=[]
self.outputNames=[]
self.norm_output=[]
self.MSE=None
self.load()
def denormOutputs(self,inputs,outputs):
return self.normOutputs(inputs,outputs,denormalize=True)
def normOutputs(self, inputs, outputs, denormalize=False):
for ko,itemo in enumerate(self.outputNames):
norm=1.
for ki,itemi in enumerate(self.inputNames):
norm*=(inputs[:,ki]**self.norm_output[ko][ki])
#print('*normalize %s with %s'%(itemo,itemi))
if isinstance(norm,ndarray):
if not denormalize:
#print('*apply normalization on %s: %s'%(itemo,repr(self.norm_output[ko])))
outputs[:,ko]/=norm
else:
#print('*apply denormalization')
outputs[:,ko]*=norm
return outputs
def activate(self, dB, verbose=False):
inputs=dB
if isinstance(dB,dict):
inputs=[]
for k in self.inputNames:
inputs.append(dB[k].astype(float))
inputs=array(inputs).T
targets=[]
if isinstance(dB,dict):
for k in self.outputNames:
try:
targets.append(dB[k].astype(float))
except:
if verbose:
print(k+' not in dB')
targets=array(targets).T
out=[]
for k,tmp in enumerate(inputs):
tmp=(tmp-self.scale_mean_in)/self.scale_deviation_in
tmp=array(self.run(tmp))
tmp=tmp*self.scale_deviation_out+self.scale_mean_out
out.append(tmp)
out=array(out)
out=self.denormOutputs(inputs,out)
return out, targets
def __getstate__(self):
self.save()
tmp={'filename':self.filename}
return tmp
def __setstate__(self,tmp):
self.__init__(tmp['filename'])
def save(self):
super(libfann.neural_net, self).save(self.filename)
with open(self.filename,'Ur') as f:
original=f.readlines()
tmp=[]
for line in original:
if 'neurons' in line and self.scale_mean_in is not None:
tmp.append('scale_included=1')
tmp.append('scale_mean_in='+' '.join(['%6.6f'%k for k in self.scale_mean_in]))
tmp.append('scale_deviation_in='+' '.join(['%6.6f'%k for k in self.scale_deviation_in]))
tmp.append('scale_new_min_in='+' '.join(['%6.6f'%-1.0]*self.get_num_input()))
tmp.append('scale_factor_in='+' '.join(['%6.6f'%1.0]*self.get_num_input()))
tmp.append('scale_mean_out='+' '.join(['%6.6f'%k for k in self.scale_mean_out]))
tmp.append('scale_deviation_out='+' '.join(['%6.6f'%k for k in self.scale_deviation_out]))
tmp.append('scale_new_min_out='+' '.join(['%6.6f'%-1.0]*self.get_num_output()))
tmp.append('scale_factor_out='+' '.join(['%6.6f'%1.0]*self.get_num_output()))
tmp.append(line.strip())
elif 'scale_' not in line or self.scale_mean_in is None:
tmp.append(line.strip())
if len(self.inputNames):
tmp.append('input_names='+' '.join([repr(k) for k in self.inputNames]))
if len(self.outputNames):
tmp.append('output_names='+' '.join([repr(k) for k in self.outputNames]))
if len(self.norm_output):
tmp.append('norm_output='+' '.join([repr(float(k)) for k in array(self.norm_output).flatten()]))
if self.MSE is not None:
tmp.append('MSE='+' '+str(self.MSE))
with open(self.filename,'w') as f:
f.write('\n'.join(tmp))
#print('\n'.join(tmp))
def load(self):
if not os.stat(self.filename).st_size:
return self
super(libfann.neural_net, self).create_from_file(self.filename)
with open(self.filename,'Ur') as f:
tmp=f.readlines()
for line in tmp:
line=line.rstrip()
if 'input_names=' in line:
self.inputNames=eval((line.replace('input_names=','[')+']').replace("' '","','"))
elif 'output_names=' in line:
self.outputNames=eval((line.replace('output_names=','[')+']').replace("' '","','"))
elif 'norm_output=' in line:
tmp=array(eval((line.replace('norm_output=','[')+']').replace(" ",",")))
self.norm_output=reshape(tmp,(len(self.outputNames),len(self.inputNames)))
elif 'MSE=' in line:
what,value=line.strip().split('=')
self.MSE=eval(value)
elif 'scale_mean_in' in line:
what,value=line.strip().split('=')
self.scale_mean_in=array(eval('['+','.join(value.split(' '))+']'))
elif 'scale_mean_out' in line:
what,value=line.strip().split('=')
self.scale_mean_out=array(eval('['+','.join(value.split(' '))+']'))
elif 'scale_deviation_in' in line:
what,value=line.strip().split('=')
self.scale_deviation_in=array(eval('['+','.join(value.split(' '))+']'))
elif 'scale_deviation_out' in line:
what,value=line.strip().split('=')
self.scale_deviation_out=array(eval('['+','.join(value.split(' '))+']'))
return self
def activateNets(nets, dB):
net=list(nets.values())[0]
out_=empty((len(dB[list(dB.keys())[0]]),len(net.outputNames),len(nets)))
for k,n in enumerate(nets):
out_[:,:,k],targets=nets[n].activate(dB)
out=mean(out_,-1)
sut=std(out_,-1)
return out,sut,targets,nets,out_
def activateNetsFile(nets, inputFile, targetFile=None):
net=list(nets.values())[0]
dB={}
for k in net.inputNames:
dB[k]=[]
for line in open(inputFile,'Ur').readlines()[1:]:
for k,item in enumerate(line.split()):
dB[net.inputNames[k]].append( float(item))
if targetFile is not None:
for k in net.outputNames:
dB[k]=[]
for line in open(targetFile,'Ur').readlines()[1:]:
for k,item in enumerate(line.split()):
dB[net.outputNames[k]].append( float(item))
for k in net.inputNames:
dB[k]=array(dB[k])
return activateNets(nets,dB)
def activateMergeNets(nets, dB, merge_nets):
net=list(nets.values())[0]
out_0=empty((len(dB[list(dB.keys())[0]]),len(net.outputNames),len(nets)))
index=net.inputNames.index(merge_nets)
centers=empty(len(nets))
merge_norm=empty(len(nets))
for k,n in enumerate(nets):
out_0[:,:,k],targets=nets[n].activate(dB)
centers[k]=nets[n].scale_mean_in[index]
merge_norm[k]=nets[n].scale_deviation_in[index]
w=dB[merge_nets][:,newaxis]-centers[newaxis,:]
w=exp(-(w/merge_norm[newaxis,:])**2)
w=w/numpy.sum(w,1)[newaxis,:].T
out_=out_0*w[:,newaxis,:]
out=numpy.sum(out_,-1)
sut=sqrt(numpy.sum(w[:,newaxis,:]*(out_0-out[:,:,newaxis])**2,-1))
return out,sut,targets,nets,out_0