Ejemplo n.º 1
0
    def __init__(self, dim, nNeurons, name=None, outputFullMap=False):
        if outputFullMap:
            outdim = nNeurons**2
        else:
            outdim = 2
        Module.__init__(self, dim, outdim, name)

        # switch modes
        self.outputFullMap = outputFullMap

        # create neurons
        self.neurons = random.random((nNeurons, nNeurons, dim))
        self.difference = zeros(self.neurons.shape)
        self.winner = zeros(2)
        self.nInput = dim
        self.nNeurons = nNeurons
        self.neighbours = nNeurons
        self.learningrate = 0.01
        self.neighbourdecay = 0.9999

        # distance matrix
        distx, disty = mgrid[0:self.nNeurons, 0:self.nNeurons]
        self.distmatrix = zeros((self.nNeurons, self.nNeurons, 2))
        self.distmatrix[:, :, 0] = distx
        self.distmatrix[:, :, 1] = disty
Ejemplo n.º 2
0
    def __init__(self, dim, nNeurons, name=None, outputFullMap=False):
        if outputFullMap:
            outdim = nNeurons ** 2
        else:
            outdim = 2
        Module.__init__(self, dim, outdim, name)

        # switch modes
        self.outputFullMap = outputFullMap

        # create neurons
        self.neurons = random.random((nNeurons, nNeurons, dim))
        self.difference = zeros(self.neurons.shape)
        self.winner = zeros(2)
        self.nInput = dim
        self.nNeurons = nNeurons
        self.neighbours = nNeurons
        self.learningrate = 0.01
        self.neighbourdecay = 0.9999

        # distance matrix
        distx, disty = mgrid[0:self.nNeurons, 0:self.nNeurons]
        self.distmatrix = zeros((self.nNeurons, self.nNeurons, 2))
        self.distmatrix[:, :, 0] = distx
        self.distmatrix[:, :, 1] = disty
Ejemplo n.º 3
0
 def __init__(self, dim, peepholes = False, name = None):
     """ 
     :arg dim: number of cells
     :key peepholes: enable peephole connections (from state to gates)? """
     self.setArgs(dim = dim, peepholes = peepholes)
     
     # Internal buffers, created dynamically:
     self.bufferlist = [
         ('ingate', dim),
         ('outgate', dim),
         ('forgetgate', dim),
         ('ingatex', dim),
         ('outgatex', dim),
         ('forgetgatex', dim),
         ('state', dim),
         ('ingateError', dim),
         ('outgateError', dim),
         ('forgetgateError', dim),
         ('stateError', dim),
     ]
     
     Module.__init__(self, 4*dim, dim, name)
     if self.peepholes:
         ParameterContainer.__init__(self, dim*3)
         self._setParameters(self.params)
         self._setDerivatives(self.derivs)
    def __init__(self, indim, outdim, hiddim=6):
        Module.__init__(self, indim, outdim)

        self._network = Network()
        self._in_layer = LinearLayer(indim + outdim)
        self._hid_layer = LSTMLayer(hiddim)
        self._out_layer = LinearLayer(outdim)
        self._bias = BiasUnit()

        self._network.addInputModule(self._in_layer)
        self._network.addModule(self._hid_layer)
        self._network.addModule(self._bias)
        self._network.addOutputModule(self._out_layer)


        self._hid_to_out_connection = FullConnection(self._hid_layer , self._out_layer)
        self._in_to_hid_connection = FullConnection(self._in_layer  , self._hid_layer)
        self._network.addConnection(self._hid_to_out_connection)
        self._network.addConnection(self._in_to_hid_connection)
        self._network.addConnection(FullConnection(self._bias, self._hid_layer))

        self._network.sortModules()

        self.time = self._network.time
        self.backprojectionFactor = 0.01
Ejemplo n.º 5
0
    def __init__(self, dim, peepholes=False, name=None):
        """ 
        :arg dim: number of cells
        :key peepholes: enable peephole connections (from state to gates)? """
        self.setArgs(dim=dim, peepholes=peepholes)

        # Internal buffers, created dynamically:
        self.bufferlist = [
            ('ingate', dim),
            ('outgate', dim),
            ('forgetgate', dim),
            ('ingatex', dim),
            ('outgatex', dim),
            ('forgetgatex', dim),
            ('state', dim),
            ('ingateError', dim),
            ('outgateError', dim),
            ('forgetgateError', dim),
            ('stateError', dim),
        ]

        Module.__init__(self, 4 * dim, dim, name)
        if self.peepholes:
            ParameterContainer.__init__(self, dim * 3)
            self._setParameters(self.params)
            self._setDerivatives(self.derivs)
Ejemplo n.º 6
0
    def __init__(self, indim, outdim, hiddim=6):
        Module.__init__(self, indim, outdim)

        self._network = Network()
        self._in_layer = LinearLayer(indim + outdim)
        self._hid_layer = LSTMLayer(hiddim)
        self._out_layer = LinearLayer(outdim)
        self._bias = BiasUnit()

        self._network.addInputModule(self._in_layer)
        self._network.addModule(self._hid_layer)
        self._network.addModule(self._bias)
        self._network.addOutputModule(self._out_layer)

        self._hid_to_out_connection = FullConnection(self._hid_layer,
                                                     self._out_layer)
        self._in_to_hid_connection = FullConnection(self._in_layer,
                                                    self._hid_layer)
        self._network.addConnection(self._hid_to_out_connection)
        self._network.addConnection(self._in_to_hid_connection)
        self._network.addConnection(FullConnection(self._bias,
                                                   self._hid_layer))

        self._network.sortModules()

        self.offset = self._network.offset
        self.backprojectionFactor = 0.01
Ejemplo n.º 7
0
    def __init__(self, dim, oscParams=None, freqDist=None, name=None):
        """Create a layer with dim number of units."""
        Module.__init__(self, dim*2, dim, name=name)

        if oscParams is None:
            oscParams = { 'a': 0, 'b1': -1, 'b2': -1, 'd1': 0, 'd2': 0, 'e': 1 }

        if freqDist is None:
            freqDist = {
                'fspac': 'log',
                'min': .5,
                'max': 8
            }
        freqDist['min_r'] = freqDist['min'] * TWO_PI
        freqDist['max_r'] = freqDist['max'] * TWO_PI

        self.conns = []

        self.setArgs(dim=dim, oscParams=oscParams, freqDist=freqDist)

        self.setFreqs(freqDist)

        self.z0 = np.zeros(dim, dtype=np.complex64)
        self._ranomiseOscs()
        self.kSteps = np.zeros((4, dim), dtype=np.complex64)
        self.t = np.float32(0.)
Ejemplo n.º 8
0
    def __init__(self, numRows, numColumns, name=None):
        """ initialize with the number of rows and columns. the table
            values are all set to zero.
        """
        Module.__init__(self, 2, 1, name)
        ParameterContainer.__init__(self, numRows*numColumns)

        self.numRows = numRows
        self.numColumns = numColumns
Ejemplo n.º 9
0
    def __init__(self, numRows, numColumns, name=None):
        """ initialize with the number of rows and columns. the table
            values are all set to zero.
        """
        Module.__init__(self, 2, 1, name)
        ParameterContainer.__init__(self, numRows*numColumns)

        self.numRows = numRows
        self.numColumns = numColumns
Ejemplo n.º 10
0
 def __init__(self, numActions, random_state, name=None):
     Module.__init__(self, 1, 1, name)
     self.n_actions = numActions
     self.numColumns = numActions
     self.random_state = random_state
     if isinstance(self, Module) or isinstance(self, Connection):
         self.hasDerivatives = True
     if self.hasDerivatives:
         self._derivs = None
     self.randomize()
     self._params = None
Ejemplo n.º 11
0
    def __init__(self, actionnum, T, theta, **args):
        self.feadim = len(theta)
        Module.__init__(self, self.feadim * actionnum, 1, **args)
        ParameterContainer.__init__(self, self.feadim)
        self.T = T
        self.g = None
        self.bf = None

        # feadimx1 vector.
        self.theta = theta
        self.actionnum = actionnum

        self.cachedActionProb = None
Ejemplo n.º 12
0
    def __init__ (self, indim = 27, outdim = 6, seed = None, channels_setup = None, steps = None, types_subset = None):
        self._seed = seed or []
        self._steps = steps or 10
        self._channels_setup = channels_setup

        self._types_subset = types_subset or []
        if isinstance(self._seed, str):
            self.parse_seed(self._seed)
        if not self._seed:
            self.generate_seed()

        #print "Init module ", self.get_num_channels()
        Module.__init__(self, indim, outdim, None)
        ParameterContainer.__init__(self, indim)
Ejemplo n.º 13
0
    def __init__(self, policy, name=None):
        self.policy = policy
        self.paramdim = policy.feadim
        self.actionnum = policy.actionnum
        self.statefeadim = self.paramdim
        self.feadesc, outdim = self._transformFeatureDescriptor(
            self.getFeatureDescriptor())

        self.expectedFeaDim = (self.actionnum + 1) * self.paramdim

        Module.__init__(self,
                        indim = self.expectedFeaDim + 1,
                        outdim = outdim,
                        name = name
                        )
Ejemplo n.º 14
0
 def activate(self, state, action):
     """ The super class commonly ignores the state and simply passes the
         action through the module. implement _forwardImplementation()
         in subclasses.
     """
     self.state = state
     return Module.activate(self, action)
Ejemplo n.º 15
0
 def activate(self, state, action):
     """ The super class commonly ignores the state and simply passes the
         action through the module. implement _forwardImplementation()
         in subclasses.
     """
     self.state = state
     return Module.activate(self, action)
Ejemplo n.º 16
0
    def sortModules(self):
        """Prepare the network for activation by sorting the internal
        datastructure.

        Needs to be called before activation."""
        if self.sorted:
            return
        # Sort the modules.
        self._topologicalSort()
        # Sort the connections by name.
        for m in self.modules:
            self.connections[m].sort(key=lambda x: x.name)
        self.motherconnections.sort(key=lambda x: x.name)

        # Create a single array with all parameters.
        tmp = [pc.params for pc in self._containerIterator()]
        total_size = sum(scipy.size(i) for i in tmp)
        ParameterContainer.__init__(self, total_size)
        if total_size > 0:
            self.params[:] = scipy.concatenate(tmp)
            self._setParameters(self.params)

            # Create a single array with all derivatives.
            tmp = [pc.derivs for pc in self._containerIterator()]
            self.resetDerivatives()
            self.derivs[:] = scipy.concatenate(tmp)
            self._setDerivatives(self.derivs)

        # TODO: make this a property; indim and outdim are invalid before
        # .sortModules is called!
        # Determine the input and output dimensions of the network.
        self.indim = sum(m.indim for m in self.inmodules)
        self.outdim = sum(m.outdim for m in self.outmodules)

        self.indim = 0
        for m in self.inmodules:
            self.indim += m.indim
        self.outdim = 0
        for m in self.outmodules:
            self.outdim += m.outdim

        # Initialize the network buffers.
        self.bufferlist = []
        Module.__init__(self, self.indim, self.outdim, name=self.name)
        self.sorted = True
Ejemplo n.º 17
0
def adaptAgentObject(agent_object):
    """Return an object that adapts a pybrain agent to the rlglue interface.
    """
    # This is pretty hacky: We first take a bogus agent with a bogus module
    # for our function adaptAgent to work, then substitue the bogus agent with
    # our actual agent.
    agent = adaptAgent(LearningAgent)(Module(1, 1))
    agent.agent = agent_object
    return agent
Ejemplo n.º 18
0
    def __init__(self, dim, dimensions=1, peepholes=False, name=None):
        self.setArgs(dim=dim, peepholes=peepholes, dimensions=dimensions)

        # Internal buffers:
        self.bufferlist = [
            ('ingate', dim),
            ('outgate', dim),
            ('forgetgate', dim * dimensions),
            ('ingatex', dim),
            ('outgatex', dim),
            ('forgetgatex', dim * dimensions),
            ('state', dim),
            ('ingateError', dim),
            ('outgateError', dim),
            ('forgetgateError', dim * dimensions),
            ('stateError', dim),
        ]

        Module.__init__(self, (3 + 2 * dimensions) * dim, dim * 2, name)

        if self.peepholes:
            ParameterContainer.__init__(self, dim * (2 + dimensions))
            self._setParameters(self.params)
            self._setDerivatives(self.derivs)
Ejemplo n.º 19
0
 def __init__(self, dim, dimensions=1, peepholes=False, name=None):
     self.setArgs(dim=dim, peepholes=peepholes, dimensions=dimensions)
     
     # Internal buffers:
     self.bufferlist = [
         ('ingate', dim),
         ('outgate', dim),
         ('forgetgate', dim * dimensions),
         ('ingatex', dim),
         ('outgatex', dim),
         ('forgetgatex', dim * dimensions),
         ('state', dim),
         ('ingateError', dim),
         ('outgateError', dim),
         ('forgetgateError', dim * dimensions),
         ('stateError', dim),
     ]
     
     Module.__init__(self, (3 + 2 * dimensions) * dim, dim * 2, name)
     
     if self.peepholes:
         ParameterContainer.__init__(self, dim * (2 + dimensions))
         self._setParameters(self.params)
         self._setDerivatives(self.derivs)        
Ejemplo n.º 20
0
    def __init__(self, outdim, hiddim=15):
        """ Create an EvolinoNetwork with for sequences of dimension outdim and
        hiddim dimension of the RNN Layer."""
        indim = 0
        Module.__init__(self, indim, outdim)

        self._network = RecurrentNetwork()
        self._in_layer = LinearLayer(indim + outdim)
        self._hid_layer = LSTMLayer(hiddim)
        self._out_layer = LinearLayer(outdim)
        self._bias = BiasUnit()

        self._network.addInputModule(self._in_layer)
        self._network.addModule(self._hid_layer)
        self._network.addModule(self._bias)
        self._network.addOutputModule(self._out_layer)

        self._in_to_hid_connection = FullConnection(self._in_layer,
                                                    self._hid_layer)
        self._bias_to_hid_connection = FullConnection(self._bias,
                                                      self._hid_layer)
        self._hid_to_out_connection = FullConnection(self._hid_layer,
                                                     self._out_layer)
        self._network.addConnection(self._in_to_hid_connection)
        self._network.addConnection(self._bias_to_hid_connection)
        self._network.addConnection(self._hid_to_out_connection)

        self._recurrent_connection = FullConnection(self._hid_layer,
                                                    self._hid_layer)
        self._network.addRecurrentConnection(self._recurrent_connection)

        self._network.sortModules()
        self._network.reset()

        self.offset = self._network.offset
        self.backprojectionFactor = 0.01
Ejemplo n.º 21
0
    def __init__(self, outdim, hiddim=15):
        """ Create an EvolinoNetwork with for sequences of dimension outdim and
        hiddim dimension of the RNN Layer."""
        indim = 0
        Module.__init__(self, indim, outdim)

        self._network = RecurrentNetwork()
        self._in_layer = LinearLayer(indim + outdim)
        self._hid_layer = LSTMLayer(hiddim)
        self._out_layer = LinearLayer(outdim)
        self._bias = BiasUnit()

        self._network.addInputModule(self._in_layer)
        self._network.addModule(self._hid_layer)
        self._network.addModule(self._bias)
        self._network.addOutputModule(self._out_layer)

        self._in_to_hid_connection = FullConnection(self._in_layer,
                                                    self._hid_layer)
        self._bias_to_hid_connection = FullConnection(self._bias,
                                                      self._hid_layer)
        self._hid_to_out_connection = FullConnection(self._hid_layer,
                                                     self._out_layer)
        self._network.addConnection(self._in_to_hid_connection)
        self._network.addConnection(self._bias_to_hid_connection)
        self._network.addConnection(self._hid_to_out_connection)

        self._recurrent_connection = FullConnection(self._hid_layer,
                                                    self._hid_layer)
        self._network.addRecurrentConnection(self._recurrent_connection)

        self._network.sortModules()
        self._network.reset()

        self.offset = self._network.offset
        self.backprojectionFactor = 0.01
Ejemplo n.º 22
0
 def __init__(self, numStates, numActions, name=None):
     Module.__init__(self, numActions, 1, name)
     self.numRows = numStates
     self.numColumns = numActions
     self.actionvalues = {}
Ejemplo n.º 23
0
 def __init__(self, dim, name=None):
     Module.__init__(self, dim, dim * 2, name)
     self.setArgs(dim=dim, name=self.name)
Ejemplo n.º 24
0
 def __init__(self, name=None):
     Module.__init__(self, 0, 1, name = name)
Ejemplo n.º 25
0
 def __init__(self, *args, **kwargs):
     super(GLFWSBoltzmanPolicy, self).__init__(*args, **kwargs)
     Module.__init__(self, self.feadim * (self.actionnum + 1), 1, *args,
                     **kwargs)
Ejemplo n.º 26
0
 def __init__(self, dim, name=None):
     Module.__init__(self, dim, dim * 2, name)
     self.setArgs(dim=dim, name=self.name)
Ejemplo n.º 27
0
 def __init__(self, dim, name=None):
     Module.__init__(self, dim, dim * 2, name)
Ejemplo n.º 28
0
    def __init__(self,
                 timedim,
                 shape,
                 hiddendim,
                 outsize,
                 blockshape=None,
                 name=None):
        """Initialize an MdrnnLayer.

        The dimensionality of the sequence - for example 2 for a
        picture or 3 for a video - is given by `timedim`, while the sidelengths
        along each dimension are given by the tuple `shape`.

        The layer will have `hiddendim` hidden units per swiping direction. The
        number of swiping directions is given by 2**timedim, which corresponds
        to one swipe from each corner to its opposing corner and back.

        To indicate how many outputs per timesteps are used, you have to specify
        `outsize`.

        In order to treat blocks of the input and not single voxels, you can
        also specify `blockshape`. For example the layer will then feed (2, 2)
        chunks into the network at each timestep which correspond to the (2, 2)
        rectangles that the input can be split into.
        """
        self.timedim = timedim
        self.shape = shape
        blockshape = tuple([1] * timedim) if blockshape is None else blockshape
        self.blockshape = shape
        self.hiddendim = hiddendim
        self.outsize = outsize
        self.indim = reduce(operator.mul, shape, 1)
        self.blocksize = reduce(operator.mul, blockshape, 1)
        self.sequenceLength = self.indim / self.blocksize
        self.outdim = self.sequenceLength * self.outsize

        self.bufferlist = [('cellStates', self.sequenceLength * self.hiddendim)
                           ]

        Module.__init__(self, self.indim, self.outdim, name=name)

        # Amount of parameters that are required for the input to the hidden
        self.num_in_params = self.blocksize * self.hiddendim * (3 +
                                                                self.timedim)

        # Amount of parameters that are needed for the recurrent connections.
        # There is one of the parameter for every time dimension.
        self.num_rec_params = outsize * hiddendim * (3 + self.timedim)

        # Amount of parameters that are needed for the output.
        self.num_out_params = outsize * hiddendim

        # Amount of parameters that are needed from the bias to the hidden and
        # the output
        self.num_bias_params = (3 +
                                self.timedim) * self.hiddendim + self.outsize

        # Total list of parameters.
        self.num_params = sum(
            (self.num_in_params, self.timedim * self.num_rec_params,
             self.num_out_params, self.num_bias_params))

        ParameterContainer.__init__(self, self.num_params)

        # Some layers for internal use.
        self.hiddenlayer = MDLSTMLayer(self.hiddendim, self.timedim)

        # Every point in the sequence has timedim predecessors.
        self.predlayers = [LinearLayer(self.outsize) for _ in range(timedim)]

        # We need a single layer to hold the input. We will swipe a connection
        # over the corrects part of it, in order to feed the correct input in.
        self.inlayer = LinearLayer(self.indim)
        # Make some layers the same to save memory.
        self.inlayer.inputbuffer = self.inlayer.outputbuffer = self.inputbuffer

        # In order to allocate not too much memory, we just set the size of the
        # layer to 1 and correct it afterwards.
        self.outlayer = LinearLayer(self.outdim)
        self.outlayer.inputbuffer = self.outlayer.outputbuffer = self.outputbuffer

        self.bias = BiasUnit()
Ejemplo n.º 29
0
 def reset(self):
     """Reset all component modules and the network."""
     Module.reset(self)
     for m in self.modules:
         m.reset()
Ejemplo n.º 30
0
Archivo: gate.py Proyecto: HKou/pybrain
 def __init__(self, dim, name=None):
     Module.__init__(self, dim, dim * 2, name)
Ejemplo n.º 31
0
 def __init__(self, dim, name=None):
     """Create a layer with dim number of units."""
     Module.__init__(self, dim, dim, name=name)
     self.setArgs(dim=dim)
Ejemplo n.º 32
0
 def __init__(self, dim, name=None):
     """Create a layer with dim number of units."""
     Module.__init__(self, dim, dim, name=name)
     self.setArgs(dim=dim)
Ejemplo n.º 33
0
    def __init__(self, timedim, shape,
                 hiddendim, outsize, blockshape=None, name=None):
        """Initialize an MdrnnLayer.

        The dimensionality of the sequence - for example 2 for a
        picture or 3 for a video - is given by `timedim`, while the sidelengths
        along each dimension are given by the tuple `shape`.

        The layer will have `hiddendim` hidden units per swiping direction. The
        number of swiping directions is given by 2**timedim, which corresponds
        to one swipe from each corner to its opposing corner and back.

        To indicate how many outputs per timesteps are used, you have to specify
        `outsize`.

        In order to treat blocks of the input and not single voxels, you can
        also specify `blockshape`. For example the layer will then feed (2, 2)
        chunks into the network at each timestep which correspond to the (2, 2)
        rectangles that the input can be split into.
        """
        self.timedim = timedim
        self.shape = shape
        blockshape = tuple([1] * timedim) if blockshape is None else blockshape
        self.blockshape = shape
        self.hiddendim = hiddendim
        self.outsize = outsize
        self.indim = reduce(operator.mul, shape, 1)
        self.blocksize = reduce(operator.mul, blockshape, 1)
        self.sequenceLength = self.indim / self.blocksize
        self.outdim = self.sequenceLength * self.outsize

        self.bufferlist = [('cellStates', self.sequenceLength * self.hiddendim)]

        Module.__init__(self, self.indim, self.outdim, name=name)

        # Amount of parameters that are required for the input to the hidden
        self.num_in_params = self.blocksize * self.hiddendim * (3 + self.timedim)

        # Amount of parameters that are needed for the recurrent connections.
        # There is one of the parameter for every time dimension.
        self.num_rec_params = outsize * hiddendim * (3 + self.timedim)

        # Amount of parameters that are needed for the output.
        self.num_out_params = outsize * hiddendim

        # Amount of parameters that are needed from the bias to the hidden and
        # the output
        self.num_bias_params = (3 + self.timedim) * self.hiddendim + self.outsize

        # Total list of parameters.
        self.num_params = sum((self.num_in_params,
                               self.timedim * self.num_rec_params,
                               self.num_out_params,
                               self.num_bias_params))

        ParameterContainer.__init__(self, self.num_params)

        # Some layers for internal use.
        self.hiddenlayer = MDLSTMLayer(self.hiddendim, self.timedim)

        # Every point in the sequence has timedim predecessors.
        self.predlayers = [LinearLayer(self.outsize) for _ in range(timedim)]

        # We need a single layer to hold the input. We will swipe a connection
        # over the corrects part of it, in order to feed the correct input in.
        self.inlayer = LinearLayer(self.indim)
        # Make some layers the same to save memory.
        self.inlayer.inputbuffer = self.inlayer.outputbuffer = self.inputbuffer

        # In order to allocate not too much memory, we just set the size of the
        # layer to 1 and correct it afterwards.
        self.outlayer = LinearLayer(self.outdim)
        self.outlayer.inputbuffer = self.outlayer.outputbuffer = self.outputbuffer

        self.bias = BiasUnit()
Ejemplo n.º 34
0
 def __init__(self, name=None):
     Module.__init__(self, 0, 1, name = name)