def test_array(): neurons = 40 net = nef.Network('Array Test', seed=50, command_arguments=sys.argv[2:]) net.make_input('in', np.arange(-1, 1, .34), zero_after_time=1.0) #net.make_input('in', value=1, zero_after=1.0) net.make('B', neurons=neurons, array_size=3, dimensions=2) net.make_array('A', neurons=neurons, array_size=1, dimensions=6) net.make('A2', neurons=neurons, array_size=2, dimensions=3) net.make('B2', neurons=neurons, array_size=6, dimensions=1) net.connect('in', 'A') net.connect('in', 'A2') net.connect('in', 'B') net.connect('in', 'B2') net.connect('A2', 'B') timesteps = 200 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.01 Ip = net.make_probe('in', dt_sample=dt_step, pstc=pstc) Ap = net.make_probe('A', dt_sample=dt_step, pstc=pstc) A2p = net.make_probe('A2', dt_sample=dt_step, pstc=pstc) Bp = net.make_probe('B', dt_sample=dt_step, pstc=pstc) B2p = net.make_probe('B2', dt_sample=dt_step, pstc=pstc) print "starting simulation" net.run(timesteps * dt_step) ip_data = Ip.get_data() ap_data = Ap.get_data() a2p_data = A2p.get_data() bp_data = Bp.get_data() b2p_data = B2p.get_data() print "input 'in' probe data" for x in ip_data: print x print "ensemble 'A' probe data" for x in ap_data: print x print "ensemble 'A2' probe data" for x in a2p_data: print x print "ensemble 'B' probe data" for x in bp_data: print x print "ensemble 'B2' probe data" for x in b2p_data: print x
def test_basalganglia(): import numpy as np # import matplotlib.pyplot as plt import math import sys sys.path.append(sys.argv[1]) import nef_theano as nef net = nef.Network('BG Test', seed=100) def func(x): return [math.sin(x), .5, .2] net.make_input('in', value=func) make(net=net, name='BG', neurons=300, dimensions=3) net.connect('in', 'BG.input', pstc=.01) timesteps = 1000 dt_step = 0.01 # t = np.linspace(dt_step, 1, timesteps) t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.01 Ip = net.make_probe('in', dt_sample=dt_step, pstc=pstc) BGp = net.make_probe('BG.output', dt_sample=dt_step, pstc=pstc) print "starting simulation" net.run(timesteps * dt_step) ip_data = Ip.get_data() bgp_data = BGp.get_data() print "input 'in' probe data" for x in ip_data: print x print "ensemble 'output' probe data" for x in bgp_data: print x
"""Test of Network.make_subnetwork(), which should allow for easy nesting of collections of ensembles. """ import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef net = nef.Network('Main', seed=93, command_arguments=sys.argv[2:]) netA = net.make_subnetwork('A') netB = net.make_subnetwork('B') net.make('X', neurons=50, dimensions=1) netA.make('Y', neurons=50, dimensions=1) netB.make('Z', neurons=50, dimensions=1) netB.make('W', neurons=50, dimensions=1) netB.connect('Z', 'W') # connection within a subnetwork net.connect('X', 'A.Y') # connection into a subnetwork net.connect('A.Y', 'X') # connection out of a subnetwork net.connect('A.Y', 'B.Z') # connection across subnetworks netC = netA.make_subnetwork('C') netC.make('I', neurons=50, dimensions=1) netC.make('J', neurons=50, dimensions=1) netC.connect('I', 'J') # connection within a subsubnetwork net.connect('X', 'A.C.I') # connection into a subsubnetwork
Need to test - 1) inhibitory to ensemble connection - 2) inhibitory to network array connection """ import nef_theano as nef import numpy as np import matplotlib.pyplot as plt neurons = 300 dimensions = 1 array_size = 3 inhib_scale = 10 net = nef.Network('WeightMatrix Test') net.make_input('in1', 1, zero_after=2.5) net.make_input('in2', [1, .5, 0]) net.make('A', neurons=neurons, dimensions=dimensions, intercept=(.1, 1)) net.make('B', neurons=neurons, dimensions=dimensions) # for test 1 net.make('B2', neurons=neurons, dimensions=dimensions, array_size=array_size) # for test 2 # setup inhibitory scaling matrix inhib_scaling_matrix = [[0] * dimensions for i in range(dimensions)] for i in range(dimensions): inhib_scaling_matrix[i][i] = -inhib_scale # setup inhibitory matrix inhib_matrix = [] for i in range(dimensions): inhib_matrix_part = [[inhib_scaling_matrix[i]] * neurons]
"""This is a test file to test the SimpleNode object""" import math import random import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef net = nef.Network('SimpleNode Test', seed=92) class TrainingInput(nef.simplenode.SimpleNode): def init(self): self.input_vals = np.arange(-1, 1, .2) self.period_length = 2 self.choose_time = 0.0 # Note: seeding this random number is necessary for comparing outputs # can be removed otherwise self.random = random.Random() self.random.seed(92) def origin_test1(self): if (self.t >= self.choose_time): # choose an input randomly from the set self.index = self.random.randint(0, 9) if (self.index < 5):
"""This is a test file to test the weight, index_pre, and index_post parameters on the connect function. """ import nef_theano as nef import math import numpy as np import matplotlib.pyplot as plt net=nef.Network('Weight, Index_Pre, and Index_Post Test') net.make_input('in', value=math.sin) net.make('A', 300, 1) net.make('B', 300, 1) net.make('C', 400, 2) net.make('D', 800, 3) net.make('E', 400, 2) net.make('F', 400, 2) net.connect('in', 'A', weight=.5) net.connect('A', 'B', weight=2) net.connect('A', 'C', index_post=1) net.connect('A', 'D') net.connect('C', 'E', index_pre=1) net.connect('C', 'F', index_pre=1, index_post=0) timesteps = 500 Invals = np.zeros((timesteps, 1)) Avals = np.zeros((timesteps, 1)) Bvals = np.zeros((timesteps, 1)) Cvals = np.zeros((timesteps, 2)) Dvals = np.zeros((timesteps, 3))
This tests: -1) creating origin w/ eval_points -2) creating ensemble w/ eval_points -3) creating ensemble w/ eval_points, creating origin w/ eval_points """ import nef_theano as nef reload(nef) import math import numpy as np import matplotlib.pyplot as plt # create the list of evaluation points eval_points = np.arange(-1, 0, .5) net = nef.Network('EvalPoints Test') net.make_input('in', value=math.sin) net.make('A1', neurons=300, dimensions=1) # for test 1 net.make('A2', neurons=300, dimensions=1, eval_points=eval_points) # for test 2 net.make('A3', neurons=300, dimensions=1, eval_points=eval_points) # for test 3 net.make('B', neurons=100, dimensions=1) net.make('C', neurons=100, dimensions=1) net.make('D', neurons=100, dimensions=1) # function for testing evaluation points def pow(x):
import math import time import numpy as np #import matplotlib.pyplot as plt import sys, getopt sys.path.append(sys.argv[1]) import nef_theano as nef import functions build_time_start = time.time() net = nef.Network('Direct Mode Test', seed=47, command_arguments=sys.argv[2:], usr_module='test/nengo_tests/functions.py') net.make_input('in', math.sin) net.make('A', neurons=100, dimensions=1) net.make('B', neurons=1, dimensions=1, mode='direct') net.make('C', neurons=100, dimensions=1) net.make('D', neurons=1, dimensions=2, mode='direct') net.make('E', neurons=1, array_size=2, dimensions=2, mode='direct') net.connect('in', 'A') net.connect('A', 'B') net.connect('B', 'C') net.connect('B', 'E') net.connect('E', 'D', func=functions.product)
"""This test file is for checking the run time of the theano code""" import nef_theano as nef import math import time net = nef.Network('Runtime Test') net.make_input('in', value=math.sin) net.make('A', 1000, 1) net.make('B', 1000, 1) net.make('C', 1000, 1) net.make('D', 1000, 1) # some functions to use in our network def pow(x): return [xval**2 for xval in x] def mult(x): return [xval * 2 for xval in x] net.connect('in', 'A') net.connect('A', 'B') net.connect('A', 'C', func=pow) net.connect('A', 'D', func=mult) net.connect('D', 'B', func=pow) # throw in some recurrency whynot timesteps = 5000 # running for timesteps * .1 seconds start_time = time.time()
# Perform matrix multiplication on arbitrary matrices import sys sys.path.append(sys.argv[1]) import nef_theano as nef import functions hosts_file = sys.argv[2] if len(sys.argv) > 2 else None if hosts_file: net = nef.Network('Matrix Multiplication', 100, 100, hosts_file=hosts_file) else: net = nef.Network('Matrix Multiplication', 100, 100) # Adjust these values to change the matrix dimensions # Matrix A is D1xD2 # Matrix B is D2xD3 # result is D1xD3 D1 = 1 D2 = 2 D3 = 2 # values should stay within the range (-radius,radius) radius = 1 # make 2 matrices to store the input net.make_array('A', 50, D1 * D2, radius=radius) net.make_array('B', 50, D2 * D3, radius=radius) # connect inputs to them so we can set their value
"""This is a file to test the net.write_to_file method """ import numpy as np # import matplotlib.pyplot as plt import math import time from neo import hdf5io import sys sys.path.append(sys.argv[1]) import nef_theano as nef build_time_start = time.time() net = nef.Network('Write Out Test', seed=50) net.make_input('in', math.sin) net.make('A', 50, 1) net.make('B', 5, 1) net.connect('in', 'A') net.connect('in', 'B') timesteps = 100 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.01 Ip = net.make_probe('in', dt_sample=dt_step, pstc=pstc) Ap = net.make_probe('A', dt_sample=dt_step, pstc=pstc) Bp = net.make_probe('B', dt_sample=dt_step)
phrase = spa.Buffer(dimensions=dimensions, feedback=0) motor = spa.Buffer(dimensions=dimensions, feedback=0) noun = spa.Buffer(dimensions=dimensions) verb = spa.Buffer(dimensions=dimensions) BG = spa.BasalGanglia(Rules) thal = spa.Thalamus(BG) input = spa.Input(0.5, vision='WRITE') input.next(0.5, vision='ONE') input.next(0.5, vision='NONE') net = nef.Network('ParseWrite', seed=1, command_arguments=sys.argv[2:], usr_module='../examples/spa-example/functions.py') pw = ParseWrite(net) net.connect('noun.buffer', 'phrase.buffer', transform=pw.sinks['phrase'].parse('NOUN').get_transform_matrix()) net.connect('verb.buffer', 'phrase.buffer', transform=pw.sinks['phrase'].parse('VERB').get_transform_matrix()) pThal = net.make_probe('thal.rule', dt_sample=0.001) pMotor = net.make_probe('motor.buffer', dt_sample=0.001) net.run(1.5)
"""This is a file to test the fixed_seed parameter, which should make identical ensembles. """ import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef net = nef.Network('Array Test', fixed_seed=5, command_arguments=sys.argv[2:]) net.make_input('in', [1], zero_after_time=1.0) net.make('A', num_subs=1, neurons=50, dimensions=1) net.make('B', num_subs=1, neurons=50, dimensions=1) net.make_array('AB', neurons=50, array_size=2) net.connect('in', 'A') net.connect('in', 'B') net.connect('in', 'AB', index_post=[0, 1]) timesteps = 200 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.01 Ip = net.make_probe('in', dt_sample=dt_step, pstc=pstc) Ap = net.make_probe('A', dt_sample=dt_step, pstc=pstc) Bp = net.make_probe('B', dt_sample=dt_step, pstc=pstc)
import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef # TODO: If the seed value is 97, the tests don't pass. # All output is the same except for ensemble B's output. # If you switch the order of the lines for net.make of A and B, # then A will exhibit the problem instead of B # 1) Determine why the tests don't pass when the seed value is 97 # 2) If the root cause is a bug in the new version of the code, fix that bug. net = nef.Network('Weight, Index_Pre, and Index_Post Test', seed=96, command_arguments=sys.argv[2:]) net.make_input('in', value=math.sin) net.make('A', neurons=300, dimensions=1) net.make('B', neurons=300, dimensions=1) net.make('C', neurons=400, dimensions=2) net.make('D', neurons=300, dimensions=1) net.make('E', neurons=400, dimensions=2) net.make('F', neurons=400, dimensions=2) net.connect('in', 'A', weight=.5) net.connect('A', 'B', weight=2) net.connect('A', 'C', index_post=1) net.connect('A', 'D') net.connect('C', 'E', index_pre=1)
import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef import functions # create the list of evaluation points eval_points1 = np.arange(-1, 0, .5) eval_points2 = np.array([[1, 1], [-1, 1], [-1, -1], [1, -1]]).T hosts_file = sys.argv[2] if len(sys.argv) > 2 else None if hosts_file: net = nef.Network('EvalPoints Test', seed=5, hosts_file=hosts_file) else: net = nef.Network('EvalPoints Test', seed=5) net.make_input('in', value=math.sin) # for test 1 net.make('A1', neurons=300, dimensions=1) # for test 2 net.make('A2', neurons=300, dimensions=1, eval_points=eval_points1) # for test 3 net.make('A3', neurons=300, dimensions=1, eval_points=eval_points1) # for test 4 net.make('A4', neurons=300, array_size=3,
"""This is a file to test the network array function, both with make_array, and by using the array_size parameter in the network.make command""" import nef_theano as nef import numpy as np import matplotlib.pyplot as plt net=nef.Network('Array Test', seed=5) net.make_input('in', [-1,0,0,0,0,1], zero_after=1.0) net.make_array('A', neurons=200, array_size=3, dimensions=2, neuron_type='lif') net.make('A2', neurons=200, array_size=2, dimensions=3, neuron_type='lif') net.make('B', 200, 6, neuron_type='lif') net.make('B2', 200, dimensions=1, array_size=6, neuron_type='lif') net.connect('in', 'A', pstc=0.1) net.connect('in', 'A2', pstc=0.1) net.connect('in', 'B', pstc=0.1) net.connect('in', 'B2', pstc=0.1) timesteps = 500 # setup arrays to store data gathered from sim Fvals = np.zeros((timesteps, 6)) Avals = np.zeros((timesteps, 6)) A2vals = np.zeros((timesteps, 6)) Bvals = np.zeros((timesteps, 6)) B2vals = np.zeros((timesteps, 6)) print "starting simulation" for i in range(timesteps): net.run(0.005) Fvals[i] = net.nodes['in'].decoded_output.get_value()
import matplotlib.pyplot as plt def sin3(x): return math.sin(x) * 3 def pow(x): return [xval**2 for xval in x] def mult(x): return [xval * 2 for xval in x] net = nef.Network('Encoder Test') net.make_input('in', value=sin3) net.make('A', 1000, 1, radius=5) net.make('B', 300, 1, radius=.5) net.make('C', 1000, 1, radius=10) net.make('D', 300, 1, radius=6) net.connect('in', 'A') net.connect('A', 'B') net.connect('A', 'C', func=pow) net.connect('A', 'D', func=mult) timesteps = 500 Fvals = np.zeros((timesteps, 1)) Avals = np.zeros((timesteps, 1)) Bvals = np.zeros((timesteps, 1))
"""This is a test file to test the transform parameter on the connect function. The transform matrix is post x pre dimensions""" import nef_theano as nef import math import numpy as np import matplotlib.pyplot as plt def func(x): return [math.sin(x), -math.sin(x)] net = nef.Network('Transform Test') net.make_input('in', value=func) net.make('A', 300, 3) # define our transform and connect up! transform = [[0, 1], [1, 0], [1, -1]] net.connect('in', 'A', transform=transform) timesteps = 500 Fvals = np.zeros((timesteps, 2)) Avals = np.zeros((timesteps, 3)) for i in range(timesteps): net.run(0.01) Fvals[i] = net.nodes['in'].decoded_output.get_value() Avals[i] = net.nodes['A'].origin['X'].decoded_output.get_value() plt.ion() plt.clf()
"""This is a test file to test the func parameter on the connect method""" import math import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef import functions net = nef.Network('Function Test', seed=91, command_arguments=sys.argv[2:], usr_module='test/nengo_tests/functions.py') net.make_input('in', value=math.sin) net.make('A', neurons=250, dimensions=1, num_subs=2) net.make('B', neurons=250, dimensions=3) net.connect('in', 'A') net.connect('A', 'B', func=functions.square, pstc=0.1) timesteps = 500 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.03
# Perform matrix multiplication on arbitrary matrices import nef_theano as nef net = nef.Network('Matrix Multiplication', 100, 100) #Create the network object # Adjust these values to change the matrix dimensions # Matrix A is D1xD2 # Matrix B is D2xD3 # result is D1xD3 D1 = 1 D2 = 2 D3 = 2 # values should stay within the range (-radius,radius) radius = 1 # make 2 matrices to store the input net.make_array('A', 50, D1 * D2, radius=radius) net.make_array('B', 50, D2 * D3, radius=radius) # connect inputs to them so we can set their value net.make_input('input A', [0] * D1 * D2) net.make_input('input B', [0] * D2 * D3) net.connect('input A', 'A') net.connect('input B', 'B') # the C matrix holds the intermediate product calculations # need to compute D1*D2*D3 products to multiply 2 matrices together
"""This is a test file to test the func parameter on the connect method""" import nef_theano as nef import numpy as np import matplotlib.pyplot as plt import math #TODO: this doesn't actually work, all output is the same net = nef.Network('Function Test') net.make_input('in', value=math.sin) net.make('A', neurons=500, dimensions=1) net.make('B', neurons=500, dimensions=3) # function example for testing def square(x): return [-x[0] * x[0], -x[0], x[0]] net.connect('in', 'A') net.connect('A', 'B', func=square, pstc=0.1) timesteps = 500 # setup arrays to store data gathered from sim Fvals = np.zeros((timesteps, 1)) Avals = np.zeros((timesteps, 1)) Bvals = np.zeros((timesteps, 3)) print "starting simulation" for i in range(timesteps): net.run(0.01)
#import matplotlib.pyplot as plt import sys, getopt sys.path.append(sys.argv[1]) import nef_theano as nef import functions build_time_start = time.time() timesteps = 1000 dt_step = 0.001 net = nef.Network('Encoder Test', dt=dt_step, seed=103, command_arguments=sys.argv[2:], usr_module='test/nengo_tests/functions.py') net.make_input('in1', math.sin) net.make_input('in2', math.cos) net.make('A', neurons=100, dimensions=1) net.make('B', neurons=100, dimensions=1, encoders=[[1]], intercept=(0, 1.0)) net.make('C', neurons=100, dimensions=2, radius=1.5) net.make('D', neurons=100, dimensions=2, encoders=[[1, 1], [1, -1], [-1, -1], [-1, 1]], radius=1.5) net.make('outputC', neurons=1, dimensions=1, mode='direct') net.make('outputD', neurons=1, dimensions=1, mode='direct')
import nef_theano as nef import numpy net=nef.Network('Cleanup',seed=3) D = 128 M = 50000 N1 = 100 N2 = 50 index = 0 def make_vector(): v = numpy.random.normal(size=(D,)) norm = numpy.linalg.norm(v) v = v / norm return v print 'making words...' words = [make_vector() for i in range(M)] words = numpy.array(words) print '...done' net.make_array('A', N1, D) net.make_array('B', N1, D) net.make_array('C', N2, M)#,intercept=(0.6,0.9)) print 'made' net.connect('A', 'C', words.T, pstc=0.1) net.connect('C', 'B', words, pstc=0.1)
import numpy as np #import matplotlib.pyplot as plt import sys, getopt sys.path.append(sys.argv[1]) import nef_theano as nef neurons = 100 dimensions = 1 array_size = 3 inhib_scale = 10 net = nef.Network('WeightMatrix Test', seed=100, command_arguments=sys.argv[2:]) net.make_input('in1', 1, zero_after_time=2.5) net.make_input('in2', [1, .5, -.5]) net.make('A', neurons=neurons, dimensions=dimensions, intercept=(.1, 1)) net.make('B', neurons=neurons, dimensions=dimensions) # for test 1 net.make('B2', neurons=neurons, dimensions=dimensions, array_size=array_size) # for test 2 net.make('B3', neurons=neurons, dimensions=dimensions, array_size=array_size) # for test 3 net.make('B4', neurons=neurons, dimensions=dimensions, array_size=array_size) # for test 4 # setup inhibitory scaling matrix inhib_matrix_1 = [[-10] * dimensions] * neurons # for test 1 and 2
the function being computed so that it has the proper shape inside unit length). """ import math import numpy as np #import matplotlib.pyplot as plt import sys sys.path.append(sys.argv[1]) import nef_theano as nef import functions net = nef.Network('Radius Test', seed=97, command_arguments=sys.argv[2:]) net.make_input('in', value=functions.sin3) net.make('A', neurons=1000, dimensions=1, radius=5) net.make('B', neurons=300, dimensions=1, radius=.5) net.make('C', neurons=1000, dimensions=1, radius=10) net.make('D', neurons=300, dimensions=1, radius=6) net.connect('in', 'A') net.connect('A', 'B') net.connect('A', 'C', func=functions.pow) net.connect('A', 'D', func=functions.mult) timesteps = 500 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps)
"""This is a test file to test the noise parameter on ensemble""" import math import numpy as np #import matplotlib.pyplot as plt import sys, getopt sys.path.append(sys.argv[1]) import nef_theano as nef net = nef.Network('Noise Test', seed=91, command_arguments=sys.argv[2:]) net.make_input('in', value=math.sin) net.make('A', neurons=300, dimensions=1, noise=1) net.make('A2', neurons=300, dimensions=1, noise=100) net.make('B', neurons=300, dimensions=2, noise=1000, noise_type='Gaussian') net.make('C', neurons=100, dimensions=1, array_size=3, noise=10) net.connect('in', 'A') net.connect('in', 'A2') net.connect('in', 'B') net.connect('in', 'C') timesteps = 500 dt_step = 0.01 t = np.linspace(dt_step, timesteps * dt_step, timesteps) pstc = 0.01 Ip = net.make_probe('in', dt_sample=dt_step, pstc=pstc) Ap = net.make_probe('A', dt_sample=dt_step, pstc=pstc) A2p = net.make_probe('A2', dt_sample=dt_step, pstc=pstc)