def connectLayers(self, sourceLayer, destLayer,
                   interconnected=False, recurrent=False, peephole=False):
     """
     Connects the |sourceLayer| to the |destLayer|.
     If |interconnected|, the layers are connected fully.
     Otherwise, the layers must be the same length, and will be connected
         one-to-one.
     If |recurrent|, this connection is a connection forward in time.
     If |peephole|, this connection is a peephole connection that uses the
         input neuron's state rather than output.
     """
     connections = []
     if interconnected is False:
         if len(sourceLayer) != len(destLayer):
             raise ValueError("Cannot create non-interconnected layer \
                     between layers of different sizes.")
         for source,destination in zip(sourceLayer, destLayer):
             connections.append(
                 connectNeurons(source, destination, recurrent=recurrent, peephole=peephole))
     else:
         for destination in destLayer:
             for source in sourceLayer:
                 connections.append(
                     connectNeurons(source, destination, recurrent=recurrent, peephole=peephole))
     self.connections = self.connections.union(connections)
     if recurrent:
         self.recurrentConnections = self.recurrentConnections.union(connections)
     return connections
    def addSelfRecurrence(self, layer, gateLayer=None):
        """
        Adds a self recurrent connection for each node in the given |layer|.

        If a |gateLayer| is provided, it will be used to gate the connections:
            If the layers are the same length, the connections will be created
                with one-to-one gating.
            Else, if the gate layer contains one neuron, that neuron will be
                used to gate every self recurrent connection.
            Else, an error will be raised.

        The created connections are returned.
        """
        if gateLayer is None:
            connections =  [connectNeurons(neuron, neuron) for neuron in layer]
        elif len(layer) == len(gateLayer):
            connections =  [connectNeurons(neuron, neuron, gate)
                    for neuron,gate in zip(layer, gateLayer)]
        elif len(gateLayer) == 1:
            connections =  [connectNeurons(neuron, neuron, gateLayer[0]) for neuron in layer]
        else:
            raise ValueError("Cannot gate self recurrent layer with gate \
                layer of differing length!")
        self.connections = self.connections.union(connections)
        self.recurrentConnections = self.recurrentConnections.union(connections)
        return connections