Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/mechanisms/ execute #754

Merged
merged 19 commits into from
Apr 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .idea/runConfigurations/Make_HTML.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Scripts/Examples/EVC-Gratton.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@

# Show graph of system (with control components)
# mySystem.show_graph(show_dimensions=pnl.ALL, show_projection_labels=True)
# mySystem.show_graph(show_control=True, show_projection_labels=False)
mySystem.show_graph(show_control=True, show_mechanism_structure=True, show_headers=False)
mySystem.show_graph(show_control=True, show_projection_labels=False)
# mySystem.show_graph(show_control=True, show_mechanism_structure=True, show_headers=False)

# configure EVC components
mySystem.controller.control_signals[0].intensity_cost_function = pnl.Exponential(rate=0.8046).function
Expand Down
134 changes: 134 additions & 0 deletions Scripts/Examples/Multilayer-Learning FOR FIG.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
import functools
import numpy as np
import psyneulink as pnl

Input_Layer = pnl.TransferMechanism(
name='Input',
function=pnl.Logistic,
params={pnl.INPUT_LABELS_DICT:{'red': [-1, 30]}},
default_variable=np.zeros((2,)))

Hidden_Layer_1 = pnl.TransferMechanism(
name='Hidden1',
function=pnl.Logistic(),
default_variable=np.zeros((5,)))

Hidden_Layer_2 = pnl.TransferMechanism(
name='Hidden2',
function=pnl.Logistic(),
default_variable=[0, 0, 0, 0])

Output_Layer = pnl.TransferMechanism(
name='Output',
function=pnl.Logistic,
default_variable=[0, 0, 0])

Input_Weights_matrix = (np.arange(2 * 5).reshape((2, 5)) + 1) / (2 * 5)
Middle_Weights_matrix = (np.arange(5 * 4).reshape((5, 4)) + 1) / (5 * 4)
Output_Weights_matrix = (np.arange(4 * 3).reshape((4, 3)) + 1) / (4 * 3)

# This Projection will be used by the Process below by referencing it in the Process' pathway;
# note: sender and receiver args don't need to be specified
Input_Weights = pnl.MappingProjection(
name='Input Weights',
matrix=Input_Weights_matrix
)

# This Projection will be used by the Process below by assigning its sender and receiver args
# to mechanisms in the pathway
Middle_Weights = pnl.MappingProjection(
name='Middle Weights',
sender=Hidden_Layer_1,
receiver=Hidden_Layer_2,
matrix=Middle_Weights_matrix
)

# Treated same as Middle_Weights Projection
Output_Weights = pnl.MappingProjection(
name='Output Weights',
sender=Hidden_Layer_2,
receiver=Output_Layer,
matrix=Output_Weights_matrix
)

z = pnl.Process(
default_variable=[0, 0],
pathway=[
Input_Layer,
# The following reference to Input_Weights is needed to use it in the pathway
# since it's sender and receiver args are not specified in its declaration above
Input_Weights,
Hidden_Layer_1,
# Middle_Weights,
# No Projection specification is needed here since the sender arg for Middle_Weights
# is Hidden_Layer_1 and its receiver arg is Hidden_Layer_2
Hidden_Layer_2,
# Output_Weights,
# Output_Weights does not need to be listed for the same reason as Middle_Weights
# If Middle_Weights and/or Output_Weights is not declared above, then the Process
# will assign a default for rhe missing Projection
Output_Layer
],
clamp_input=pnl.SOFT_CLAMP,
learning=pnl.LEARNING,
target=[0, 0, 1],
prefs={
pnl.VERBOSE_PREF: False,
pnl.REPORT_OUTPUT_PREF: True
}
)


def print_header(system):
print("\n\n**** Time: ", system.scheduler_processing.clock.simple_time)


def show_target(system):
i = system.input
t = system.target_input_states[0].value
print('\nOLD WEIGHTS: \n')
print('- Input Weights: \n', Input_Weights.matrix)
print('- Middle Weights: \n', Middle_Weights.matrix)
print('- Output Weights: \n', Output_Weights.matrix)

print('\nSTIMULI:\n\n- Input: {}\n- Target: {}\n'.format(i, t))
print('ACTIVITY FROM OLD WEIGHTS: \n')
print('- Middle 1: \n', Hidden_Layer_1.value)
print('- Middle 2: \n', Hidden_Layer_2.value)
print('- Output:\n', Output_Layer.value)


mySystem = pnl.System(
processes=[z],
targets=[0, 0, 1],
learning_rate=2.0
)

# Log Middle_Weights of MappingProjection to Hidden_Layer_2
# Hidden_Layer_2.set_log_conditions('Middle Weights')
Middle_Weights.set_log_conditions('matrix')

mySystem.reportOutputPref = True
# Shows graph will full information:
# mySystem.show_graph(show_dimensions=pnl.ALL)
mySystem.show_graph()
# 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=10,
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)}
)

# 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='matrix', header=False))
2 changes: 1 addition & 1 deletion Scripts/Examples/Multilayer-Learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def show_target(system):
mySystem.reportOutputPref = True
# Shows graph will full information:
# mySystem.show_graph(show_dimensions=pnl.ALL)
mySystem.show_graph(show_learning=pnl.ALL, show_dimensions=pnl.ALL)
mySystem.show_graph(show_learning=True)
# mySystem.show_graph(show_learning=pnl.ALL, show_dimensions=pnl.ALL, show_mechanism_structure=True)
# Shows minimal graph:
# mySystem.show_graph()
Expand Down
18 changes: 18 additions & 0 deletions Scripts/Laura Stroop w EVC.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import numpy as np
import matplotlib.pyplot as plt
import psyneulink as pnl

ci = pnl.TransferMechanism(size=2, name='COLORS INPUT')
wi = pnl.TransferMechanism(size=2, name='WORDS INPUT')
ch = pnl.TransferMechanism(size=2, function=pnl.Logistic, name='COLORS HIDDEN')
wh = pnl.TransferMechanism(size=2, function=pnl.Logistic, name='WORDS HIDDEN')
tl = pnl.TransferMechanism(size=2, function=pnl.Logistic(gain=pnl.CONTROL), name='TASK CONTROL')
rl = pnl.LCA(size=2, function=pnl.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()
3 changes: 2 additions & 1 deletion Scripts/Laura Stroop.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ def pass_threshold(mech1, mech2, thresh):
respond_green_accumulator.reinitialize(0)
respond_red_accumulator.reinitialize(0)
# now run test trial
my_Stroop.show_graph(show_mechanism_structure=pnl.VALUES)
my_Stroop.show_graph()
# my_Stroop.show_graph(show_mechanism_structure=pnl.VALUES)
my_Stroop.run(inputs=CN_incongruent_trial_input, termination_processing=terminate_trial)


Expand Down
Loading