def test_log_multi_calls_single_timestep(self, scheduler_conditions, multi_run): con_with_rpc_pipeline = pnl.Context(rpc_pipeline=Queue()) pipeline = con_with_rpc_pipeline.rpc_pipeline lca = pnl.LCAMechanism( size=2, leak=0.5, threshold=0.515, reset_stateful_function_when=pnl.AtTrialStart() ) lca.set_delivery_conditions(pnl.VALUE) m0 = pnl.ProcessingMechanism( size=2 ) comp = pnl.Composition() comp.add_linear_processing_pathway([m0, lca]) if scheduler_conditions: comp.scheduler.add_condition(lca, pnl.AfterNCalls(m0, 2)) comp.run(inputs={m0: [[1, 0], [1, 0], [1, 0]]}, context=con_with_rpc_pipeline) actual = [] while not pipeline.empty(): actual.append(pipeline.get()) integration_end_dict = {i.time: i for i in actual} if scheduler_conditions: expected_times = ['0:0:1:1', '0:1:1:1', '0:2:1:1'] else: expected_times = ['0:0:0:1', '0:1:0:1', '0:2:0:1'] assert list(integration_end_dict.keys()) == expected_times vals = [i.value.data for i in integration_end_dict.values()] # floats in value, so use np.allclose assert np.allclose(vals, [[[0.52466739, 0.47533261]] * 3]) if multi_run: comp.run(inputs={m0: [[1, 0], [1, 0], [1, 0]]}, context=con_with_rpc_pipeline) actual = [] while not pipeline.empty(): actual.append(pipeline.get()) integration_end_dict.update({i.time: i for i in actual}) if scheduler_conditions: expected_times = ['0:0:1:1', '0:1:1:1', '0:2:1:1', '1:0:1:1', '1:1:1:1', '1:2:1:1'] else: expected_times = ['0:0:0:1', '0:1:0:1', '0:2:0:1', '1:0:0:1', '1:1:0:1', '1:2:0:1'] assert list(integration_end_dict.keys()) == expected_times vals = [i.value.data for i in integration_end_dict.values()] # floats in value, so use np.allclose assert np.allclose(vals, [[[0.52466739, 0.47533261]] * 6])
# Log Middle_Weights of MappingProjection to Hidden_Layer_2 # Hidden_Layer_2.set_log_conditions('Middle Weights') Middle_Weights.set_log_conditions('mod_matrix') mySystem.reportOutputPref = True # Shows graph will full information: mySystem.show_graph(show_dimensions=pnl.ALL) mySystem.show_graph(show_learning=pnl.ALL) # mySystem.show_graph(show_learning=pnl.ALL, show_processes=True) # mySystem.show_graph(show_learning=pnl.ALL, show_dimensions=pnl.ALL, show_mechanism_structure=True) # Shows minimal graph: # mySystem.show_graph() stim_list = {Input_Layer: ['red']} target_list = {Output_Layer: [[0, 0, 1]]} mySystem.run( num_trials=1, inputs=stim_list, targets=target_list, call_before_trial=functools.partial(print_header, mySystem), call_after_trial=functools.partial(show_target, mySystem), termination_processing={pnl.TimeScale.TRIAL: pnl.AfterNCalls(Output_Layer, 1)}, animate={'show_learning':pnl.ALL, 'unit':pnl.EXECUTION_SET, pnl.SAVE_IMAGES:True} ) # Print out logged weights for Middle_Weights # print('\nMiddle Weights (to Hidden_Layer_2): \n', Hidden_Layer_2.log.nparray(entries='Middle Weights', header=False)) print('\nMiddle Weights (to Hidden_Layer_2): \n', Middle_Weights.log.nparray(entries='mod_matrix', header=False))
mySystem.reportOutputPref = True # Shows graph will full information: mySystem.show_graph(show_dimensions=pnl.ALL) mySystem.show_graph(show_learning=pnl.ALL) # mySystem.show_graph(show_learning=pnl.ALL, show_processes=True) # mySystem.show_graph(show_learning=pnl.ALL, show_dimensions=pnl.ALL, show_mechanism_structure=True) # Shows minimal graph: # mySystem.show_graph() stim_list = {Input_Layer: ['red']} target_list = {Output_Layer: [[0, 0, 1]]} mySystem.run(num_trials=1, inputs=stim_list, targets=target_list, call_before_trial=functools.partial(print_header, mySystem), call_after_trial=functools.partial(show_target, mySystem), termination_processing={ pnl.TimeScale.TRIAL: pnl.AfterNCalls(Output_Layer, 1) }, animate={ 'show_learning': pnl.ALL, 'unit': pnl.EXECUTION_SET, pnl.SAVE_IMAGES: True }) # Print out logged weights for Middle_Weights # print('\nMiddle Weights (to Hidden_Layer_2): \n', Hidden_Layer_2.log.nparray(entries='Middle Weights', header=False)) print('\nMiddle Weights (to Hidden_Layer_2): \n', Middle_Weights.log.nparray(entries='mod_matrix', header=False))
function=pnl.FitzHughNagumoIntegrator( name='FitzHughNagumoIntegrator Function-0', d_v=1, initial_v=-1, initializer=[[0]], default_variable=[[0]])) im = pnl.IntegratorMechanism(name='im', function=pnl.AdaptiveIntegrator( initializer=[[0]], rate=0.5, default_variable=[[0]])) comp.add_node(fn) comp.add_node(im) comp.add_projection(projection=pnl.MappingProjection( name='MappingProjection from fn[OutputPort-0] to im[InputPort-0]', function=pnl.LinearMatrix(matrix=[[1.0]], default_variable=[-1.0])), sender=fn, receiver=im) comp.scheduler.add_condition(fn, pnl.Always()) comp.scheduler.add_condition( im, pnl.All(pnl.EveryNCalls(fn, 20.0), pnl.AfterNCalls(fn, 1600.0))) comp.scheduler.termination_conds = { pnl.TimeScale.RUN: pnl.Never(), pnl.TimeScale.TRIAL: pnl.AfterNCalls(fn, 2000) } comp.show_graph()
(simtime, fhn)) fn = pnl.IntegratorMechanism(name='fn', function=fhn) comp = pnl.Composition(name='comp') im = pnl.IntegratorMechanism(name='im') # only used to demonstrate conditions comp.add_linear_processing_pathway([fn, im]) comp.scheduler.add_condition_set({ fn: pnl.Always(), # default im: pnl.All( # run when both conditions are met pnl.EveryNCalls(fn, 1 / dt), # every 1ms, based on fn frequency pnl.AfterNCalls(fn, .8 * simtime / dt) # after 80ms, based on fn frequency ) }) comp.termination_processing = { pnl.TimeScale.RUN: pnl.Never( ), # default, "Never" for early termination - ends when all trials finished pnl.TimeScale.TRIAL: pnl.AfterNCalls(fn, int(simtime / dt)) } print('Running the SimpleFN model...') comp.run(inputs={fn: 0}, log=True) print('Finished running the SimpleFN model')
function=pnl.LinearMatrix(matrix=[[1.0]], default_variable=[0.5]), ), sender=B, receiver=D, ) comp.add_projection( projection=pnl.MappingProjection( name="MappingProjection from C[RESULT] to D[InputPort-0]", function=pnl.LinearMatrix(matrix=[[1.0]]), ), sender=C, receiver=D, ) comp.add_projection( projection=pnl.MappingProjection( name="MappingProjection from C[RESULT] to Inner Composition Input_CIM[INPUT_CIM_E_InputPort-0]", function=pnl.LinearMatrix(matrix=[[1.0]]), ), sender=C, receiver=Inner_Composition, ) comp.scheduler.add_condition(A, pnl.EveryNPasses(1, pnl.TimeScale.TRIAL)) comp.scheduler.add_condition(B, pnl.EveryNCalls(A, 2)) comp.scheduler.add_condition(C, pnl.EveryNCalls(B, 2)) comp.scheduler.termination_conds = { pnl.TimeScale.RUN: pnl.AfterNTrials(1, pnl.TimeScale.RUN), pnl.TimeScale.TRIAL: pnl.AfterNCalls(D, 4), }
name='A') B = pnl.TransferMechanism(function=pnl.Logistic, name='B') for m in [A, B]: comp.add_node(m) comp.add_projection(pnl.MappingProjection(), A, B) comp.scheduler.add_condition_set({ A: pnl.EveryNPasses(1), B: pnl.EveryNCalls(A, 2), }) comp.termination_processing = { pnl.TimeScale.RUN: pnl.AfterNTrials(1), pnl.TimeScale.TRIAL: pnl.AfterNCalls(B, 4) } comp2 = pnl.Composition(name='comp2') for m in [A, B]: comp2.add_node(m) comp2.add_projection(pnl.MappingProjection(), A, B) comp2.scheduler.add_condition_set({ A: pnl.EveryNPasses(1), B: pnl.EveryNCalls(A, 4), }) comp2.termination_processing = {
import psyneulink as pnl comp = pnl.Composition(name='comp') fn = pnl.IntegratorMechanism(name='fn', function=pnl.FitzHughNagumoIntegrator( name='FitzHughNagumoIntegrator Function-0', d_v=1, initial_v=-1, initializer=[[0]], default_variable=[[0]])) comp.add_node(fn) comp.scheduler.add_condition(fn, pnl.Always()) comp.scheduler.termination_conds = { pnl.TimeScale.RUN: pnl.Never(), pnl.TimeScale.TRIAL: pnl.AfterNCalls(fn, 2000) } comp.show_graph()
comp = pnl.Composition(name='comp') comp.add_linear_processing_pathway([fn]) # Left commented because TimeInterval is still to be implemented in PNL # im = pnl.IntegratorMechanism(name='im') # only used to demonstrate conditions # comp.add_linear_processing_pathway([fn, im]) # comp.scheduler.add_condition_set({ # fn: pnl.TimeInterval(interval=.05, unit='ms') # im: pnl.TimeInterval(start=80, interval=1, unit='ms') # }) comp.termination_processing = { pnl.TimeScale.RUN: pnl.Never( ), # default, "Never" for early termination - ends when all trials finished pnl.TimeScale.TRIAL: pnl.AfterNCalls(fn, int(simtime / dt)) # replicates time condition # pnl.TimeScale.TRIAL: pnl.TimeInterval(end=100, unit='ms') } print('Running the SimpleFN model...') comp.run(inputs={fn: 0}, log=True) print('Finished running the SimpleFN model') base_fname = __file__.replace('.py', '') with open(f'{base_fname}.json', 'w') as outfi: outfi.write(comp.json_summary) with open(f'{base_fname}.converted.py', 'w') as outfi: outfi.write(pnl.generate_script_from_json(comp.json_summary))
comp.add_node(im) comp.add_projection( projection=pnl.MappingProjection( name="MappingProjection_from_fn_OutputPort_0__to_im_InputPort_0_", function=pnl.LinearMatrix(default_variable=[-1.0], matrix=[[1.0]]), ), sender=fn, receiver=im, ) comp.scheduler.add_condition(fn, pnl.Always()) comp.scheduler.add_condition( im, pnl.All( pnl.EveryNCalls(dependency=fn, n=20.0), pnl.AfterNCalls(dependency=fn, n=1600.0, time_scale=pnl.TimeScale.ENVIRONMENT_STATE_UPDATE), ), ) comp.scheduler.termination_conds = { pnl.TimeScale.ENVIRONMENT_SEQUENCE: pnl.Never(), pnl.TimeScale.ENVIRONMENT_STATE_UPDATE: pnl.AfterNCalls(dependency=fn, n=2000, time_scale=pnl.TimeScale.ENVIRONMENT_STATE_UPDATE), }