-
Notifications
You must be signed in to change notification settings - Fork 0
/
SingleNeuronRL.py
172 lines (157 loc) · 4.59 KB
/
SingleNeuronRL.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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 23 19:38:16 2020
@author: kevin
"""
import numpy as np
import scipy as sp
import matplotlib.pyplot as plt
from dotmap import DotMap
import seaborn as sns
color_names = ["windows blue", "red", "amber", "faded green"]
colors = sns.xkcd_palette(color_names)
sns.set_style("white")
sns.set_context("talk")
# %% functions
def Activation(w,x,nu):
"""
Response nonlinearity of the single neuron
"""
S = WeightedSum(w,x)
if S+nu>0:
y = +1
else:
y = -1
return y
def WeightedSum(w,x):
"""
Linear sum of the weighted patterns
"""
return np.dot(w,x)
def Est_y_s(w,x,T):
"""
Estimated expected response given weight, pattern, and effective temperature
"""
S = WeightedSum(w,x)
Est = (np.exp(S/T)-1)/(np.exp(S/T)+1)
return Est
def WeightChange(r,y,x,w,pars):
"""
Associative reward-penalty learning algorithm
Given learning parameters, pattern x, response y, and reward r, return weight change with RL rule
"""
lam, rho, T = pars
Est = Est_y_s(w,x,T)
if r==+1:
dw = rho*(r*y - Est)*x
elif r==-1:
dw = lam*rho*(r*y - Est)*x
# else:
# dw = 0
return dw
def Environment(x,y,dxy):
"""
Given pattern x and response y, feedback reward according to environment dxy (with lookup table of P(reward))
"""
patterns = dxy.x #list of N patterns
probs = dxy.p #probabilities Nx2 (for +1 and -1)
pos = FindPattern(x, patterns) #find the matching pattern
if y==+1:
prob = probs[pos,1]
if prob>np.random.rand():
rt = +1
else:
rt = -1
elif y==-1:
prob = probs[pos,0]
if prob>np.random.rand():
rt = +1
else:
rt = -1
return rt
def FindPattern(x,patterns):
"""
Given a list of array patterns, return the index in the list that has the matching pattern to x
"""
for pi,pp in enumerate(patterns):
comparison = x == pp
equal_arrays = comparison.all()
if equal_arrays == True:
pos = pi
return pos
# %% parameters
#learning parameters
lam = 0.01
rho = 0.5
T = 0.15
pars = lam, rho, T
nu = 0.001
#trials structures
trials = 3000
seqs = 200
seq = np.random.choice(2,seqs)
Rs = np.zeros(trials)
#environment setting
P = 2 #two patterns for now
patterns = np.array([1,0]), np.array([1,1])
Pr = np.array([[0.6, 0.9],\
[0.4, 0.2]])
dxy = DotMap()
dxy.x = patterns
dxy.p = Pr
#learning
w = np.zeros(P) #np.random.randn(2)
for tt in range(trials):
#initialize for trial
reward = 0 #used for reward counting
w_ = np.zeros(P) #template used for batch update
for ss in range(seqs):
x = patterns[np.random.choice(P)] #[seq[ss]] # #randomly pick pattern
y = Activation(w, x, np.random.randn()*nu) #measure activity
rt = Environment(x, y, dxy) #compute reward
dw = WeightChange(rt, y, x, w, pars) #update weights
w_ = w_ + dw
if rt>0:
reward = reward + rt
w = w + w_
Rs[tt] = reward/seqs #recording the probability of getting reward
plt.figure()
plt.plot(Rs)
print(w)
# %% record stochastic dynamics
def optimal_policy(x,dxy):
patterns = dxy.x #list of N patterns
probs = dxy.p #probabilities Nx2 (for +1 and -1)
pos = FindPattern(x, patterns) #find the matching pattern
opt_y = np.argmax(probs[pos,:])
if opt_y==0:
y = -1
elif opt_y==1:
y = +1
return y
ws = np.zeros((P,trials)) #record weights
dws = np.zeros((P,trials)) #record for forcing
match = np.zeros(trials) #a target and a result
#learning
w = np.zeros(P) #np.random.randn(2)
for tt in range(trials):
#initialize for trial
reward = 0 #used for reward counting
w_ = np.zeros(P) #template used for batch update
ys = np.zeros(seqs)
opt_y = np.zeros(seqs)
for ss in range(seqs):
x = patterns[np.random.choice(P)] #[seq[ss]] # #randomly pick pattern
y = Activation(w, x, np.random.randn()*nu) #measure activity
rt = Environment(x, y, dxy) #compute reward
dw = WeightChange(rt, y, x, w, pars) #update weights
w_ = w_ + dw
if rt>0:
reward = reward + rt
ys[ss] = y
opt_y[ss] = optimal_policy(x,dxy)
w = w + w_
ws[:,tt] = w
dws[:,tt] = w_
match[tt] = np.dot(ys,opt_y)/seqs
Rs[tt] = reward/seqs #recording the probability of getting reward