Scripting Examples

Bar Chart

To get a very basic feel for scripting, go to Scripts > Edit/Run Scriptand, load BarChartDemo.bsh, and try modifying the parameters at the top, before running the simulation.  Try this a few times to see how the parameters affect the simulation.

Creating a simple network from scratch

Here is an example of a simple script to create a few neurons connected by a synapse.   You can use this example to get a feel for creating a script from scratch.

To start, open a text file, copy and paste the text below, and save it as a .bsh file (for example, "SimpleNet.bsh"). Then execute the script, either using Scripts > Edit/Run Script... or by placing it in the {SimbrainHome}/scripts/scriptmenu directory and re-running Simbrain.

import org.simbrain.network.*;
import org.simbrain.network.core.*;
import org.simbrain.network.networks.*;
import org.simbrain.network.neuron_update_rules.*;
import org.simbrain.workspace.*;

{
    NetworkComponent networkComponent = new NetworkComponent("Test Network");
    workspace.addWorkspaceComponent(networkComponent);
    Network network = networkComponent.getNetwork();

    Neuron neuron1 = new Neuron(network, "LinearRule");
    neuron1.setLocation(2,2);
    network.addNeuron(neuron1);

    Neuron neuron2 = new Neuron(network, "LinearRule");
    neuron2.setLocation(50,2);
    network.addNeuron(neuron2);

    network.addSynapse(new Synapse(neuron1, neuron2));

}

Logging hidden unit activations

To do this, you must create a custom update action.   First create a network and add a Backprop subnetwork to it.  Then go to  File > Edit  Update Sequence....  Then click the "add custom action" button.  Then  copy and paste the code below in to the editor:


import org.simbrain.network.core.*;
import org.simbrain.network.subnetworks.*;
import org.simbrain.network.groups.*;
import org.simbrain.network.update_actions.*;

/**
 * Log data from hidden units.
 */
{
    // Object used for standard update.
    BufferedUpdate bufferedUpdate = new BufferedUpdate(network);

    // Customize this
    this.action = new NetworkUpdateAction() {
        public void invoke() {

            // Log hidden unit activations
            BackpropNetwork backpropNet = network.getGroupByLabel("Backprop");
            NeuronGroup hiddenLayer = backpropNet.getNeuronGroupByLabel("Layer 2");
             for (Neuron neuron: hiddenLayer.getNeuronList()) {
                System.out.print(neuron.getActivation() + " ");
             }
             System.out.println();
                               
        }
       
        // This is how the action appears in the update manager dialog
        public String getDescription() {
            return "Print hidden unit activations";
        }
       
        // This is a longer description for the tooltip
        public String getLongDescription() {
            return "Print hidden unit activations";
        }

    };
}

After this, when you run the network it will print out the activations of the hidden units.

Custom update of a neuron

This is another example of using a custom update script, this time to do something custom to the neural activations in a network.  Use the same script as above, but change the block under "Log Hidden Unit Activations" to this:

          // Randomize hidden unit activations
          BackpropNetwork backpropNet = network.getGroupByLabel("Backprop");
          NeuronGroup inputLayer = backpropNet.getNeuronGroupByLabel("Layer 1");
          for (Neuron neuron: inputLayer.getNeuronList()) {
                neuron.forceSetActivation(Math.random());
          }

You should also change the getDescription() and getLongDescription() methods.

This will result in the activations of the input neurons being randomized at every iteration.  Changes like this can be used to customize network update in various ways.