Exemple #1
0
 def _setDerivatives(self, d, owner=None):
     """ put slices of this array back into the modules """
     ParameterContainer._setDerivatives(self, d, owner)
     index = 0
     for x in self._containerIterator():
         x._setDerivatives(self.derivs[index:index + x.paramdim], self)
         index += x.paramdim
 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, 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 #4
0
    def __init__(self, dim, sigma=0.):
        Explorer.__init__(self, dim, dim)
        self.dim = dim

        # initialize parameters to sigma
        ParameterContainer.__init__(self, dim, stdParams=0)
        self.sigma = [sigma] * dim
Exemple #5
0
 def _setParameters(self, p, owner=None):
     ParameterContainer._setParameters(self, p, owner)
     size = self.dim
     self.ingatePeepWeights = self.params[:size]
     self.forgetgatePeepWeights = self.params[size:size *
                                              (1 + self.dimensions)]
     self.outgatePeepWeights = self.params[size * (1 + self.dimensions):]
Exemple #6
0
 def _setInitEvaluable(self, evaluable):
     if evaluable is None:
         # if there is no initial point specified, we start at one that's sampled 
         # normally around the origin.
         if self.numParameters is not None:
             evaluable = randn(self.numParameters)
         else:
             raise ValueError('Could not determine the dimensionality of the evaluator. '+\
                              'Please provide an initial search point.')   
     if isinstance(evaluable, list):
         evaluable = array(evaluable)
     
     # If the evaluable is provided as a list of numbers or as an array,
     # we wrap it into a ParameterContainer.
     if isinstance(evaluable, ndarray):            
         pc = ParameterContainer(len(evaluable))
         pc._setParameters(evaluable)
         self._wasWrapped = True
         evaluable = pc
     self._initEvaluable = evaluable
     if isinstance(self._initEvaluable, ParameterContainer):
         if self.numParameters is None:            
             self.numParameters = len(self._initEvaluable)
         elif self.numParameters is not len(self._initEvaluable):
             raise ValueError("Parameter dimension mismatch: evaluator expects "+str(self.numParameters)\
                              +" but the evaluable has "+str(len(self._initEvaluable))+".")
Exemple #7
0
 def _setDerivatives(self, d, owner=None):
     ParameterContainer._setDerivatives(self, d, owner)
     size = self.dim
     self.ingatePeepDerivs = self.derivs[:size]
     self.forgetgatePeepDerivs = \
         self.derivs[size:size * (1 + self.dimensions)]
     self.outgatePeepDerivs = \
         self.derivs[size * (1 + self.dimensions):]
Exemple #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
 def __init__(self,
              inmod,
              outmod,
              name=None,
              inSliceFrom=0,
              inSliceTo=None,
              outSliceFrom=0,
              outSliceTo=None):
     if inSliceTo is None:
         inSliceTo = outmod.indim
     size = inSliceTo - inSliceFrom
     Connection.__init__(self, inmod, outmod, name, inSliceFrom, inSliceTo,
                         outSliceFrom, outSliceTo)
     ParameterContainer.__init__(self, size)
Exemple #10
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 #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
    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, 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)
 def __init__(self, numStates, numActions, name=None):
     Module.__init__(self, 1, 5, name)
     ParameterContainer.__init__(self, numStates * numActions)
     self.numRows = numStates
     self.numColumns = numActions
Exemple #16
0
 def __init__(self, nbparams, **args):
     assert nbparams > 0
     ParameterContainer.__init__(self, nbparams, **args)
     self.setArgs(nbparams=self.paramdim)
Exemple #17
0
 def mutate(self, *args, **kwargs):
     ParameterContainer.mutate(self, *args, **kwargs)
     self.__stored._params[:] = self._params
Exemple #18
0
 def randomize(self, *args, **kwargs):
     ParameterContainer.randomize(self, *args, **kwargs)
     self.__stored._params[:] = self._params
Exemple #19
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 #20
0
 def __init__(self, *args, **kwargs):
     Connection.__init__(self, *args, **kwargs)
     ParameterContainer.__init__(self, self.indim*self.outdim)
 def _setDerivatives(self, d, owner = None):
     ParameterContainer._setDerivatives(self, d, owner)
     dim = self.outdim
     self.ingatePeepDerivs = self.derivs[:dim]
     self.forgetgatePeepDerivs = self.derivs[dim:dim*2]
     self.outgatePeepDerivs = self.derivs[dim*2:]
 def _setParameters(self, p, owner = None):
     ParameterContainer._setParameters(self, p, owner)
     dim = self.outdim
     self.ingatePeepWeights = self.params[:dim]
     self.forgetgatePeepWeights = self.params[dim:dim*2]
     self.outgatePeepWeights = self.params[dim*2:]
task = BalanceTask()
task.N = 10
# for the simple evolvable class defined below
evoEval = lambda e: e.x

# starting points
# ----------------------
xlist1 = [2.]
xlist2 = [0.2, 10]
xlist100 = list(range(12, 112))

xa1 = array(xlist1)
xa2 = array(xlist2)
xa100 = array(xlist100)

pc1 = ParameterContainer(1)
pc2 = ParameterContainer(2)
pc100 = ParameterContainer(100)
pc1._setParameters(xa1)
pc2._setParameters(xa2)
pc100._setParameters(xa100)

# for the task object, we need a module
nnet = buildNetwork(task.outdim, 2, task.indim)


# a mimimalistic Evolvable subclass that is not (like usual) a ParameterContainer
class SimpleEvo(Evolvable):
    def __init__(self, x):
        self.x = x