-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneuralmath
More file actions
108 lines (93 loc) · 3.47 KB
/
Copy pathneuralmath
File metadata and controls
108 lines (93 loc) · 3.47 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 25 11:33:36 2019
@author: thausmann
"""
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
import time
class NN():
A_t1 = []
A_t2 = []
V = []
Ws = []
def propagate(self):
A_t1 = self.A_t1
A_t2 = self.A_t2
V = self.V
Ws = self.Ws
#V[3,1,:,:]+= np.ones(shape=V[3,1,:,:].shape)
for direction in range(len(A_t1.shape)):
np.add(V,Ws[2*direction],V,where=np.roll(A_t1,1,axis=direction))
np.add(V,Ws[2*direction+1],V,where=np.roll(A_t1,-1,axis=direction))
A_t2 = V>0.8
V[A_t2] = 0
A_t1 = A_t2
self.A_t1 = A_t1
self.A_t2 = A_t2
self.V = V
self.condition()
def condition(self):
ALPHA = 1.01
BETA = 1 - 1.5 * (ALPHA - 1)
Ws = self.Ws
for direction in range(len(self.A_t1.shape)):
np.multiply(Ws[2*direction],ALPHA,Ws[2*direction],where=np.logical_and(np.roll(A_t1,1,axis=direction),A_t2))
np.multiply(Ws[2*direction+1],ALPHA,Ws[2*direction+1],where=np.logical_and(np.roll(A_t1,-1,axis=direction),A_t2))
np.multiply(Ws[2*direction],BETA,Ws[2*direction],where=np.logical_and(np.logical_not(np.roll(A_t1,1,axis=direction)),A_t2))
np.multiply(Ws[2*direction+1],BETA,Ws[2*direction+1],where=np.logical_and(np.logical_not(np.roll(A_t1,-1,axis=direction)),A_t2))
self.Ws = Ws
A_t1 = np.array(np.zeros((4,101,101,3)), dtype=bool)
A_t2 = np.array(np.zeros((4,101,101,3)), dtype=bool)
A_t1[:,0,0,1] = True
V = np.zeros(A_t1.shape)
A_reduced = np.array(A_t1.shape)
Ws = [] # setup weights as one offset matrices of A
indeces = [slice(None),slice(None),slice(None),slice(None)]
for direction in range(len(A_t1.shape)):
indeces[direction] = slice(1,A_t1.shape[direction])
Ws.append(np.zeros(shape=A_t1.shape))
Ws[-1][tuple(indeces)] = np.random.normal(loc=0.45,size=A_t1[tuple(indeces)].shape)
Reverse = np.random.normal(size=(Ws[-1].shape))>0
Ws[-1][Reverse] = 0
Reverse = np.logical_not(Reverse)
indeces[direction] = slice(0,A_t1.shape[direction]-1)
Ws.append(np.zeros(shape=A_t1.shape))
Ws[-1][tuple(indeces)] = np.random.normal(loc=0.45,size=A_t1[tuple(indeces)].shape)
Ws[-1][Reverse] = 0
Athena = NN()
Athena.A_t1 = A_t1
Athena.A_t2 = A_t2
Athena.V = V
Athena.Ws = Ws
"""start = time.time()
Amount = 100
for i in range(Amount):
Athena.propagate()
stop = time.time()
print(f'{Amount} in {stop-start}s, meaning {((stop-start)/Amount*1000).__round__(2)}ms per iteration')
"""
fig = plt.figure()
ax = plt.axes()
line = plt.matshow(A_t1[0,:,:,1],fignum=0)
Matrices = np.zeros(shape=A_t1[0,:,:,1].shape)
Weight = np.ones(shape=A_t1[0,:,:,1].shape)
# initialization function: plot the background of each frame
def init():
line.set_data(Athena.A_t1[3,:,:,1])
return line,
# animation function. This is called sequentially
def animate(i):
np.add(Matrices,Weight,Matrices, where=Athena.A_t1[3,:,:,1])
for i in range(1):
Athena.propagate()
Athena.V[:,0,:,:] += 1 if i%2==0 else 0
Athena.V[:,50,:,:] += 1 if i%2==0 else 0
line.set_data(Athena.A_t1[3,:,:,1])
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=80, blit=True)
plt.show()