{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Welcome to PsyNeuLink\n", "\n", "PsyNeuLink is an integrated language and toolkit for creating cognitive models. It decreases the overhead required for cognitive modeling by providing standard building blocks (DDMS, Neural Nets, etc.) and the means to connect them together in a single environment. PsyNeuLink is designed to make the user think about computation in a \"mind/brain-like\" way while imposing minimal constraint on the type of models that can be implemented." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true }, "source": [ "## How to get PsyNeuLink\n", "\n", "PsyNeuLink is compatible with python versions >= 3.5, and is available through PyPI:\n", "\n", "```python\n", "pip install psyneulink\n", "```\n", "Or you can clone the github repo [here](https://github.com/PrincetonUniversity/PsyNeuLink). Download the package with the green \"Clone or download\" button on the right side of the page and \"Download ZIP.\" Open the version of this Tutorial in the cloned folder before continuing on.\n", "\n", "## Installation\n", "\n", "To install the package, navigate to the cloned directory in a terminal, switch to your preferred python3 environment, then run the command __\"pip install .\"__ (make sure to include the period and to use the appropriate pip/pip3 command for python 3.5). All prerequisite packages will be automatically added to your enviroment.\n", "\n", "For the curious, these are:\n", "* numpy\n", "* matplotlib\n", "* toposort\n", "* mpi4py\n", "* typecheck-decorator" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\n", "## Tutorial Overview\n", "\n", "This tutorial is meant to get you accustomed to the structure of PsyNeuLink and be able to construct basic models. Starting with a simple 1-to-1 transformation, we will build up to making the Stroop model from Cohen et al. (1990). Let's get started!" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Imports and file structure\n", "\n", "The following code block will import the necessary components for basic neural network models in PsyNeuLink. In particular, we need tools for handling *[systems](https://princetonuniversity.github.io/PsyNeuLink/System.html)*, *[processes](https://princetonuniversity.github.io/PsyNeuLink/Process.html)*, the set of specific *[mechanisms](https://princetonuniversity.github.io/PsyNeuLink/Mechanism.html)* that will make up our networks, and the *[projections](https://princetonuniversity.github.io/PsyNeuLink/Projection.html)* to connect them. We also import basic prerequisites and set up the jupyter environment for visualization." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import psyneulink as pnl" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "% matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Creating a mechanism\n", "\n", "*[Mechanisms](https://princetonuniversity.github.io/PsyNeuLink/Mechanism.html)* are the basic units of computation in PsyNeuLink. At their core is a parameterized *function* but they also contain the machinery to interact with input, output, control, and learning signals. Our first mechanism will perform a linear transformation on a scalar input. For now, we will initialize it by just specifying the *function* of the mechanism." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_mechanism = pnl.TransferMechanism(\n", " function=pnl.Linear(slope=1, intercept=0))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In this case, we didn't actually need to specify the slope and intercept as the function will default to reasonable values (in this case, 1 and 0 respectively)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_mechanism.execute([1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Try reparamaterizing the mechanism and executing again before moving on." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Organizing into Processes\n", "\n", "Generally with PsyNeuLink, you won't be executing mechanisms as stand-alone entities. Rather, they will be encapsulated inside *[Processes](https://princetonuniversity.github.io/PsyNeuLink/Process.html)* and later *[Systems](https://princetonuniversity.github.io/PsyNeuLink/System.html)*. A process is a collection of mechanisms and projections to be executed serially. They may be thought of as a way to concatenate mechanisms into a single, more complex unit of analysis in order to simplify interacting with larger models.\n", "\n", "The main parameter when initializing a process is its pathway, which is the order in which the mechanisms will execute. Of course, with only one mechanism in our process, the list has just one element.\n", "\n", "To better see how the process runs, we also turn on output reporting. Reporting can happen at every level in PsyNeuLink so we set the preference for both the process and mechanism." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_process = pnl.Process(pathway = [linear_transfer_mechanism])\n", "\n", "linear_transfer_mechanism.reportOutputPref = True\n", "linear_transfer_process.reportOutputPref = True" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_process.execute([4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Let's turn off the reporting and look at our process' output over a wider range of values." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_mechanism.reportOutputPref = False\n", "linear_transfer_process.reportOutputPref = False\n", "\n", "xVals = np.linspace(-3, 3, num=51)\n", "yVals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " yVals[i] = linear_transfer_process.execute([xVals[i]])[0]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, yVals)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The previous cell provides a way to plot the net input-output function of a composed Process. If you want to see the function associated with an individual Mechanism, there is simple way to do this (and because the current Process is only has one Mechanism the plotted function will be the same):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_transfer_mechanism.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now let's put it all together and make a new transfer process, this time with a logistic activation function. We will also extend our mechanism by giving it two units (operating on a 1x2 matrix) rather than the default one (operating on a scalar)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create the mechanism\n", "logistic_transfer_mechanism = pnl.TransferMechanism(default_variable=[0, 0],\n", " function=pnl.Logistic(gain=1,\n", " bias=0))\n", "\n", "# Package into a process\n", "logistic_transfer_process = pnl.Process(pathway=[logistic_transfer_mechanism])\n", "\n", "# Iterate and plot\n", "xVals = np.linspace(-3, 3, num=51)\n", "y1Vals = np.zeros((51,))\n", "y2Vals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " # clarify why multiplying times 2\n", " output = logistic_transfer_process.execute([xVals[i], xVals[i] * 2])\n", " y1Vals[i] = output[0]\n", " y2Vals[i] = output[1]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, y1Vals)\n", "plt.plot(xVals, y2Vals)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The `default_variable` parameter serves a dual function. It specifies the dimensionality of the mechanism as well as providing the inputs that will be given in the absence of explicit input at runtime." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can also think of the functions in mechanisms as simple building blocks, and it is possible to combine them to create a wide variety of different functions, as in the next two examples." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toymech2 = pnl.TransferMechanism(\n", " name='toymech2',\n", " function=pnl.Logistic(gain = -20, bias = -.5)\n", ")\n", "\n", "toymech3 = pnl.TransferMechanism(\n", " name='toymech3',\n", " function=pnl.Logistic(gain = -20, bias = .5),\n", ")\n", "\n", "toymech4 =pnl.TransferMechanism(\n", " name='toymech4',\n", " function=pnl.Linear()\n", ")\n", "\n", "toyprocess1 = pnl.Process(\n", " pathway=[toymech2, toymech4] \n", ")\n", "\n", "toyprocess2 = pnl.Process(\n", " pathway=[toymech3, toymech4] \n", ")\n", "\n", "\n", "toysystem2 = pnl.System(processes=[toyprocess1, toyprocess2],\n", " name='SumLog')\n", "\n", "toymech2.reportOutputPref = False\n", "toymech3.reportOutputPref = False\n", "toymech4.reportOutputPref = False\n", "\n", "# Iterate and plot\n", "xVals = np.linspace(-2, 2, num=51)\n", "yVals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " yVals[i] = toysystem2.execute([[xVals[i]],[xVals[i]]])[0]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, yVals)\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "toymech2 = pnl.TransferMechanism(\n", " name='toymech2',\n", " function=pnl.Logistic(gain = 3, bias = -.5)\n", ")\n", "\n", "toymech3 = pnl.TransferMechanism(\n", " name='toymech3',\n", " function=pnl.Logistic(gain = -3, bias = .5),\n", ")\n", "\n", "toymech4 =pnl.TransferMechanism(\n", " name='toymech4',\n", " function=pnl.Linear()\n", ")\n", "\n", "toyprocess1 = pnl.Process(\n", " pathway=[toymech2, toymech4] \n", ")\n", "\n", "toyprocess2 = pnl.Process(\n", " pathway=[toymech3, toymech4] \n", ")\n", "\n", "\n", "toysystem2 = pnl.System(processes=[toyprocess1, toyprocess2],\n", " name='SumLog')\n", "\n", "toymech2.reportOutputPref = False\n", "toymech3.reportOutputPref = False\n", "toymech4.reportOutputPref = False\n", "\n", "# Iterate and plot\n", "xVals = np.linspace(-2, 2, num=51)\n", "yVals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " yVals[i] = toysystem2.execute([[xVals[i]],[xVals[i]]])[0]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, yVals)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Adding Projections\n", "\n", "To make more complex processes, we need a way to link mechanisms together. This is done through *[Projections](https://princetonuniversity.github.io/PsyNeuLink/Projection.html)*. A projection takes a mechanism output, multiplies it by the projection's mapping matrix, and delivers the transformed value to the next mechanism in the process." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_input_unit = pnl.TransferMechanism(function=pnl.Linear(slope=2, intercept=2))\n", "logistic_output_unit = pnl.TransferMechanism(function=pnl.Logistic())\n", "mini_connected_network = pnl.Process(\n", " pathway=[linear_input_unit, pnl.IDENTITY_MATRIX, logistic_output_unit])\n", "\n", "# Iterate and plot\n", "xVals = np.linspace(-3, 3, num=51)\n", "yVals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " yVals[i] = mini_connected_network.execute([xVals[i]])[0]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, yVals)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you want to plot the two Mechanism individually, the following cell allows you to do this. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_input_unit.plot()\n", "logistic_output_unit.plot()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`IDENTITY_MATRIX` is a keyword that provides a projection from the unit preceding it to the unit following that creates a one-to-one output to input projection between the two. Other useful projection keywords are [here](https://princetonuniversity.github.io/PsyNeuLink/Keywords.html#psyneulink.globals.keywords.MatrixKeywords.IDENTITY_MATRIX)\n", "\n", "Now let's make our projection definition a bit more explicit." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "linear_input_unit = pnl.TransferMechanism(function=pnl.Linear(slope=2, intercept=2))\n", "logistic_output_unit = pnl.TransferMechanism(function=pnl.Logistic())\n", "\n", "mapping_matrix = np.asarray([[1]])\n", "unit_mapping_projection = pnl.MappingProjection(sender=linear_input_unit,\n", " receiver=logistic_output_unit,\n", " matrix=mapping_matrix)\n", "mini_connected_network = pnl.Process(\n", " pathway=[linear_input_unit, unit_mapping_projection, logistic_output_unit])\n", "\n", "# Iterate and plot\n", "xVals = np.linspace(-3, 3, num=51)\n", "yVals = np.zeros((51,))\n", "for i in range(xVals.shape[0]):\n", " yVals[i] = mini_connected_network.execute([xVals[i]])[0]\n", " # Progress bar\n", " print(\"-\", end=\"\")\n", "plt.plot(xVals, yVals)\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This time we specified our mapping matrix (which is a 2-D numpy array) then explicitly initialized a *[MappingProjection](https://princetonuniversity.github.io/PsyNeuLink/MappingProjection.html)* with that matrix as well as its input and output mechanisms. Note: because we specified the input and output mechanisms in the projection itself, we didn't need to include it in the process pathway as it will infer its position from those parameters. Ultimately, however, this does the exact same thing as our keyword method above which is far less verbose for this common use case." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Systems\n", "\n", "The highest level at which models are considered in PsyNeuLink is that of the *[System](https://princetonuniversity.github.io/PsyNeuLink/System.html)*. A system is composed of one or more processes which can then run in unison. This allows system graphs to be more complex than the strictly linear ones of processes. Our first system will consist of two input nodes that converge on a single output mechanism. We will be modelling competition between color naming and word reading in the stroop task." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "colors = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Linear,\n", " name=\"Colors\")\n", "words = pnl.TransferMechanism(default_variable=[0, 0],\n", " function=pnl.Linear(slope=1.5), name=\"Words\")\n", "response = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Logistic,\n", " name=\"Response\")\n", "\n", "color_naming_process = pnl.Process(pathway=[colors, pnl.IDENTITY_MATRIX, response],\n", " name=\"Color Naming\")\n", "word_reading_process = pnl.Process(pathway=[words, pnl.IDENTITY_MATRIX, response],\n", " name=\"Word Reading\")\n", "\n", "# Should pass in scheduler or have system create new scheduler if scheduler=None\n", "mini_stroop = pnl.System(processes=[color_naming_process, word_reading_process],\n", " name='Stroop Model')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the code above \"colors\" are assigned the default Linear function values of slope = 1, and intercept = 0. And \"words\" are explicitly assigned a slope of 1.5. These slope values can be thought of as weights, asserting that words will be processed more than colors.\n", "\n", "The order of processes in the system initialization is important here as it is the order we will feed inputs to the system. \n", "\n", "We can also visually inspect the system by creating a graph: " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mini_stroop.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "PsyNeuLink provides multiple convenient ways to inspect a model (to better understand existing models, and to debug and trouble-shoot while building your own models). Along with turning on reporting, you can use the system_name.show() to get a summary. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "mini_stroop.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we are ready to give the system inputs and run the model. In this very simple model two possible color inputs are distinguished as [1, 0] or [0, 1], and likewise for words. For both color and word you can think of [1, 0] as red, and [0, 1] as blue. In the following cell, the input is the word \"blue\" [0, 1] written in red colored ink [1, 0]. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input = { words: [0, 1],\n", " colors: [1, 0]}\n", "\n", "mini_stroop.run(input)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first output value corresponds to the strength to respond red, and the second value is the strength to respond blue. Because the task is to respond to the word, and the word is blue, the second value is higher. And because the ink is colored red, there is also some activity associated with red. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_dict = {colors: [0, 1],\n", " words: [0, 1]}\n", "mini_stroop.run(input_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The first pair of values in the output is from our first run of the model with incongruent input [red color / blue word]and the second pair of values is from the second run with congruent input [blue / blue]. As modeled, we see that word reading beats out color naming when there is conflict, but congruent stimuli elicit an even stronger response. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Pre-trained Complete Stroop Model\n", "\n", "Let's practice using systems by recreating the more complex stroop model from Cohen et al (1990). Later we will train the network ourselves, but for now we will explicitly model the learned weights." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "ink_color = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Linear())\n", "word = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Linear())\n", "task_demand = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Linear())\n", "\n", "hidden_layer = pnl.TransferMechanism(default_variable=[0, 0, 0, 0],\n", " function=pnl.Logistic(bias=-4))\n", "\n", "output_layer = pnl.TransferMechanism(default_variable=[0, 0], function=pnl.Linear())\n", "\n", "color_mapping_matrix = np.asarray([[2.2, -2.2, 0, 0], [-2.2, 2.2, 0, 0]])\n", "color_projection = pnl.MappingProjection(sender=ink_color, receiver=hidden_layer,\n", " matrix=color_mapping_matrix)\n", "word_mapping_matrix = np.asarray([[0, 0, 2.6, -2.6], [0, 0, -2.6, 2.6]])\n", "word_projection = pnl.MappingProjection(sender=word, receiver=hidden_layer,\n", " matrix=word_mapping_matrix)\n", "task_mapping_matrix = np.asarray([[4, 4, 0, 0], [0, 0, 4, 4]])\n", "task_projection = pnl.MappingProjection(sender=task_demand, receiver=hidden_layer,\n", " matrix=task_mapping_matrix)\n", "output_mapping_matrix = np.asarray(\n", " [[1.3, -1.3], [-1.3, 1.3], [2.5, -2.5], [-2.5, 2.5]])\n", "pnl.MappingProjection(sender=hidden_layer, receiver=output_layer,\n", " matrix=output_mapping_matrix)\n", "\n", "color_naming_process = pnl.Process(pathway=[ink_color, hidden_layer, output_layer])\n", "word_reading_process = pnl.Process(pathway=[word, hidden_layer, output_layer])\n", "task_process = pnl.Process(pathway=[task_demand, hidden_layer, output_layer])\n", "\n", "stroop_model = pnl.System(\n", " processes=[color_naming_process, word_reading_process, task_process])\n", "\n", "ink_color.reportOutputPref = True\n", "word.reportOutputPref = True\n", "task_demand.reportOutputPref = True\n", "hidden_layer.reportOutputPref = True" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In the next cell we will run the model with inputs. The \"ink_color\" and \"word\" are the same as the previous model, and the addition of task demand allows us to specify whether the task is to name the color of ink [0, 1], or to read the word [1, 0]. The output can be thought of as activation strengths of two possible responses [red, blue]. " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_dict = {ink_color: [1, 0],\n", " word: [0, 1],\n", " task_demand: [1, 0]}\n", "stroop_model.run(input_dict)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "To get a better sense of how the model works, try reverse engineering by changing each of the inputs (remember the options are only [1,0] or [0,1]) one at a time and running the model. \n", "\n", "We can also inspect the model as a graph or using .show(). " ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stroop_model.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "stroop_model.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can see that this model's components were not given names, so the graph and summary are more difficult to understand. To get little practice modifying the code, you can add names using the same syntax as was used for the mini_stroop model above. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Configuring Systems\n", "\n", "The following cells provide some basic examples of how to build extremely simple to increasingly complex systems. These systems aren't configured to do anything in particular -- they are merely included to show you how to get started building systems with different graph configurations. Input mechanisms are green and output mechanisms are red" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_layer = pnl.TransferMechanism(\n", " name='input_layer',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "output_layer = pnl.TransferMechanism(\n", " name='output_layer',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "simple_process1 = pnl.Process(pathway=[input_layer, output_layer])\n", "\n", "simple_system1 = pnl.System(\n", " processes=[simple_process1])\n", "\n", "\n", "simple_system1.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_layer2 = pnl.TransferMechanism(\n", " name='input_layer2',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "\n", "hidden_layer2 = pnl.TransferMechanism(\n", " name='hidden_layer2',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "output_layer2 = pnl.TransferMechanism(\n", " name='output_layer2',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "simple_process2 = pnl.Process(pathway=[input_layer2, hidden_layer2, output_layer2])\n", "\n", "simple_system2 = pnl.System(processes=[simple_process2])\n", "\n", "## the previous two lines of code builiding a 3 layer system could alternatively take the following form:\n", "\n", "# simple_process2a = process(pathway=[input_layer2, hidden_layer2])\n", "# simple_process2b = process(pathway=[hidden_layer2, output_layer2])\n", "#\n", "# simple_system2 = system(processes=[simple_process2a, simple_process2b])\n", "\n", "\n", "\n", "simple_system2.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_layer3 = pnl.TransferMechanism(\n", " name='input_layer3',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "\n", "hidden_layer3a = pnl.TransferMechanism(\n", " name='hidden_layer3a',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "hidden_layer3b = pnl.TransferMechanism(\n", " name='hidden_layer3b',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "output_layer3 = pnl.TransferMechanism(\n", " name='output_layer3',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "simple_process3a = pnl.Process(pathway=[input_layer3, hidden_layer3a])\n", "simple_process3b = pnl.Process(pathway=[hidden_layer3a, hidden_layer3b])\n", "simple_process3c = pnl.Process(pathway=[hidden_layer3b, hidden_layer3a])\n", "simple_process3d = pnl.Process(pathway=[hidden_layer3a, hidden_layer3b])\n", "simple_process3e = pnl.Process(pathway=[hidden_layer3b, output_layer3])\n", "\n", "\n", "simple_system3 = pnl.System(processes=[simple_process3a, simple_process3b, simple_process3c, simple_process3d, simple_process3e])\n", "\n", "\n", "simple_system3.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "input_layer4 = pnl.TransferMechanism(\n", " name='input_layer4',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "hidden_layer4a = pnl.TransferMechanism(\n", " name='hidden_layer4a',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "hidden_layer4b = pnl.TransferMechanism(\n", " name='hidden_layer4b',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "hidden_layer4c = pnl.TransferMechanism(\n", " name='hidden_layer4c',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "output_layer4 = pnl.TransferMechanism(\n", " name='output_layer4',\n", " function=pnl.Linear,\n", " default_variable=np.ones((4,)),\n", ")\n", "\n", "simple_process4a = pnl.Process(pathway=[input_layer4, hidden_layer4a, output_layer4])\n", "simple_process4b = pnl.Process(pathway=[input_layer4, hidden_layer4b, output_layer4])\n", "simple_process4c = pnl.Process(pathway=[input_layer4, hidden_layer4c, output_layer4])\n", "\n", "\n", "simple_system4 = pnl.System(processes=[simple_process4a, simple_process4b, simple_process4c])\n", "\n", "\n", "simple_system4.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "preinputmech1c = pnl.TransferMechanism(\n", " name='preinputmech1c',\n", " function=pnl.Linear(slope=1, intercept=0),\n", " size = 2\n", ")\n", "\n", "inputmech1c = pnl.TransferMechanism(\n", " name='inputmech1c',\n", " function=pnl.Linear(slope=1, intercept=0),\n", " size = 2\n", ")\n", "\n", "opposemech1c = pnl.TransferMechanism(\n", " name='opposemech1c',\n", " #function=pnl.Logistic(gain=3,bias=1),\n", " function=pnl.Linear(),\n", " size = 2\n", ")\n", "\n", "opposemech2c = pnl.TransferMechanism(\n", " name='opposemech2c',\n", " #function=pnl.Logistic(gain=3,bias=1),\n", " function=pnl.Linear(),\n", " size = 2\n", ")\n", "\n", "fatmech1c = pnl.TransferMechanism(\n", " name='fatmech1c',\n", " function=pnl.Logistic()\n", ")\n", "\n", "fatmech2c = pnl.TransferMechanism(\n", " name='fatmech2c',\n", " function=pnl.Logistic(),\n", ")\n", "\n", "fatmech3c =pnl.TransferMechanism(\n", " name='fatmech3c',\n", " function=pnl.Linear()\n", ")\n", "\n", "fatmech4c = pnl.TransferMechanism(\n", " name='fatmech4c',\n", " function=pnl.Logistic()\n", ")\n", "\n", "fatmech5c = pnl.TransferMechanism(\n", " name='fatmech5c',\n", " function=pnl.Logistic(),\n", ")\n", "\n", "fatmech6c =pnl.TransferMechanism(\n", " name='fatmech6c',\n", " function=pnl.Linear()\n", ")\n", "\n", "# Create projections\n", "xy_mapmat = np.array(((1,0),(0,-1)))\n", "yx_mapmat = np.array(((-1,0),(0,1)))\n", " \n", "\n", "# Create processes\n", "\n", "toyprocess1cp = pnl.Process(\n", " pathway=[preinputmech1c, inputmech1c]\n", ")\n", "\n", "toyprocess1c = pnl.Process(\n", " pathway=[inputmech1c, xy_mapmat, opposemech1c]\n", ")\n", "\n", "toyprocess2c = pnl.Process(\n", " pathway=[inputmech1c, yx_mapmat, opposemech2c] \n", ")\n", "\n", "toyprocess3c = pnl.Process(\n", " pathway=[opposemech1c, fatmech1c, fatmech3c] \n", ")\n", "\n", "toyprocess4c = pnl.Process(\n", " pathway=[opposemech1c, fatmech2c, fatmech3c] \n", ")\n", "\n", "toyprocess5c = pnl.Process(\n", " pathway=[opposemech2c, fatmech4c, fatmech6c] \n", ")\n", "\n", "toyprocess6c = pnl.Process(\n", " pathway=[opposemech2c, fatmech5c, fatmech6c] \n", ")\n", "\n", "toyprocess7c = pnl.Process(\n", " pathway=[fatmech3c, inputmech1c] \n", ")\n", "\n", "toyprocess8c = pnl.Process(\n", " pathway=[fatmech6c, inputmech1c] \n", ")\n", "\n", "# Create system\n", "\n", "\n", "toysystem1c = pnl.System(processes=[toyprocess1cp, toyprocess1c, toyprocess2c, toyprocess3c, toyprocess4c, toyprocess5c, toyprocess6c, toyprocess7c, toyprocess8c],\n", " name='toysystem1c')\n", "\n", "toysystem1c.show_graph(output_fmt = 'jupyter')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This is currently the end of the tutorial, but more content is being added weekly. For further examples, look to the Scripts folder inside your PsyNeuLink directory for several functioning models." ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.3" } }, "nbformat": 4, "nbformat_minor": 1 }