示例#1
0
    def test_alias_duplicate_base_access_fails(self):
        mech = pnl.LCAMechanism(function=pnl.ReLU)

        with pytest.raises(
            pnl.ParameterPortError,
            match='Did you want leak-function or rate'
        ):
            mech.parameter_ports['leak']
示例#2
0
    def test_alias_duplicate(self):
        mech = pnl.LCAMechanism(function=pnl.ReLU)

        assert mech.parameter_ports[
            'leak-function'].source is mech.function.parameters.leak
        assert mech.parameter_ports[
            'leak-integrator_function'] is mech.parameter_ports[
                'integration_rate']
        assert mech.parameter_ports[
            'leak-integrator_function'].source is mech.integrator_function.parameters.rate
示例#3
0
    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])
示例#4
0
    integration_rate=processing_rate,
    name='WORDS HIDDEN')

# OUTPUT LAYER
r = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    integrator_mode=False,
    noise=psyneulink.core.components.functions.distributionfunctions.
    NormalDist(mean=0, standard_deviation=unit_noise).function,
    integration_rate=processing_rate,
    name='RESPONSE')

# DECISION LAYER
d = pnl.DDM(input_format=pnl.ARRAY)
l = pnl.LCAMechanism(size=2)

#   LOGGING
ch.set_log_conditions('value')
wh.set_log_conditions('value')
r.set_log_conditions('value')

#  PROJECTIONS
c_ih = pnl.MappingProjection(matrix=[[2.2, -2.2], [-2.2, 2.2]],
                             name='COLOR INPUT TO HIDDEN')
w_ih = pnl.MappingProjection(matrix=[[2.6, -2.6], [-2.6, 2.6]],
                             name='WORD INPUT TO HIDDEN')
c_hr = pnl.MappingProjection(matrix=[[1.3, -1.3], [-1.3, 1.3]],
                             name='COLOR HIDDEN TO OUTPUT')
w_hr = pnl.MappingProjection(matrix=[[2.5, -2.5], [-2.5, 2.5]],
                             name='WORD HIDDEN TO OUTPUT')
示例#5
0
                                    size=1,
                                    function=pnl.Linear(slope=1, intercept=0),
                                    output_ports=[pnl.RESULT],
                                    name='Cue-Stimulus Interval')

taskLayer = pnl.TransferMechanism(default_variable=[[0.0, 0.0]],
                                  size=2,
                                  function=pnl.Linear(slope=1, intercept=0),
                                  output_ports=[pnl.RESULT],
                                  name='Task Input [I1, I2]')

activation = pnl.LCAMechanism(default_variable=[[0.0, 0.0]],
                              size=2,
                              function=pnl.Logistic(gain=1),
                              leak=.5,
                              competition=2,
                              noise=0,
                              time_step_size=.1,
                              termination_measure=pnl.TimeScale.TRIAL,
                              termination_threshold=3,
                              name='Task Activations [Act 1, Act 2]')

# response = pnl.ProcessingMechanism()

# Create controller
csiController = pnl.ControlMechanism(monitor_for_control=cueInterval,
                                     control_signals=[
                                         (pnl.TERMINATION_THRESHOLD,
                                          activation)
                                     ],
                                     modulation=pnl.OVERRIDE)
示例#6
0
def get_trained_network(bipartite_graph, num_features=3, num_hidden=200, epochs=10, learning_rate=20, attach_LCA=True, competition=0.2, self_excitation=0.2, leak=0.4, threshold=1e-4):
    # Get all tasks from bipartite graph (edges) and strip 'i/o' suffix
    all_tasks = get_all_tasks(bipartite_graph)

    # Analyze bipartite graph for network properties
    onodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 0 ]
    inodes = [ n for n, d in bipartite_graph.nodes(data=True) if d['bipartite'] == 1 ]
    input_dims = len(inodes)
    output_dims = len(onodes)
    num_tasks = len(all_tasks)

    # Start building network as PsyNeuLink object
    # Layer parameters
    nh = num_hidden
    D_i = num_features * input_dims
    D_c = num_tasks
    D_h = nh
    D_o = num_features * output_dims

    # Weight matrices (defaults provided by Dillon)
    wih = np.random.rand(D_i, D_h) * 0.02 - 0.01
    wch = np.random.rand(D_c, D_h) * 0.02 - 0.01
    wco = np.random.rand(D_c, D_o) * 0.02 - 0.01
    who = np.random.rand(D_h, D_o) * 0.02 - 0.01

    # Training params (defaults provided by Dillon)
    patience = 10
    min_delt = 0.00001
    lr = learning_rate

    # Instantiate layers and projections
    il = pnl.TransferMechanism(size=D_i, name='input')
    cl = pnl.TransferMechanism(size=D_c, name='control')

    hl = pnl.TransferMechanism(size=D_h, name='hidden',
                                               function=pnl.Logistic(bias=-2))

    ol = pnl.TransferMechanism(size=D_o, name='output',
                                               function=pnl.Logistic(bias=-2))

    pih = pnl.MappingProjection(matrix=wih)
    pch = pnl.MappingProjection(matrix=wch)
    pco = pnl.MappingProjection(matrix=wco)
    pho = pnl.MappingProjection(matrix=who)

    # Create training data for network
    # We train across all possible inputs, one task at a time
    input_examples, output_examples, control_examples = generate_training_data(all_tasks, num_features, input_dims, output_dims)

    # Training parameter set
    input_set = {
            'inputs': {
                    il: input_examples.tolist(),
                    cl: control_examples.tolist()
            },
            'targets': {
                    ol: output_examples.tolist()
            },
            'epochs': epochs
    }

    mnet = pnl.AutodiffComposition(learning_rate=learning_rate,
                                   name='mnet')

    mnet.output_CIM.parameters.value._set_history_max_length(100000)
    mnet.add_node(il)
    mnet.add_node(cl)
    mnet.add_node(hl)
    mnet.add_node(ol)
    mnet.add_projection(projection=pih, sender=il, receiver=hl)
    mnet.add_projection(projection=pch, sender=cl, receiver=hl)
    mnet.add_projection(projection=pco, sender=cl, receiver=ol)
    mnet.add_projection(projection=pho, sender=hl, receiver=ol)

    # Train network
    print("training 1")
    t1 = time.time()
    mnet.learn(
        inputs=input_set,
        minibatch_size=1,
        bin_execute=MNET_BIN_EXECUTE,
        patience=patience,
        min_delta=min_delt,
    )
    t2 = time.time()
    print("training 1:", MNET_BIN_EXECUTE, t2-t1)

    # Apply LCA transform (values from Sebastian's code -- supposedly taken from the original LCA paper from Marius & Jay)
    if attach_LCA:
        lca = pnl.LCAMechanism(size=D_o,
                               leak=leak,
                               competition=competition,
                               self_excitation=self_excitation,
                               time_step_size=0.01,
                               threshold=threshold,
                               threshold_criterion=pnl.CONVERGENCE,
                               reset_stateful_function_when=pnl.AtTrialStart(),
                               name='lca')

        # Wrapper composition used to pass values between mnet (AutodiffComposition) and lca (LCAMechanism)
        wrapper_composition = pnl.Composition()

        # Add mnet and lca to outer_composition
        wrapper_composition.add_linear_processing_pathway([mnet, lca])

        return wrapper_composition

    return mnet
示例#7
0
    def test_alias_unique(self):
        mech = pnl.LCAMechanism()

        assert mech.parameter_ports['leak'] is mech.parameter_ports[
            'integration_rate']
# Input Layer --- [ Target 1, Target 2, Distractor ]

# First, we create the 3 layers of the behavioral network, i.e. INPUT LAYER, DECISION LAYER, and RESPONSE LAYER.
input_layer = pnl.TransferMechanism(
    size=3,                      # Number of units in input layer
    initial_value=[[0.0, 0.0, 0.0]],     # Initial input values
    name='INPUT LAYER'                  # Define the name of the layer; this is optional,
)                 # but will help you to overview your model later on

# Create Decision Layer  --- [ Target 1, Target 2, Distractor ]
decision_layer = pnl.LCAMechanism(
    size=3,                            # Number of units in input layer
    initial_value=[[0.0, 0.0, 0.0]],    # Initial input values
    time_step_size=dt,                 # Integration step size
    leak=-1.0,                         # Sets off diagonals to negative values
    self_excitation=selfdwt,           # Set diagonals to self excitate
    competition=inhwt,                 # Set off diagonals to inhibit
    function=psyneulink.core.components.functions.transferfunctions.Logistic(x_0=decbias),   # Set the Logistic function with bias = decbias
    # noise=pnl.UniformToNormalDist(standard_deviation = SD).function, # The UniformToNormalDist function will
    integrator_mode=True,               # set the noise with a seed generator that is compatible with
    name='DECISION LAYER'               # MATLAB random seed generator 22 (rsg=22)
)

# decision_layer.set_log_conditions('RESULT')  # Log RESULT of the decision layer
decision_layer.set_log_conditions('value')  # Log value of the decision layer

for output_state in decision_layer.output_states:
    output_state.value *= 0.0                                       # Set initial output values for decision layer to 0

# Create Response Layer  --- [ Target1, Target2 ]
response_layer = pnl.LCAMechanism(
    size=2,                                        # Number of units in input layer
示例#9
0
# Create mechanisms ---------------------------------------------------------------------------------------------------

# Input Layer --- [ Target, Distractor ]
input_layer = pnl.TransferMechanism(size=2,
                                    initial_value=np.array([[0.0, 0.0]]),
                                    name='INPUT LAYER')

# Create Decision Layer  --- [ Target, Distractor ]

decision_layer = pnl.LCAMechanism(
    size=2,
    time_step_size=dt,
    leak=-1.0,
    self_excitation=w_XiXi,
    competition=w_XiXj,
    #  Recurrent matrix: [  w_XiXi   -w_XiXj ]
    #                    [ -w_XiXj    w_XiXi ]
    function=psyneulink.core.components.functions.transferfunctions.Logistic(
        x_0=b_decision),
    noise=psyneulink.core.components.functions.distributionfunctions.
    NormalDist(standard_deviation=SD).function,
    integrator_mode=True,
    name='DECISION LAYER')

# Create Response Layer  --- [ Target ]

response_layer = pnl.LCAMechanism(
    size=1,
    time_step_size=dt,
    leak=-1.0,
    self_excitation=w_X3X3,
    #  Recurrent matrix: [w_X3X3]
示例#10
0
multitasking_system = pnl.System(processes=[network_process, hidden_control_process, output_control_process])

# WEIGHTS TO COME FROM SEBASTIAN

example_stimulus_inputs = [[1,0,0,1],[1,0,1,0]]
example_task_inputs = [[0,0,0,1],[1,0,0,0]]
example_training_pattern = [[0,0,0,1],[1,0,0,0]]

# RUN THIS TO GET SPACE OF INPUTS ON WHICH TO OPTIMIZE LCAMechanism PARAMS:
inputs_to_LCA = multitasking_system.run(inputs={stimulus_layer:example_stimulus_inputs,
                                                task_layer:example_task_inputs})

# SOME PYTHON ALGORITHM HERE THAT SELECTS THE 2-UNIT SUBVECTOR FROM inputs_to_LCA CORRESPONDING TO THE RELEVANT TASK
# AS INPUT TO optimization_system BELOW, AND THEN RUN THE SYSTEM FOR EACH INPUT, USING EVC TO OPTIMIZE LCAMechanism PARAMETERS
#  FOR EACH, BASED CONTROL PARAMETERS AND OBJECTIVE FUNCTION

input_layer = pnl.TransferMechanism(size=2)
decision_layer = pnl.LCAMechanism(size=2,
                                  # INCLUDE TERMINATION CONDITION USING THREHSOLD = ControlSignal)
                                  )
decision_process = pnl.Process(pathway=[input_layer, decision_layer])
optimization_system = pnl.System(processes=[decision_process],
                                 monitor_for_control=[decision_layer.output_states[pnl.RESULT]])
# ADD COMPARATOR MECHANISM FOR ACCURACY DETERMINATION


# EVC PARAMS:
# - number of simulations to run per LCAMechanism
# - which inputs to provide (i.e., *NOT* using typical PredictionMechanisms)
示例#11
0
import psyneulink as pnl
import psyneulink.core.components.functions.transferfunctions

ci = pnl.TransferMechanism(size=2, name='COLORS INPUT')
wi = pnl.TransferMechanism(size=2, name='WORDS INPUT')
ch = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='COLORS HIDDEN')
wh = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='WORDS HIDDEN')
tl = pnl.TransferMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic(
        gain=pnl.CONTROL),
    name='TASK CONTROL')
rl = pnl.LCAMechanism(
    size=2,
    function=psyneulink.core.components.functions.transferfunctions.Logistic,
    name='RESPONSE')
cp = pnl.Process(pathway=[ci, ch, rl])
wp = pnl.Process(pathway=[wi, wh, rl])
tc = pnl.Process(pathway=[tl, ch])
tw = pnl.Process(pathway=[tl, wh])
s = pnl.System(processes=[tc, tw, cp, wp],
               controller=pnl.EVCControlMechanism(name='EVC Mechanimsm'),
               monitor_for_control=[rl])
s.show_graph()
def get_stroop_model(unit_noise_std=.01, dec_noise_std=.1):
    # model params
    # TODO bad practice, to be moved
    hidden_func = pnl.Logistic(gain=1.0, x_0=4.0)
    integration_rate = .2
    leak = 0
    competition = 1
    # input layer, color and word
    inp_clr = pnl.TransferMechanism(size=N_UNITS,
                                    function=pnl.Linear,
                                    name='COLOR INPUT')
    inp_wrd = pnl.TransferMechanism(size=N_UNITS,
                                    function=pnl.Linear,
                                    name='WORD INPUT')
    # task layer, represent the task instruction; color naming / word reading
    inp_task = pnl.TransferMechanism(size=N_UNITS,
                                     function=pnl.Linear,
                                     name='TASK')
    # hidden layer for color and word
    hid_clr = pnl.TransferMechanism(
        size=N_UNITS,
        function=hidden_func,
        integrator_mode=True,
        integration_rate=integration_rate,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        name='COLORS HIDDEN')
    hid_wrd = pnl.TransferMechanism(
        size=N_UNITS,
        function=hidden_func,
        integrator_mode=True,
        integration_rate=integration_rate,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        name='WORDS HIDDEN')
    # output layer
    output = pnl.TransferMechanism(
        size=N_UNITS,
        function=pnl.Logistic,
        integrator_mode=True,
        integration_rate=integration_rate,
        noise=pnl.NormalDist(standard_deviation=unit_noise_std).function,
        name='OUTPUT')
    # decision layer, some accumulator
    decision = pnl.LCAMechanism(
        size=N_UNITS,
        leak=leak,
        competition=competition,
        # MAX_VS_NEXT=lca_mvn,
        noise=pnl.UniformToNormalDist(
            standard_deviation=dec_noise_std).function,
        name='DECISION')
    # PROJECTIONS, weights copied from cohen et al (1990)
    wts_clr_ih = pnl.MappingProjection(matrix=[[2.2, -2.2], [-2.2, 2.2]],
                                       name='COLOR INPUT TO HIDDEN')
    wts_wrd_ih = pnl.MappingProjection(matrix=[[2.6, -2.6], [-2.6, 2.6]],
                                       name='WORD INPUT TO HIDDEN')
    wts_clr_ho = pnl.MappingProjection(matrix=[[1.3, -1.3], [-1.3, 1.3]],
                                       name='COLOR HIDDEN TO OUTPUT')
    wts_wrd_ho = pnl.MappingProjection(matrix=[[2.5, -2.5], [-2.5, 2.5]],
                                       name='WORD HIDDEN TO OUTPUT')
    wts_tc = pnl.MappingProjection(matrix=[[4.0, 4.0], [0, 0]],
                                   name='COLOR NAMING')
    wts_tw = pnl.MappingProjection(matrix=[[0, 0], [4.0, 4.0]],
                                   name='WORD READING')
    # build the model
    model = pnl.Composition(name='STROOP model')
    model.add_node(inp_clr)
    model.add_node(inp_wrd)
    model.add_node(hid_clr)
    model.add_node(hid_wrd)
    model.add_node(inp_task)
    model.add_node(output)
    model.add_node(decision)
    model.add_linear_processing_pathway([inp_clr, wts_clr_ih, hid_clr])
    model.add_linear_processing_pathway([inp_wrd, wts_wrd_ih, hid_wrd])
    model.add_linear_processing_pathway([hid_clr, wts_clr_ho, output])
    model.add_linear_processing_pathway([hid_wrd, wts_wrd_ho, output])
    model.add_linear_processing_pathway([inp_task, wts_tc, hid_clr])
    model.add_linear_processing_pathway([inp_task, wts_tw, hid_wrd])
    model.add_linear_processing_pathway(
        [output, pnl.IDENTITY_MATRIX, decision])
    # # LOGGING
    # hid_clr.set_log_conditions('value')
    # hid_wrd.set_log_conditions('value')
    # output.set_log_conditions('value')
    # collect the node handles
    nodes = [inp_clr, inp_wrd, inp_task, hid_clr, hid_wrd, output, decision]
    metadata = [integration_rate, dec_noise_std, unit_noise_std]
    return model, nodes, metadata
    name="word_hidden",
    function=pnl.Logistic(bias=-4.0, default_variable=[[0.0, 0.0]]),
    default_variable=[[0.0, 0.0]],
)
task_input = pnl.ProcessingMechanism(
    name="task_input",
    function=pnl.Linear(default_variable=[[0.0, 0.0]]),
    default_variable=[[0.0, 0.0]],
)
TASK = pnl.LCAMechanism(
    name="TASK",
    combination_function=pnl.LinearCombination(default_variable=[[0.0, 0.0]]),
    function=pnl.Logistic(default_variable=[[0.0, 0.0]]),
    integrator_function=pnl.LeakyCompetingIntegrator(
        name="LeakyCompetingIntegrator_Function_0",
        initializer=[[0.5, 0.5]],
        rate=0.5,
        default_variable=[[0.0, 0.0]],
    ),
    output_ports=["RESULTS"],
    termination_comparison_op=">=",
    default_variable=[[0.0, 0.0]],
)
DECISION = pnl.DDM(
    name="DECISION",
    function=pnl.DriftDiffusionAnalytical(default_variable=[[0.0]]),
    input_ports=[{
        pnl.NAME:
        pnl.ARRAY,
        pnl.VARIABLE: [[0.0, 0.0]],
        pnl.FUNCTION:
        pnl.Reduce(default_variable=[[0.0, 0.0]], weights=[1, -1]),
word_input_to_hidden_wts = np.array([[3, -3], [-3, 3]])
word_hidden = pnl.ProcessingMechanism(
    name="word_hidden", size=2, function=pnl.Logistic(bias=-4)
)
word_hidden_to_output_wts = np.array([[3, -3], [-3, 3]])
word_pathway = [
    word_input,
    word_input_to_hidden_wts,
    word_hidden,
    word_hidden_to_output_wts,
    output,
]

# Construct the task specification pathways
task_input = pnl.ProcessingMechanism(name="task_input", size=2)
task = pnl.LCAMechanism(name="TASK", size=2, initial_value=[0.5, 0.5])
task_color_wts = np.array([[4, 4], [0, 0]])
task_word_wts = np.array([[0, 0], [4, 4]])
task_color_pathway = [task_input, task, task_color_wts, color_hidden]
task_word_pathway = [task_input, task, task_word_wts, word_hidden]

# Construct the decision pathway:
decision = pnl.DDM(name="DECISION", input_format=pnl.ARRAY)
decision_pathway = [output, decision]

# Construct control mechanism
control = pnl.ControlMechanism(
    name="CONTROL",
    objective_mechanism=pnl.ObjectiveMechanism(
        name="Conflict Monitor",
        function=pnl.Energy(size=2, matrix=[[0, -2.5], [-2.5, 0]]),
示例#15
0
    default_variable=[[0.0, 0.0]],
)
word_hidden = pnl.ProcessingMechanism(
    name="word_hidden",
    function=pnl.Logistic(bias=-4, default_variable=[[0.0, 0.0]]),
    default_variable=[[0.0, 0.0]],
)
task_input = pnl.ProcessingMechanism(
    name="task_input",
    function=pnl.Linear(default_variable=[[0.0, 0.0]]),
    default_variable=[[0.0, 0.0]],
)
TASK = pnl.LCAMechanism(
    name="TASK",
    function=pnl.Logistic(default_variable=[[0.0, 0.0]]),
    initial_value=[[0.5, 0.5]],
    output_ports=["RESULTS"],
    termination_comparison_op=">=",
    default_variable=[[0.0, 0.0]],
)
DECISION = pnl.DDM(
    name="DECISION",
    function=pnl.DriftDiffusionAnalytical(default_variable=[[0.0]]),
    input_ports=[
        {
            pnl.FUNCTION: pnl.Reduce(
                default_variable=[[0.0, 0.0]], weights=[1, -1]
            ),
            pnl.NAME: pnl.ARRAY,
            pnl.VARIABLE: [[0.0, 0.0]],
        }
    ],