Skip to content

Commit

Permalink
Added learning examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tcstewar committed Apr 12, 2017
1 parent ed9b227 commit ab13adf
Show file tree
Hide file tree
Showing 12 changed files with 952 additions and 0 deletions.
31 changes: 31 additions & 0 deletions groningen_2017/day5/learning1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# The basic simplest learning model

import nengo
import numpy as np

model = nengo.Network()
with model:
stim = nengo.Node(lambda t: np.sin(t*2*np.pi))

pre = nengo.Ensemble(n_neurons=100, dimensions=1)
nengo.Connection(stim, pre)

post = nengo.Ensemble(n_neurons=100, dimensions=1)

def init_func(x):
return 0
learn_conn = nengo.Connection(pre, post, function=init_func,
learning_rule_type=nengo.PES())

error = nengo.Ensemble(n_neurons=100, dimensions=1)

def desired_func(x):
# adjust this to change what function is learned
return x
nengo.Connection(stim, error, function=desired_func, transform=-1)
nengo.Connection(post, error, transform=1)

nengo.Connection(error, learn_conn.learning_rule)

stop_learn = nengo.Node(1)
nengo.Connection(stop_learn, error.neurons, transform=-10*np.ones((100,1)))
86 changes: 86 additions & 0 deletions groningen_2017/day5/learning1.py.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
_viz_0 = nengo_gui.components.Value(error)
_viz_config[_viz_0].synapse = 0.01
_viz_config[_viz_0].max_value = 1
_viz_config[_viz_0].min_value = -1
_viz_config[_viz_0].legend_labels = [u'label_0']
_viz_config[_viz_0].height = 0.09945749762260893
_viz_config[_viz_0].label_visible = False
_viz_config[_viz_0].width = 0.13535502958579881
_viz_config[_viz_0].show_legend = False
_viz_config[_viz_0].y = 0.12640144665461045
_viz_config[_viz_0].x = 0.7374539466339157
_viz_1 = nengo_gui.components.SpikeGrid(error)
_viz_config[_viz_1].label_visible = False
_viz_config[_viz_1].width = 0.053994077197193395
_viz_config[_viz_1].height = 0.07052440539837745
_viz_config[_viz_1].y = 0.04651913654294851
_viz_config[_viz_1].x = 0.3766730527468894
_viz_2 = nengo_gui.components.SpikeGrid(pre)
_viz_config[_viz_2].label_visible = False
_viz_config[_viz_2].width = 0.07174555648713422
_viz_config[_viz_2].height = 0.07233273056057866
_viz_config[_viz_2].y = 0.5365280289330894
_viz_config[_viz_2].x = 0.36171430166350366
_viz_3 = nengo_gui.components.Value(pre)
_viz_config[_viz_3].synapse = 0.01
_viz_config[_viz_3].max_value = 1
_viz_config[_viz_3].min_value = -1
_viz_config[_viz_3].legend_labels = [u'label_0']
_viz_config[_viz_3].height = 0.18083182640144665
_viz_config[_viz_3].label_visible = False
_viz_config[_viz_3].width = 0.14792899408284024
_viz_config[_viz_3].show_legend = False
_viz_config[_viz_3].y = 0.7770343580470149
_viz_config[_viz_3].x = 0.4911521714859901
_viz_4 = nengo_gui.components.Value(post)
_viz_config[_viz_4].synapse = 0.01
_viz_config[_viz_4].max_value = 1
_viz_config[_viz_4].min_value = -1
_viz_config[_viz_4].legend_labels = [u'label_0']
_viz_config[_viz_4].height = 0.18083182640144665
_viz_config[_viz_4].label_visible = False
_viz_config[_viz_4].width = 0.14792899408284024
_viz_config[_viz_4].show_legend = False
_viz_config[_viz_4].y = 0.7785966642728792
_viz_config[_viz_4].x = 0.854327307813989
_viz_5 = nengo_gui.components.SpikeGrid(post)
_viz_config[_viz_5].label_visible = False
_viz_config[_viz_5].width = 0.08505917159763314
_viz_config[_viz_5].height = 0.06962023936818348
_viz_config[_viz_5].y = 0.3257443646715493
_viz_config[_viz_5].x = 0.9320358661565868
_viz_6 = nengo_gui.components.Slider(stop_learn)
_viz_config[_viz_6].label_visible = True
_viz_config[_viz_6].width = 0.05472216803646652
_viz_config[_viz_6].x = 0.10734032720326386
_viz_config[_viz_6].y = 0.13626767374861884
_viz_config[_viz_6].max_value = 1
_viz_config[_viz_6].min_value = 0
_viz_config[_viz_6].height = 0.13110306724286208
_viz_7 = nengo_gui.components.Slider(stim)
_viz_config[_viz_7].label_visible = True
_viz_config[_viz_7].width = 0.07396449704142012
_viz_config[_viz_7].x = 0.11066924936319217
_viz_config[_viz_7].y = 0.799961327054948
_viz_config[_viz_7].max_value = 1
_viz_config[_viz_7].min_value = -1
_viz_config[_viz_7].height = 0.18083182640144665
_viz_ace_editor = nengo_gui.components.AceEditor()
_viz_net_graph = nengo_gui.components.NetGraph()
_viz_sim_control = nengo_gui.components.SimControl()
_viz_config[_viz_sim_control].kept_time = 4.0
_viz_config[_viz_sim_control].shown_time = 0.5
_viz_config[error].pos=(0.5381743515986875, 0.1363925542644679)
_viz_config[error].size=(0.09433962264150943, 0.1)
_viz_config[model].pos=(-2.7755575615628914e-17, 0)
_viz_config[model].size=(1, 1)
_viz_config[model].expanded=True
_viz_config[model].has_layout=True
_viz_config[post].pos=(0.8602988843017502, 0.4867986798679869)
_viz_config[post].size=(0.09433962264150943, 0.1)
_viz_config[pre].pos=(0.5215704588993711, 0.4867760012890966)
_viz_config[pre].size=(0.09433962264150943, 0.1)
_viz_config[stim].pos=(0.11359991811388985, 0.4785478547854787)
_viz_config[stim].size=(0.07547169811320754, 0.08)
_viz_config[stop_learn].pos=(0.24661892317256237, 0.13403171934986233)
_viz_config[stop_learn].size=(0.028489029476249428, 0.0290271132376396)
1 change: 1 addition & 0 deletions groningen_2017/day5/learning2-conditioning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Classical conditioning# There are three different unconditioned stimuli (US) that are provided# to the model, one after the other. Each has a different hardwired# unconditioned response (UR).# There is also a conditioned stimulus (CS) provided, and there is a different# one for each US. The model attempts to learn to trigger the correct# conditioned response (CR) in response to the CS. # After learning, the CR should start to respond before the corresponding UR.import nengoimport numpy as npD = 3N = D*50model = nengo.Network(label="My Network")with model: def us_stim(t): # cycle through the three US t = t % 3 if 0.9 < t< 1: return [1, 0, 0] if 1.9 < t< 2: return [0, 1, 0] if 2.9 < t< 3: return [0, 0, 1] return [0, 0, 0] us_stim = nengo.Node(us_stim) def cs_stim(t): # cycle through the three CS t = t % 3 if 0.7 < t< 1: return [1, 0, 0] if 1.7 < t< 2: return [0, 1, 0] if 2.7 < t< 3: return [0, 0, 1] return [0, 0, 0] cs_stim = nengo.Node(cs_stim) us = nengo.Ensemble(N, D) cs = nengo.Ensemble(N*2, D*2) nengo.Connection(us_stim, us[:D]) nengo.Connection(cs_stim, cs[:D]) nengo.Connection(cs[:D], cs[D:], synapse=0.2) ur = nengo.Ensemble(N, D) nengo.Connection(us, ur) cr = nengo.Ensemble(N, D) learn_conn = nengo.Connection(cs, cr, function=lambda x: [0]*D) learn_conn.learning_rule_type = nengo.PES(learning_rate=3e-4) error = nengo.Ensemble(N, D) nengo.Connection(error, learn_conn.learning_rule) nengo.Connection(ur, error, transform=-1) nengo.Connection(cr, error, transform=1, synapse=0.1) stop_learn = nengo.Node([1]) nengo.Connection(stop_learn, error.neurons, transform=-10*np.ones((N, 1)))
Expand Down
88 changes: 88 additions & 0 deletions groningen_2017/day5/learning2-conditioning.py.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
_viz_0 = nengo_gui.components.Value(us_stim)
_viz_config[_viz_0].synapse = 0.01
_viz_config[_viz_0].max_value = 1
_viz_config[_viz_0].min_value = -1
_viz_config[_viz_0].legend_labels = [u'label_0', u'label_1', u'label_2']
_viz_config[_viz_0].height = 0.33548396878846254
_viz_config[_viz_0].label_visible = True
_viz_config[_viz_0].width = 0.4957209110107955
_viz_config[_viz_0].show_legend = False
_viz_config[_viz_0].y = -0.02555910612184552
_viz_config[_viz_0].x = 1.700039848878386
_viz_1 = nengo_gui.components.Value(ur)
_viz_config[_viz_1].synapse = 0.01
_viz_config[_viz_1].max_value = 1
_viz_config[_viz_1].min_value = -1
_viz_config[_viz_1].legend_labels = [u'label_0', u'label_1', u'label_2']
_viz_config[_viz_1].height = 0.32182079943792036
_viz_config[_viz_1].label_visible = True
_viz_config[_viz_1].width = 0.495144778456076
_viz_config[_viz_1].show_legend = False
_viz_config[_viz_1].y = 0.5733259602606832
_viz_config[_viz_1].x = 1.7147639769541727
_viz_2 = nengo_gui.components.Value(cs_stim)
_viz_config[_viz_2].synapse = 0.01
_viz_config[_viz_2].max_value = 1
_viz_config[_viz_2].min_value = -1
_viz_config[_viz_2].legend_labels = [u'label_0', u'label_1', u'label_2']
_viz_config[_viz_2].height = 0.3080052838044119
_viz_config[_viz_2].label_visible = True
_viz_config[_viz_2].width = 0.4870583465273344
_viz_config[_viz_2].show_legend = False
_viz_config[_viz_2].y = 1.7277954081858138
_viz_config[_viz_2].x = 1.751852596980785
_viz_3 = nengo_gui.components.Slider(stop_learn)
_viz_config[_viz_3].label_visible = True
_viz_config[_viz_3].width = 0.1547748292929397
_viz_config[_viz_3].x = -0.3916735861372964
_viz_config[_viz_3].y = 0.8953981450355386
_viz_config[_viz_3].max_value = 1
_viz_config[_viz_3].min_value = 0
_viz_config[_viz_3].height = 0.31007340556684215
_viz_4 = nengo_gui.components.Value(error)
_viz_config[_viz_4].synapse = 0.01
_viz_config[_viz_4].max_value = 1
_viz_config[_viz_4].min_value = -1
_viz_config[_viz_4].legend_labels = [u'label_0', u'label_1', u'label_2']
_viz_config[_viz_4].height = 0.2257152344298681
_viz_config[_viz_4].label_visible = True
_viz_config[_viz_4].width = 0.20143166395797582
_viz_config[_viz_4].show_legend = False
_viz_config[_viz_4].y = 0.5627173081804691
_viz_config[_viz_4].x = 0.10709213631277122
_viz_5 = nengo_gui.components.Value(cr)
_viz_config[_viz_5].synapse = 0.01
_viz_config[_viz_5].max_value = 1
_viz_config[_viz_5].min_value = -1
_viz_config[_viz_5].legend_labels = [u'label_0', u'label_1', u'label_2']
_viz_config[_viz_5].height = 0.3039418895589711
_viz_config[_viz_5].label_visible = True
_viz_config[_viz_5].width = 0.4990954016884383
_viz_config[_viz_5].show_legend = False
_viz_config[_viz_5].y = 1.1351110887586613
_viz_config[_viz_5].x = 1.7173993354555648
_viz_ace_editor = nengo_gui.components.AceEditor()
_viz_net_graph = nengo_gui.components.NetGraph()
_viz_sim_control = nengo_gui.components.SimControl()
_viz_config[_viz_sim_control].kept_time = 4
_viz_config[_viz_sim_control].shown_time = 0.5
_viz_config[cr].pos=(0.92363314826799, 1.2555342516494816)
_viz_config[cr].size=(0.1, 0.1)
_viz_config[cs].pos=(-0.07265676167966198, 1.5904674747609822)
_viz_config[cs].size=(0.08197448923850104, 0.11875791389680282)
_viz_config[cs_stim].pos=(-0.4373586893881203, 1.5370666962614927)
_viz_config[cs_stim].size=(0.06557959139080084, 0.09500633111744225)
_viz_config[error].pos=(0.48578680503416993, 1.0028755763063855)
_viz_config[error].size=(0.08197448923850104, 0.11875791389680282)
_viz_config[model].pos=(0.585874641705596, 0.4922791654943769)
_viz_config[model].size=(0.3399166770891136, 0.3399166770891136)
_viz_config[model].expanded=True
_viz_config[model].has_layout=True
_viz_config[stop_learn].pos=(0.058760108602561846, 0.9812679671654226)
_viz_config[stop_learn].size=(0.06557959139080084, 0.09500633111744225)
_viz_config[ur].pos=(0.8681629327651732, 0.6460127315459735)
_viz_config[ur].size=(0.1, 0.1)
_viz_config[us].pos=(-0.014138006217292345, 0.15947489505857995)
_viz_config[us].size=(0.08197448923850104, 0.11875791389680282)
_viz_config[us_stim].pos=(-0.4373586893881203, 0.15947489505858004)
_viz_config[us_stim].size=(0.06557959139080084, 0.09500633111744225)
71 changes: 71 additions & 0 deletions groningen_2017/day5/learning3-consolidation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# Cortical consolidation

# If you have a slow, complex neural model with lots of internal steps,
# you may want to have its output be used to train up a fast system that
# directly approximates the desired function.

# Here, we have a slow mapping from pre->wm->target. In this case, it is
# slow due to very long time constants, but in general this could also be
# something that requires a few steps through the basal ganglia or some other
# complex system.

# A direct connection from pre->post is trained using the error signal from
# the slow system. It should learn to produce the correct output, and it
# will actually end up being faster than the original.

# You can change the function being computed by the slow system by adjusting
# the context slider. If you change it, the fast system should learn the
# new function instead.

import nengo
import numpy as np

model = nengo.Network()
with model:
def stim_pulse(t):
return np.sin(t)
index = int(t / 1.0)
values = [1, 0, -1, 0]
return values[index % len(values)]
pre_value = nengo.Node(stim_pulse)

tau_slow = 0.2

pre = nengo.Ensemble(100, 1)
post = nengo.Ensemble(100, 1)
target = nengo.Ensemble(100, 1)
nengo.Connection(pre_value, pre)

conn = nengo.Connection(pre, post, function=lambda x: np.random.random(),
learning_rule_type=nengo.PES())

wm = nengo.Ensemble(300, 2, radius=1.4)
context = nengo.Node(1)
nengo.Connection(context, wm[1])
nengo.Connection(pre, wm[0], synapse=tau_slow)

nengo.Connection(wm, target, synapse=tau_slow,
function=lambda x: x[0]*x[1])

error = nengo.Ensemble(n_neurons=100, dimensions=1)
nengo.Connection(post, error, synapse=tau_slow*2, transform=1)
nengo.Connection(target, error, transform=-1)

nengo.Connection(error, conn.learning_rule)

stop_learn = nengo.Node([1])
nengo.Connection(stop_learn, error.neurons, transform=-10*np.ones((100,1)))

both = nengo.Node(None, size_in=2)
nengo.Connection(post, both[0], synapse=None)
nengo.Connection(target, both[1], synapse=None)










76 changes: 76 additions & 0 deletions groningen_2017/day5/learning3-consolidation.py.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
_viz_0 = nengo_gui.components.Value(both)
_viz_config[_viz_0].synapse = 0.01
_viz_config[_viz_0].max_value = 1
_viz_config[_viz_0].min_value = -1
_viz_config[_viz_0].legend_labels = [u'post', u' target']
_viz_config[_viz_0].height = 0.24419889502762432
_viz_config[_viz_0].label_visible = True
_viz_config[_viz_0].width = 0.1584601611459266
_viz_config[_viz_0].show_legend = True
_viz_config[_viz_0].y = 0.553558822538534
_viz_config[_viz_0].x = 0.7263304875096088
_viz_1 = nengo_gui.components.Slider(context)
_viz_config[_viz_1].label_visible = True
_viz_config[_viz_1].width = 0.05767012687427912
_viz_config[_viz_1].x = 0.04440599769319495
_viz_config[_viz_1].y = 0.6434737871070638
_viz_config[_viz_1].max_value = 1
_viz_config[_viz_1].min_value = -1
_viz_config[_viz_1].height = 0.1594896331738437
_viz_2 = nengo_gui.components.Value(pre)
_viz_config[_viz_2].synapse = 0.01
_viz_config[_viz_2].max_value = 1
_viz_config[_viz_2].min_value = -1
_viz_config[_viz_2].legend_labels = [u'label_0']
_viz_config[_viz_2].height = 0.1140323255621075
_viz_config[_viz_2].label_visible = True
_viz_config[_viz_2].width = 0.09630711433384245
_viz_config[_viz_2].show_legend = False
_viz_config[_viz_2].y = 0.08105292702445056
_viz_config[_viz_2].x = 0.1433083533736154
_viz_3 = nengo_gui.components.Slider(stop_learn)
_viz_config[_viz_3].label_visible = True
_viz_config[_viz_3].width = 0.04325258635594633
_viz_config[_viz_3].x = 0.2741508568306297
_viz_config[_viz_3].y = 0.4097467552611884
_viz_config[_viz_3].max_value = 1
_viz_config[_viz_3].min_value = 0
_viz_config[_viz_3].height = 0.09728865189985796
_viz_4 = nengo_gui.components.Value(error)
_viz_config[_viz_4].synapse = 0.01
_viz_config[_viz_4].max_value = 1
_viz_config[_viz_4].min_value = -1
_viz_config[_viz_4].legend_labels = [u'label_0']
_viz_config[_viz_4].height = 0.08117443868739206
_viz_config[_viz_4].label_visible = True
_viz_config[_viz_4].width = 0.08798017348203221
_viz_config[_viz_4].show_legend = False
_viz_config[_viz_4].y = 0.5465713930679981
_viz_config[_viz_4].x = 0.4574769204109885
_viz_ace_editor = nengo_gui.components.AceEditor()
_viz_net_graph = nengo_gui.components.NetGraph()
_viz_sim_control = nengo_gui.components.SimControl()
_viz_config[_viz_sim_control].kept_time = 4.0
_viz_config[_viz_sim_control].shown_time = 0.5
_viz_config[both].pos=(0.560094831022105, 0.5408902000106459)
_viz_config[both].size=(0.1, 0.1)
_viz_config[context].pos=(0.042483660130718956, 0.870967741935484)
_viz_config[context].size=(0.0261437908496732, 0.05161290322580646)
_viz_config[error].pos=(0.3566435263346475, 0.5425043141220942)
_viz_config[error].size=(0.0326797385620915, 0.06451612903225808)
_viz_config[model].pos=(0.06343713956170703, 0.04465709728867622)
_viz_config[model].size=(1.0, 1.0)
_viz_config[model].expanded=True
_viz_config[model].has_layout=True
_viz_config[post].pos=(0.48190082684848146, 0.23797911200288133)
_viz_config[post].size=(0.0326797385620915, 0.06451612903225808)
_viz_config[pre].pos=(0.14811419727980515, 0.2357797749214648)
_viz_config[pre].size=(0.0326797385620915, 0.06451612903225808)
_viz_config[pre_value].pos=(0.05286428296808919, 0.24226989761794498)
_viz_config[pre_value].size=(0.0261437908496732, 0.05161290322580646)
_viz_config[stop_learn].pos=(0.26684498865124223, 0.5401533047550865)
_viz_config[stop_learn].size=(0.016293643688451154, 0.017127071823204408)
_viz_config[target].pos=(0.4533230020725094, 0.8294451686255208)
_viz_config[target].size=(0.0326797385620915, 0.06451612903225808)
_viz_config[wm].pos=(0.25124951941560936, 0.8582085712815761)
_viz_config[wm].size=(0.0326797385620915, 0.06451612903225808)
Loading

0 comments on commit ab13adf

Please sign in to comment.