-
Notifications
You must be signed in to change notification settings - Fork 0
/
reinforce.py
124 lines (101 loc) · 3.24 KB
/
reinforce.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
import torch
from simulator.core.network import Network
from simulator.core.reward_manager import RewardManager
from simulator.model.connection import *
from simulator.model.group import *
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import torch.cuda
import random
use_cuda = False and torch.cuda.is_available()
def main():
net = Network(log_limit=500)
rm = RewardManager(net, tau_d=20.0)
g1 = LeakyIFGroup(
network=net,
name="g1",
size=40,
cuda=use_cuda,
excitatory=True
# threshold=25.0,
# a=0.02,
# b=0.2,
# c=-65.0,
# d=2.0
)
g2 = LeakyIFGroup(
network=net,
name="inh",
size=10,
cuda=use_cuda,
excitatory=False
# a=0.1,
# d=2.0,
# c=-60.0,
# reset=-45.0,
# threshold=-40.0
)
g1r = RSTDP(g1, g1, p=1, cuda=use_cuda, nu=1e-1)
g1g2 = MSTDP(g1, g2, p=1, cuda=use_cuda, nu=1e-1)
g2g1 = MSTDP(g2, g1, p=1, cuda=use_cuda, nu=1e-1)
fig = plt.figure(num=10)
fig.set_size_inches(w=5, h=5)
ax = fig.add_subplot(111)
ax.matshow(g1r.w.cpu().numpy())
ax.set_title('Group 1 connection weights matrix')
ax.set_xlabel("Weight")
ax.set_ylabel("Neuron")
fig.savefig('plots/g1r_rp_initial.png')
fig.clf()
net.step()
while net.time < 1000 * 25:
if use_cuda:
input = torch.cuda.FloatTensor(g1.size).uniform_() * 4
else:
input = torch.rand(g1.size) * 4
input[21] += 10
input[22] += 10
input[23] += 10
input[24] += 10
input[25] += 10
net.step({'g1': input})
if g1.cache[-1][19] > 0:
net.reinforce(-1)
# elif g1.cache[-1][20] > 0 and any(g1.cache[-2][x] > 0 for x in [18, 19]):
# net.reinforce(-0.01)
if g1.cache[-1][20] > 0:
net.reinforce(1)
if int(net.time) % 1000 == 0:
group_1_activity = torch.stack(g1.cache).cpu().numpy().transpose()
fig = plt.figure(num=10)
fig.set_size_inches(w=15, h=3)
ax = fig.add_subplot(111)
ax.matshow(group_1_activity)
ax.set_title('Group 1 activity')
fig.savefig('plots/group_1_activity.png')
fig.clf()
if int(net.time) % 1000 == 0:
x = g1r.w.cpu().numpy().transpose()
x[0, 20] = 1
fig = plt.figure(num=10)
fig.set_size_inches(w=5, h=5)
ax = fig.add_subplot(111)
ax.matshow(x)
ax.set_title('Group 1 connection weights matrix')
ax.set_xlabel("Weight")
ax.set_ylabel("Neuron")
fig.savefig('plots/rp_reinforced.png')
fig.clf()
print(int(net.time / 1000), "seconds of simulation")
# plt.matshow(g1r.w.cpu().numpy())
# plt.title('Group 1 connection weights matrix')
# plt.savefig('plots/g1r.png')
# plt.close()
group_1_activity = torch.stack(g1.cache).cpu().numpy().transpose()
plt.matshow(group_1_activity)
plt.title('Group 1 activity')
plt.savefig('plots/group_1_activity.png')
plt.close()
if __name__ == '__main__':
main()