Exemple #1
0
    def __init__(self, dim, sigma=0.0):
        Explorer.__init__(self, dim, dim)
        self.dim = dim

        # initialize parameters to sigma
        ParameterContainer.__init__(self, dim, stdParams=0)
        self.sigma = [sigma] * dim
Exemple #2
0
 def __init__(self, dim, name=None):
     NeuronLayer.__init__(self, dim, name)
     # initialize sigmas to 0
     ParameterContainer.__init__(self, dim, stdParams=0)
     # if autoalpha is set to True, alpha_sigma = alpha_mu = alpha*sigma^2
     self.autoalpha = False
     self.enabled = True
Exemple #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)
Exemple #4
0
 def __init__(self, inmod, outmod, name=None,
              inSliceFrom=0, inSliceTo=None, outSliceFrom=0, outSliceTo=None):
     if inSliceTo is None:
         inSliceTo = inmod.outdim
     size = inSliceTo - inSliceFrom
     Connection.__init__(self, inmod, outmod, name,
                         inSliceFrom, inSliceTo, outSliceFrom, outSliceTo)
     ParameterContainer.__init__(self, size)
Exemple #5
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
Exemple #6
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
Exemple #7
0
 def __init__(self, inmod, outmod, name=None,
              inSliceFrom=0, inSliceTo=None, outSliceFrom=0, outSliceTo=None):
     if inSliceTo is None:
         inSliceTo = inmod.outdim
     size = inSliceTo - inSliceFrom
     # FIXME: call `super()` with named kwargs so that cooperative inhertiance will work, otherwise...
     # >>> isinstance(LinearConnection, Connection)
     # False
     # >>> isinstance(LinearConnection, ParameterContainer)
     # False
     Connection.__init__(self, inmod, outmod, name,
                         inSliceFrom, inSliceTo, outSliceFrom, outSliceTo)
     ParameterContainer.__init__(self, size)
Exemple #8
0
    def __init__(self, statedim, actiondim, sigma= -2.):
        Explorer.__init__(self, actiondim, actiondim)
        self.statedim = statedim
        self.actiondim = actiondim

        # initialize parameters to sigma
        ParameterContainer.__init__(self, actiondim, stdParams=0)
        self.sigma = [sigma] * actiondim

        # exploration matrix (linear function)
        self.explmatrix = random.normal(0., expln(self.sigma), (statedim, actiondim))

        # store last state
        self.state = None
Exemple #9
0
    def __init__(self, dx, dy, *args, **kwargs):
        Connection.__init__(self, *args, **kwargs)
        self.dx = dx
        self.dy = dy
        ParameterContainer.__init__(self, 4 * self.outdim)

        for i in xrange(0, self.outdim):
            self.params[2 + i * 4] = dx / 6.0
            self.params[3 + i * 4] = dx / 4.0

        self.xx = numpy.repeat([numpy.arange(0, self.dx, 1)], self.dy, axis=0).T
        self.yy = numpy.repeat([numpy.arange(0, self.dy, 1)], self.dx, axis=0)
        assert self.indim == self.dx * self.dy, "Indim (%i) does not equal dx * dy (%i %i)" % (
            self.indim,
            self.dx,
            self.dy,
        )
Exemple #10
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
Exemple #11
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
Exemple #12
0
 def __init__(self, name=None, **args):
     ParameterContainer.__init__(self, **args)
     self.name = name
     # Due to the necessity of regular testing for membership, modules are
     # stored in a set.
     self.modules = set()
     self.modulesSorted = []
     # The connections are stored in a dictionary: the key is the module
     # where the connection leaves from, the value is a list of the
     # corresponding connections.
     self.connections = {}
     self.inmodules = []
     self.outmodules = []
     # Special treatment of weight-shared connections.
     self.motherconnections = []
     # This flag is used to make sure that the modules are reordered when
     # new connections are added.
     self.sorted = False
Exemple #13
0
    def __init__(self, dim, module, name=None, onesigma=True):
        NeuronLayer.__init__(self, dim, name)
        self.exploration = zeros(dim, float)
        self.state = None
        self.onesigma = onesigma

        if self.onesigma:
            # one single parameter: sigma
            ParameterContainer.__init__(self, 1)
        else:
            # sigmas for all parameters in the exploration module
            ParameterContainer.__init__(self, module.paramdim)

        # a module for the exploration
        assert module.outdim == dim, "Passed module does not have right dimension"
        self.module = module
        self.autoalpha = False
        self.enabled = True
Exemple #14
0
    def __init__(self, indim, outdim, peepholes=False, name=None):
        nrNeurons = outdim
        self.peep = peepholes
        # internal buffers:
        self.ingate = zeros((0, nrNeurons))
        self.outgate = zeros((0, nrNeurons))
        self.forgetgate = zeros((0, nrNeurons))
        self.cell = zeros((0, nrNeurons))
        self.ingatex = zeros((0, nrNeurons))
        self.outgatex = zeros((0, nrNeurons))
        self.forgetgatex = zeros((0, nrNeurons))
        self.cellx = zeros((0, nrNeurons))
        self.state = zeros((0, nrNeurons))
        self.ingateError = zeros((0, nrNeurons))
        self.outgateError = zeros((0, nrNeurons))
        self.forgetgateError = zeros((0, nrNeurons))
        self.stateError = zeros((0, nrNeurons))
        self.Sin = zeros((0, indim * nrNeurons))
        self.Sforget = zeros((0, indim * nrNeurons))
        self.Scell = zeros((0, indim * nrNeurons))
        self.SinRec = zeros((0, nrNeurons * nrNeurons))
        self.SforgetRec = zeros((0, nrNeurons * nrNeurons))
        self.ScellRec = zeros((0, nrNeurons * nrNeurons))

        Module.__init__(self, indim, outdim, name)
        if self.peep:
            ParameterContainer.__init__(
                self, nrNeurons * 3 + (4 * indim + nrNeurons) * nrNeurons)
            self.Sin_peep = zeros((0, nrNeurons))
            self.Sforget_peep = zeros((0, nrNeurons))
            self.Scell_peep = zeros((0, nrNeurons))
        else:
            ParameterContainer.__init__(self,
                                        (4 * indim + nrNeurons) * nrNeurons)
        self._setParameters(self.params)
        self._setDerivatives(self.derivs)

        # transfer functions and their derivatives
        self.f = sigmoid
        self.fprime = sigmoidPrime
        self.g = lambda x: 2 * tanh(x)
        self.gprime = lambda x: 2 * tanhPrime(x)
        self.h = self.g
        self.hprime = self.gprime
 def __init__(self, dim, module, name=None, onesigma=True):
     NeuronLayer.__init__(self, dim, name)
     self.exploration = zeros(dim, float)
     self.state = None
     self.onesigma = onesigma
     
     if self.onesigma:
         # one single parameter: sigma
         ParameterContainer.__init__(self, 1)
     else:
         # sigmas for all parameters in the exploration module
         ParameterContainer.__init__(self, module.paramdim)
     
     # a module for the exploration
     assert module.outdim == dim, (
         "Passed module does not have right dimension")
     self.module = module
     self.autoalpha = False
     self.enabled = True
Exemple #16
0
 def __init__(self, indim, outdim, peepholes = False, name = None):
     nrNeurons = outdim
     self.peep = peepholes
     # internal buffers:
     self.ingate = zeros((0,nrNeurons))
     self.outgate = zeros((0,nrNeurons))
     self.forgetgate = zeros((0,nrNeurons))
     self.cell = zeros((0,nrNeurons))
     self.ingatex = zeros((0,nrNeurons))
     self.outgatex = zeros((0,nrNeurons))
     self.forgetgatex = zeros((0,nrNeurons))
     self.cellx = zeros((0,nrNeurons))
     self.state = zeros((0,nrNeurons))
     self.ingateError = zeros((0,nrNeurons))
     self.outgateError = zeros((0,nrNeurons))
     self.forgetgateError = zeros((0,nrNeurons))
     self.stateError = zeros((0,nrNeurons))
     self.Sin = zeros((0,indim*nrNeurons))
     self.Sforget = zeros((0,indim*nrNeurons))
     self.Scell = zeros((0,indim*nrNeurons))
     self.SinRec = zeros((0,nrNeurons*nrNeurons))
     self.SforgetRec = zeros((0,nrNeurons*nrNeurons))
     self.ScellRec = zeros((0,nrNeurons*nrNeurons))
     
     Module.__init__(self, indim, outdim, name)
     if self.peep:
         ParameterContainer.__init__(self, nrNeurons*3 + (4*indim+nrNeurons)*nrNeurons)
         self.Sin_peep = zeros((0,nrNeurons))
         self.Sforget_peep = zeros((0,nrNeurons))
         self.Scell_peep = zeros((0,nrNeurons))
     else:
         ParameterContainer.__init__(self, (4*indim+nrNeurons)*nrNeurons)
     self._setParameters(self.params)
     self._setDerivatives(self.derivs)
         
     # transfer functions and their derivatives
     self.f = sigmoid
     self.fprime = sigmoidPrime
     self.g = lambda x: 2*tanh(x)
     self.gprime = lambda x: 2*tanhPrime(x)
     self.h = self.g
     self.hprime = self.gprime
Exemple #17
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)
Exemple #18
0
 def __init__(self, dim, peepholes = False, name = None):
     self.setArgs(dim = dim, peepholes = peepholes)
     
     # Internal buffers:
     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)
Exemple #19
0
    def __init__(self, dim, peepholes=False, name=None):
        self.setArgs(dim=dim, peepholes=peepholes)

        # Internal buffers:
        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)
Exemple #20
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)
Exemple #21
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()
Exemple #22
0
 def __init__(self, *args, **kwargs):
     Connection.__init__(self, *args, **kwargs)
     ParameterContainer.__init__(self, self.indim * self.outdim)
Exemple #23
0
 def __init__(self, numStates, numActions, name=None):
     Module.__init__(self, 1, 1, name)
     ParameterContainer.__init__(self, numStates * numActions)
     self.numRows = numStates
     self.numColumns = numActions
Exemple #24
0
 def __init__(self, *args, **kwargs):
     IdentityConnection.__init__(self, *args, **kwargs)
     ParameterContainer.__init__(self, self.indim)
Exemple #25
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()
Exemple #26
0
 def __init__(self, *args, **kwargs):
     Connection.__init__(self, *args, **kwargs)
     ParameterContainer.__init__(self, self.indim*self.outdim)
Exemple #27
0
 def __init__(self, numStates, numActions, name=None):
     Module.__init__(self, 1, 1, name)
     ParameterContainer.__init__(self, numStates * numActions)
     self.numRows = numStates
     self.numColumns = numActions
Exemple #28
0
 def __init__(self, nbparams, **args):
     assert nbparams > 0
     ParameterContainer.__init__(self, nbparams, **args)
     self.setArgs(nbparams = self.paramdim)
Exemple #29
0
 def __init__(self, *args, r=0.1, **kwargs):
     Connection.__init__(self, *args, **kwargs)
     ParameterContainer.__init__(self, self.indim * self.outdim)
     self.r = r
     self.dropoutMask = np.random.binomial(1, (1 - r), (self.indim))
Exemple #30
0
 def __init__(self, nbparams, **args):
     assert nbparams > 0
     ParameterContainer.__init__(self, nbparams, **args)
     self.setArgs(nbparams = self.paramdim)
Exemple #31
0
 def __init__(self, inmod, outmod, name=None, 
              inSliceFrom=0, inSliceTo=None, outSliceFrom=0, outSliceTo=None):
     size = inSliceTo - inSliceFrom
     Connection.__init__(self, inmod, outmod, name,
                         inSliceFrom, inSliceTo, outSliceFrom, outSliceTo)
     ParameterContainer.__init__(self, size)