-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDmodel.jl
181 lines (153 loc) · 5.37 KB
/
TDmodel.jl
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
#=
Julia code for the Temporal-Difference (TD) model of classical conditioning.
As specified in:
Sutton, R.S., Barto, A.G. (1990) "Time-Derivative Models of Pavlovian
Reinforcement," in Learning and Computational Neuroscience: Foundations
of Adaptive Networks, M. Gabriel and J. Moore, Eds., pp. 497--537.
MIT Press. http://incompleteideas.net/papers/sutton-barto-90.pdf
This code was written by Amir Samani, based on the C++ program written by
Rich Sutton for the same purposes. April 4, 2019
=#
using LinearAlgebra
using Plots
mutable struct Experiment
num_stimuli::UInt32 # number of stimuli, including US
α::Float16 # TD model of Classical Conditioning params
β::Float16
γ::Float16
δ::Float16
t::UInt32 # current time step in the experiment
Vbar_prev_t::Float64
V::Array
Z::Array # Trace vector
end
function v_bar(V, X)
value = dot(V', X)
return value >= 0 ? value : 0
end
function steps(num_steps, X, λ, ep::Experiment)
Vbar_t = 0
alpha_beta_error = 0
for i = 1:num_steps
Vbar_t = v_bar(ep.V, X)
alpha_beta_error = ep.α * ep.β * (λ + ep.γ*Vbar_t - ep.Vbar_prev_t)
ep.t += 1
ep.V += alpha_beta_error * ep.Z
ep.Z += ep.δ * (X - ep.Z)
ep.Vbar_prev_t = v_bar(ep.V, X)
end
end
function fig_tes()
background = zeros(2,1)
background[1] = 1.0
CS_and_background = ones(2,1)
ep = Experiment(2,0.1,1.0,0.95,0.2,0,0,zeros(2,1),zeros(2,1))
steps(100,background,0.0,ep)
for i = 1:20
steps(1,background,1.0,ep)
steps(2-1,background,0.0,ep)
steps(4,CS_and_background,0.0,ep)
steps(100,background,0.0,ep)
print("v_back: ");print(ep.V[1]);print(" v_cs: ");print(ep.V[2]);
println(" ")
end
end
function figure_19()
background = zeros(3,1)
background[1] = 1.0
CSp_and_background = ones(3,1)
CSp_and_background[3] = 0.0
CSn_and_background = ones(3,1)
CSn_and_background[2] = 0.0
CSp_and_CSn_and_background = ones(3,1)
ep = Experiment(3,0.1,1.0,0.95,0.2,0,0,zeros(3,1),zeros(3,1))
plot_x_time_steps = []
plot_y_V_csp = []
plot_y_V_csn = []
for i = 1:80
steps(100,background,0.0,ep)
steps(4,CSp_and_background,0.0,ep)
steps(2,background,1.0,ep)
steps(100,background,0.0,ep)
steps(4,CSp_and_CSn_and_background,0.0,ep)
steps(2,background,0.0,ep)
append!(plot_x_time_steps,i)
append!(plot_y_V_csp,ep.V[2])
append!(plot_y_V_csn,ep.V[3])
end
for i = 81:130
steps(100,background,0.0,ep)
steps(4,CSp_and_background,0.0,ep)
steps(2,background,0.0,ep)
steps(100,background,0.0,ep)
steps(4,CSn_and_background,0.0,ep)
steps(2,background,0.0,ep)
append!(plot_x_time_steps,i)
append!(plot_y_V_csp,ep.V[2])
append!(plot_y_V_csn,ep.V[3])
end
plot(plot_x_time_steps,[plot_y_V_csp,plot_y_V_csn],label=["CS+" "CS-"])
end
function figure_20()
background = zeros(3,1)
background[1] = 1.0
CSA_and_background = ones(3,1)
CSA_and_background[3] = 0.0
CSB_and_background = ones(3,1)
CSB_and_background[2] = 0.0
CSA_and_CSB_and_background = ones(3,1)
ep_present = Experiment(3,0.1,1.0,0.95,0.2,0,0,zeros(3,1),zeros(3,1))
ep_absent = Experiment(3,0.1,1.0,0.95,0.2,0,0,zeros(3,1),zeros(3,1))
plot_x_time_steps = []
plot_y_V_csa_bpresent = []
plot_y_V_csa_babsent = []
for i = 1:80
steps(100,background,0.0,ep_absent)
steps(4,CSA_and_background,0.0,ep_absent)
steps(4,background,0.0,ep_absent)
steps(2,background,1.0,ep_absent)
append!(plot_x_time_steps,i)
append!(plot_y_V_csa_babsent,ep_absent.V[2])
end
for i = 1:80
steps(100,background,0.0,ep_present)
steps(4,CSA_and_background,0.0,ep_present)
steps(4,CSB_and_background,0.0,ep_present)
steps(2,background,1.0,ep_present)
append!(plot_y_V_csa_bpresent,ep_present.V[2])
end
plot(plot_x_time_steps,[plot_y_V_csa_babsent,plot_y_V_csa_bpresent],
label=["CSB Absent" "CSB Present"])
end
function figure_21()
background = zeros(3,1)
background[1] = 1.0
CSA_and_background = ones(3,1)
CSA_and_background[3] = 0.0
CSB_and_background = ones(3,1)
CSB_and_background[2] = 0.0
CSA_and_CSB_and_background = ones(3,1)
ep_present = Experiment(3,0.1,1.0,0.95,0.2,0,0,zeros(3,1),zeros(3,1))
ep_absent = Experiment(3,0.1,1.0,0.95,0.2,0,0,zeros(3,1),zeros(3,1))
plot_x_time_steps = []
plot_y_V_csb_apresent = []
plot_y_V_csb_aabsent = []
for i = 1:80
steps(100,background,0.0,ep_absent)
steps(4,background,0.0,ep_absent)
steps(4,CSB_and_background,0.0,ep_absent)
steps(2,background,1.0,ep_absent)
append!(plot_x_time_steps,i)
append!(plot_y_V_csb_aabsent,ep_absent.V[3])
end
for i = 1:80
steps(100,background,0.0,ep_present)
steps(4,CSA_and_background,0.0,ep_present)
steps(4,CSA_and_CSB_and_background,0.0,ep_present)
steps(2,background,1.0,ep_present)
append!(plot_y_V_csb_apresent,ep_present.V[3])
end
plot(plot_x_time_steps,[plot_y_V_csb_aabsent,plot_y_V_csb_apresent],
label=["CSA Absent" "CSA Present"])
end
figure_20()