def _buildBorderStructure(self, inmesh, hiddenmesh, outmesh): self._buildSwipingStructure(inmesh, hiddenmesh, outmesh) self.addModule(BiasUnit(name = 'bias')) # build the motherconnections for the borders if self.simpleborders: if not 'borderconn' in self.predefined: self.predefined['borderconn'] = MotherConnection(hiddenmesh.componentIndim, name = 'bconn') else: if not 'bordconns' in self.predefined: self.predefined['bordconns'] = {} for dim, maxval in enumerate(self.dims): if dim > 0 and self.symmetricdimensions: self.predefined['bordconns'][dim] = self.predefined['bordconns'][0] elif dim not in self.predefined['bordconns']: self.predefined['bordconns'][dim] = {} tmp = self.predefined['bordconns'][dim].copy() if len(self.dims) == 1 and () not in tmp: tmp[()] = MotherConnection(hiddenmesh.componentIndim, name = 'bconn') for t in iterCombinations(tupleRemoveItem(self.dims, dim)): tc = self._canonicForm(t, dim) if t == tc and t not in tmp: # the connections from the borders are symmetrical, # so we need separate ones only up to the middle tmp[t] = MotherConnection(hiddenmesh.componentIndim, name = 'bconn'+str(dim)+str(t)) if self.extrapolateBorderValues: p = self._extrapolateBorderAt(t, self.predefined['bordconns'][dim]) if p != None: tmp[t].params[:] = p self.predefined['bordconns'][dim] = tmp # link the bordering units to the bias, using the correct connection for dim, maxval in enumerate(self.dims): for unit in self._iterateOverUnits(): if self.simpleborders: bconn = self.predefined['borderconn'] else: tc = self._canonicForm(tupleRemoveItem(unit, dim), dim) bconn = self.predefined['bordconns'][dim][tc] hunits = [] if unit[dim] == 0: for swipe in range(self.swipes): if (swipe/2**dim) % 2 == 0: hunits.append(tuple(list(unit)+[swipe])) if unit[dim] == maxval-1: for swipe in range(self.swipes): if (swipe/2**dim) % 2 == 1: hunits.append(tuple(list(unit)+[swipe])) for hunit in hunits: self.addConnection(SharedFullConnection(bconn, self['bias'], hiddenmesh[hunit]))
def __init__(self, constructor, dimensions, name=None, baserename=False): """:arg constructor: a constructor method that returns a module :arg dimensions: tuple of dimensions. """ self.dims = dimensions if name != None: self.name = name # a dict where the tuple of coordinates is the key self.components = {} for coord in iterCombinations(self.dims): tmp = constructor() self.components[coord] = tmp tmp.name = self.name + str(coord) if baserename and isinstance(tmp, ModuleSlice): tmp.base.name = tmp.name self.componentIndim = tmp.indim self.componentOutdim = tmp.outdim
def _iterateOverUnits(self): """ iterate over the coordinates defines by the ranges of self.dims. """ return iterCombinations(self.dims)
def __iter__(self): for coord in iterCombinations(self.dims): yield self.components[coord]