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

Leabra script cleaning #547

Merged
merged 3 commits into from
Nov 20, 2017
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
64 changes: 32 additions & 32 deletions Scripts/Examples/Leabra-Demo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# The following script briefly demos the LeabraMechanism in PsyNeuLink.
# The following script briefly demos the LeabraMechanism in PsyNeuLink by comparing its output with a corresponding
# network from the leabra package.
# Before running this, please make sure you are using Python 3.5, and that you have installed the leabra package in
# your Python 3.5 environment.

Expand All @@ -22,7 +23,7 @@
# with warnings.catch_warnings():
# warnings.simplefilter("ignore")
# import leabra
from psyneulink.library.mechanisms.processing.leabramechanism import LeabraMechanism, build_network
from psyneulink.library.mechanisms.processing.leabramechanism import LeabraMechanism, build_network, train_network
from psyneulink.components.mechanisms.processing.transfermechanism import TransferMechanism
from psyneulink.components.projections.pathway.mappingprojection import MappingProjection
from psyneulink.components.functions.function import Linear
Expand All @@ -31,60 +32,59 @@
import time

random_seed_value = 1 # feel free to change this value
random.seed(random_seed_value) # this (also see random.seed below) is to ensure Leabra network is identical below
random.seed(random_seed_value) # setting the random seed ensures the two Leabra networks are identical (see line 68)
num_trials = 10 # how many trials should we run?
input_size = 4 # how big is the input layer?
output_size = 2 # how big is the output layer?
hidden_layers = 4 # how many hidden layers are there?
hidden_sizes = [2, 3, 4, 5] # how big is each hidden layer?
input_pattern = [[0, 1, 3, 4]] * num_trials # the input
input_pattern = np.repeat(np.array([[0, 1, 3, 4]]), num_trials, axis=0) # the input
print("inputs to the networks will be: ", input_pattern)
# similar example: input_pattern = [[0, 1, 3, 4]] * int(num_trials/2) + [[0, 0, 0, 0]] * int(num_trials/2)
training_pattern = [[0, 1]] * num_trials # the training pattern
training_pattern = np.repeat(np.array([[0, 0, 0]]), num_trials, axis=0) # the training pattern
print("training inputs to the networks will be: ", training_pattern)
input_size = len(input_pattern[0]) # how big is the input layer of the network?
output_size = len(training_pattern[0]) # how big is the output layer of the network?
train_flag = False # should the LeabraMechanism and leabra network learn?
# NOTE: there is currently a bug with training, in which the output may differ between trials, randomly
# ending up in one of two possible outputs. Running this script repeatedly will make this behavior clear.
# The leabra network and LeabraMechanism experience this bug equally.

# the LeabraMechanism of interest
# NOTE: The reason TransferMechanisms are used below is because there is currently a bug where LeabraMechanism
# (and other `Mechanism`s with multiple input states) cannot be used as origin Mechanisms for a System. If you desire
# to use a LeabraMechanism as an origin Mechanism, you can work around this bug by creating two `TransferMechanism`s
# as origin Mechanisms instead, and have these two TransferMechanisms pass their output to the InputStates of
# the LeabraMechanism.

# create a LeabraMechanism in PsyNeuLink
L = LeabraMechanism(input_size=input_size, output_size=output_size, hidden_layers=hidden_layers,
hidden_sizes=hidden_sizes, name='L', training_flag=True)
hidden_sizes=hidden_sizes, name='L', training_flag=train_flag)


T1 = TransferMechanism(name='T1', size=4, function=Linear)
T2 = TransferMechanism(name='T2', size=2, function=Linear)
T1 = TransferMechanism(name='T1', size=input_size, function=Linear)
T2 = TransferMechanism(name='T2', size=output_size, function=Linear)

p1 = Process(pathway=[T1, L])
proj = MappingProjection(sender=T2, receiver=L.input_states[1])
p2 = Process(pathway=[T2, L])
T2.output_state.efferents[1].matrix = T2.output_state.efferents[1].matrix * 0
p2 = Process(pathway=[T2, proj, L])
s = System(processes=[p1, p2])

print('Running Leabra in PsyNeuLink...')
start_time = time.process_time()
outputs = s.run(inputs={T1: input_pattern.copy(), T2: training_pattern.copy()})
end_time = time.process_time()

print('PNL time to run: ', end_time - start_time, "seconds")
print('PNL Outputs Over Time: ', outputs, type(outputs))
print('PNL Final Output: ', outputs[-1], type(outputs[-1]))
print('Time to run LeabraMechanism in PsyNeuLink: ', end_time - start_time, "seconds")
print('LeabraMechanism Outputs Over Time: ', outputs, type(outputs))
print('LeabraMechanism Final Output: ', outputs[-1], type(outputs[-1]))


random.seed(random_seed_value)
leabra_net = build_network(n_input=input_size, n_output=output_size, n_hidden=hidden_layers, hidden_sizes=hidden_sizes)
leabra_net.set_inputs({'input_layer': np.zeros(4)})
leabra_net.trial()

def train_network(network, input_pattern, output_pattern):
"""Run one trial on the network"""
assert len(network.layers[0].units) == len(input_pattern)

assert len(network.layers[-1].units) == len(output_pattern)
network.set_inputs({'input_layer': input_pattern})
network.set_outputs({'output_layer': output_pattern})

network.trial()
return [unit.act_m for unit in network.layers[-1].units]
leabra_net = build_network(n_input=input_size, n_output=output_size, n_hidden=hidden_layers, hidden_sizes=hidden_sizes,
training_flag=train_flag)

print('\nRunning Leabra in Leabra...')
start_time = time.process_time()
for i in range(num_trials):
train_network(leabra_net, input_pattern[i].copy(), training_pattern[i])
train_network(leabra_net, input_pattern[i].copy(), training_pattern[i].copy())
end_time = time.process_time()
print('Leabra time to run: ', end_time - start_time, "seconds")
print('Time to run Leabra on its own: ', end_time - start_time, "seconds")
print('Leabra Output: ', [unit.act_m for unit in leabra_net.layers[-1].units], type([unit.act_m for unit in leabra_net.layers[-1].units][0]))
83 changes: 0 additions & 83 deletions Scripts/Examples/Leabra-Test.py

This file was deleted.

6 changes: 6 additions & 0 deletions docs/source/LeabraMechanism.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LeabraMechanism
===============

.. automodule:: psyneulink.library.mechanisms.processing.leabramechanism
:members:
:exclude-members: random
18 changes: 16 additions & 2 deletions tests/mechanisms/test_leabra_mechanism.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@
from psyneulink.library.mechanisms.processing.leabramechanism import LeabraMechanism, build_network, train_network
from psyneulink.components.mechanisms.processing.transfermechanism import TransferMechanism
from psyneulink.components.projections.pathway.mappingprojection import MappingProjection
from psyneulink.components.functions.function import Linear
from psyneulink.components.functions.function import Linear, Logistic
from psyneulink.components.process import Process
from psyneulink.components.system import System
from psyneulink.globals.keywords import LEARNING

@pytest.mark.skipif(not leabra_available, reason='Leabra package is unavailable')
class TestLeabraMechanismInit:
Expand Down Expand Up @@ -219,4 +220,17 @@ def test_leabra_prec_half_train(self):
out_net = s_net.run(inputs={T1_net: inputs[i], T2_net: train_data[i]})
pnl_output_net = out_net[-1][0]
diffs_net = np.abs(np.array(pnl_output_net) - np.array(leabra_output))
assert all(diffs_spec < precision) and all(diffs_net < precision)
assert all(diffs_spec < precision) and all(diffs_net < precision)
#
# class TestLeabraMechanismInSystem:
#
# def test_leabra_mech_learning(self):
# T1 = TransferMechanism(size=5, function=Linear)
# T2 = TransferMechanism(size=3, function=Linear)
# L = LeabraMechanism(input_size=5, output_size=3, hidden_layers=2, hidden_sizes=[4, 4])
# train_data_proj = MappingProjection(sender=T2, receiver=L.input_states[1])
# out = TransferMechanism(size=3, function=Logistic(bias=2))
# p1 = Process(pathway=[T1, L, out], learning=LEARNING, learning_rate=1.0, target=[0, .1, .8])
# p2 = Process(pathway=[T2, train_data_proj, L, out])
# s = System(processes=[p1, p2])
# s.run(inputs = {T1: [1, 2, 3, 4, 5], T2: [0, .5, 1]})