FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
code/code/gpuarn.py at master · rmlopes/code · GitHub
rmlopes
code
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
code
/
code
/
gpuarn.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
305 lines (266 loc) · 11.3 KB
Breadcrumbs
code
/
code
/
gpuarn.py
Copy path
File metadata and controls
305 lines (266 loc) · 11.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import
ConfigParser
,
os
import
logging
import
random
import
numpy
as
np
from
time
import
clock
from
numpy
import
array
as
nparray
from
functools
import
partial
from
bitstring
import
*
from
math
import
exp
import
sys
from
sys
import
stdout
from
subprocess
import
call
from
utils
import
*
from
utils
.
bitstrutils
import
*
from
arn
import
*
import
gpukernel
from
pycuda
import
gpuarray
,
driver
as
drv
try
:
import
matplotlib
.
pyplot
as
plt
from
matplotlib
import
collections
,
legend
except
ImportError
:
logging
.
warning
(
'matplotlib not found.'
)
logging
.
warning
(
'use of silentmode = 0 will result in an error...'
)
log
=
logging
.
getLogger
(
__name__
)
TFACTORS
=
0
STRUCTS
=
1
def
iterate
(
arnet
,
samplerate
,
simtime
,
silentmode
,
simstep
,
delta
,
**
kwargs
):
s
=
clock
()
time
=
0
nump
=
arnet
.
numtf
numsamples
=
1
numrec
=
arnet
.
numrec
numeff
=
arnet
.
numeff
cuweights_e
=
arnet
.
gpu_eweights
cuweights_i
=
arnet
.
gpu_iweights
#print kwargs['inputs']
#cuccs = gpuarray.to_gpu(arnet.ccs)
while
time
<
simtime
:
#for i in range(numrec):
# arnet.ccs[nump+i] = kwargs['inputs'][i]
_update
(
arnet
.
proteins
,
arnet
.
ccs
,
cuweights_e
,
cuweights_i
,
delta
,
**
kwargs
)
#normalize ccs, ignoring receptors
#print arnet.ccs[:nump]
#cuccs.get(arnet.ccs)
totparcels
=
(
arnet
.
ccs
[:
nump
].
tolist
()
+
arnet
.
ccs
[
nump
+
numrec
:].
tolist
())
arnet
.
ccs
/=
sum
(
totparcels
)
#cuccs.get(arnet.ccs)
#enforce again input/receptor cc
#for i in range(numrec):
# arnet.ccs[nump+i] = kwargs['inputs'][i]
if
time
%
int
(
simtime
*
samplerate
)
==
0
:
log
.
debug
(
'TIME: '
+
str
(
time
))
arnet
.
cchistory
=
np
.
column_stack
((
arnet
.
cchistory
,
arnet
.
ccs
[:
nump
]))
arnet
.
receptorhist
=
np
.
column_stack
((
arnet
.
receptorhist
,
arnet
.
ccs
[
nump
:
nump
+
numrec
]))
arnet
.
effectorhist
=
np
.
column_stack
((
arnet
.
effectorhist
,
arnet
.
ccs
[
nump
+
numrec
:]))
time
+=
simstep
print
'Elapsed time: %f sec.'
%
(
clock
()
-
s
,)
return
arnet
def
_update
(
proteins
,
cuccs
,
exciteweights
,
inhibitweights
,
delta
,
**
kwargs
):
#print ccs.shape
#print cuccs.shape
esignals
=
_getSignalArrayCUDA
(
cuccs
[:
len
(
exciteweights
)],
exciteweights
,
**
kwargs
)
isignals
=
_getSignalArrayCUDA
(
cuccs
[:
len
(
inhibitweights
)],
inhibitweights
,
**
kwargs
)
deltas
=
esignals
-
isignals
#print deltas.shape
#print cuccs.shape
deltas
*=
np
.
float32
(
delta
)
#kwargs['esignals'] *= np.float32(delta)
deltas
*=
cuccs
#kwargs['esignals'] = kwargs['esignals'] * cuccs
#total = sum(ccs)+sum(deltas)
cuccs
+=
deltas
#ccs/=total
def
_getSignalArrayCUDA
(
ccs
,
weightstable
,
**
kwargs
):
#print ccs.shape
#print weightstable.shape[1]
out
=
gpuarray
.
to_gpu
(
np
.
zeros
(
weightstable
.
shape
[
1
],
dtype
=
np
.
float32
))
cuccs
=
gpuarray
.
to_gpu
(
ccs
)
#print signals.shape
#fun = gpukernel.getkernel(16,weightstable.shape[1])
kwargs
[
'gpudot'
](
out
,
cuccs
,
weightstable
,
np
.
int32
(
weightstable
.
shape
[
0
]),
np
.
int32
(
weightstable
.
shape
[
1
]))
return
np
.
float32
(
1.0
/
len
(
ccs
))
*
out
.
get
()
#return np.float32(1.0/len(ccs)) *
def
_getprotein
(
idx
,
code
,
bind_size
,
gene_size
,
protein_size
):
signature
=
BitStream
(
bin
=
applymajority
(
code
[
bind_size
*
3
:
bind_size
*
3
+
gene_size
],
protein_size
))
#EXTENDED version - Weak linkage (needs double size gene/proteins)
#p = [code[:self.bind_size],
# code[self.bind_size:self.bind_size*2],
# signature[0:self.bind_size],
# signature[self.bind_size:self.protein_size]]
#ORIGINAL version
p
=
[
idx
,
code
[:
bind_size
],
code
[
bind_size
:
bind_size
*
2
],
signature
,
signature
]
log
.
debug
(
p
)
return
p
def
_getweights
(
bindings
,
bindingsize
,
beta
,
**
kwargs
):
weights
=
bindings
-
bindingsize
weights
*=
beta
return
np
.
exp
(
weights
)
#organized in columns for the target equation
def
getbindings
(
bindtype
,
proteins
,
match_threshold
,
**
kwargs
):
return
nparray
([[
XORmatching
(
p
[
3
],
otherps
[
1
+
bindtype
],
match_threshold
)
for
otherps
in
proteins
]
for
p
in
proteins
],
dtype
=
np
.
float32
)
class
ARNetwork
:
def
__init__
(
self
,
gcode
,
config
,
**
kwargs
):
self
.
code
=
gcode
self
.
simtime
=
config
.
getint
(
'default'
,
'simtime'
)
promfun
=
bindparams
(
config
,
buildpromlist
)
productsfun
=
bindparams
(
config
,
buildproducts
)
self
.
promlist
=
promfun
(
gcode
)
self
.
proteins
=
productsfun
(
gcode
,
self
.
promlist
)
self
.
effectors
=
[]
self
.
effectorproms
=
promfun
(
gcode
,
promoter
=
'00000000'
)
if
self
.
effectorproms
:
#print 'EFFECTORS:', self.effectorproms
self
.
effectors
=
productsfun
(
gcode
,
self
.
effectorproms
)
self
.
receptors
=
[]
self
.
receptorproms
=
promfun
(
gcode
,
promoter
=
'11111111'
)
if
self
.
receptorproms
:
#print 'RECEPTORS:', self.receptorproms
self
.
receptors
=
productsfun
(
gcode
,
self
.
receptorproms
)
pbindfun
=
bindparams
(
config
,
getbindings
)
weightsfun
=
bindparams
(
config
,
_getweights
)
prob
=
kwargs
[
'problem'
]
self
.
numtf
=
len
(
self
.
proteins
)
self
.
numeff
=
min
(
len
(
self
.
effectors
),
prob
.
nout
)
self
.
effectors
=
self
.
effectors
[:
self
.
numeff
]
self
.
numrec
=
min
(
len
(
self
.
receptors
),
prob
.
ninp
)
self
.
receptors
=
self
.
receptors
[:
self
.
numrec
]
self
.
ccs
=
[]
if
self
.
promlist
:
self
.
ccs
=
nparray
([
1.0
/
(
self
.
numtf
+
self
.
numeff
+
self
.
numrec
)]
*
(
self
.
numtf
+
self
.
numeff
+
self
.
numrec
),
dtype
=
np
.
float32
)
self
.
_initializehistory
()
self
.
_initializebindings
(
pbindfun
)
self
.
_initializeweights
(
weightsfun
)
self
.
dot_
=
gpukernel
.
getkernel
(
22
,
self
.
eweights
.
shape
[
1
])
self
.
esignals
=
gpuarray
.
to_gpu
(
np
.
zeros
(
self
.
eweights
.
shape
[
1
],
dtype
=
np
.
float32
))
self
.
isignals
=
gpuarray
.
to_gpu
(
np
.
zeros
(
self
.
iweights
.
shape
[
1
],
dtype
=
np
.
float32
))
for
i
in
range
(
len
(
self
.
proteins
)):
self
.
proteins
[
i
].
append
(
self
.
ccs
[
i
])
self
.
simfun
=
bindparams
(
config
,
iterate
)
self
.
delta
=
config
.
getfloat
(
'default'
,
'delta'
)
def
_initializebindings
(
self
,
pbindfun
):
self
.
ebindings
=
pbindfun
(
0
,
self
.
proteins
+
self
.
receptors
+
self
.
effectors
)
self
.
ibindings
=
pbindfun
(
1
,
self
.
proteins
+
self
.
receptors
+
self
.
effectors
)
#effectors and dummy receptors do not regulate
if
self
.
effectors
or
self
.
receptors
:
self
.
ebindings
=
self
.
ebindings
[:
self
.
numtf
+
self
.
numrec
,:]
#print 'ebinds: ', self.ebindings.shape
self
.
ibindings
=
self
.
ibindings
[:
self
.
numtf
+
self
.
numrec
,:]
#print 'ibinds: ', self.ibindings.shape
def
_initializeweights
(
self
,
weightsfun
):
self
.
eweights
=
weightsfun
(
self
.
ebindings
)
#print 'ebinds: ', self.eweights.shape
self
.
iweights
=
weightsfun
(
self
.
ibindings
)
#print 'ebinds: ', self.iweights.shape
self
.
gpu_eweights
=
gpuarray
.
to_gpu
(
self
.
eweights
)
self
.
gpu_iweights
=
gpuarray
.
to_gpu
(
self
.
iweights
)
def
_initializehistory
(
self
):
self
.
cchistory
=
nparray
(
self
.
ccs
[:
self
.
numtf
])
self
.
receptorhist
=
nparray
(
self
.
ccs
[
self
.
numtf
:
self
.
numtf
+
self
.
numrec
])
self
.
effectorhist
=
nparray
(
self
.
ccs
[
self
.
numtf
+
self
.
numrec
:])
def
reset
(
self
):
self
.
_initializehistory
()
self
.
ccs
=
nparray
([
1.0
/
(
self
.
numtf
+
self
.
numeff
+
self
.
numrec
)]
*
(
self
.
numtf
+
self
.
numeff
+
self
.
numrec
))
def
__str__
(
self
):
return
str
(
self
.
proteins
)
def
simulate
(
self
,
*
inputs
):
if
not
inputs
:
inputs
=
[
.0
]
*
self
.
numrec
#print self.numtf
#print self.numrec
#print str(len(self.ccs) - (self.numtf+self.numrec))
if
self
.
simtime
>
0
:
self
.
simfun
(
self
,
inputs
=
inputs
,
gpudot
=
self
.
dot_
,
esignals
=
self
.
esignals
,
isignals
=
self
.
isignals
)
for
i
in
range
(
self
.
numtf
):
self
.
proteins
[
i
][
-
1
]
=
self
.
ccs
[
i
]
#for i in range(len(self.effectors)):
# self.effectors[i][-1] = self.ccs[i+self.numtf+self.numrec]
def
stepsimulate
(
self
,
proteins
,
ccs
):
_updatenonorm
(
proteins
,
ccs
,
self
.
eweights
,
self
.
iweights
,
self
.
delta
)
return
ccs
def
nstepsim
(
self
,
n
=
1000
,
*
inputs
):
self
.
simfun
(
self
,
simtime
=
n
,
numeffectors
=
self
.
numeff
,
inputs
=
inputs
)
for
i
in
range
(
len
(
self
.
proteins
)):
self
.
proteins
[
i
][
-
1
]
=
self
.
ccs
[
i
]
################################################################################
### Test ########
################################################################################
if
__name__
==
'__main__'
:
_usecuda_
=
True
class
Problem
:
pass
p
=
Problem
()
p
.
ninp
=
3
p
.
nout
=
1
arnconfigfile
=
'../configfiles/arnsimlong.cfg'
saveto
=
'genome.save'
log
.
setLevel
(
logging
.
DEBUG
)
cfg
=
ConfigParser
.
ConfigParser
()
cfg
.
readfp
(
open
(
arnconfigfile
))
proteins
=
[]
nump
=
0
prob_inp
=
[
.0
,
.0
,
.0
]
try
:
f
=
open
(
sys
.
argv
[
1
],
'r'
)
genome
=
BitStream
(
bin
=
f
.
readline
())
arnet
=
ARNetwork
(
genome
,
cfg
,
problem
=
p
)
saveto
=
sys
.
argv
[
1
]
except
:
while
nump
<
200
or
nump
>
512
or
numeff
==
0
or
not
arnet
.
receptors
:
genome
=
BitStream
(
float
=
random
.
random
(),
length
=
32
)
for
i
in
range
(
cfg
.
getint
(
'default'
,
'initdm'
)):
genome
=
dm_event
(
genome
,
.02
)
arnet
=
ARNetwork
(
genome
,
cfg
,
problem
=
p
)
nump
=
len
(
arnet
.
promlist
)
print
nump
numeff
=
len
(
arnet
.
effectors
)
#f = open(saveto,'w')
#f.write(genome.bin)
#f.close
#exit(0)
#s = clock()
arnet
.
simulate
()
#print clock() - s
if
not
cfg
.
getint
(
'default'
,
'silentmode'
):
displayARNresults
(
arnet
.
proteins
,
arnet
.
cchistory
,
cfg
.
getfloat
(
'default'
,
'samplerate'
),
temp
=
0
)
extralabels
=
[
'R'
]
*
arnet
.
numrec
+
[
'E'
]
*
arnet
.
numeff
struct_prots
=
arnet
.
receptors
+
arnet
.
effectors
hist
=
np
.
vstack
((
arnet
.
receptorhist
,
arnet
.
effectorhist
))
displayARNresults
(
struct_prots
,
hist
,
cfg
.
getfloat
(
'default'
,
'samplerate'
),
temp
=
1
,
extralabels
=
extralabels
)
for
p
in
arnet
.
proteins
:
print
p
print
'effectors: '
,
arnet
.
effectors
f
=
open
(
saveto
,
'w'
)
f
.
write
(
genome
.
bin
)
f
.
close
#print genome.bin
Back
|
FazBrowse Home
|
New Git URL