예제 #1
0
class KohonenLayer(Layer):
    
    def __init__(self, nodes, bias, neighbourhoodType, rows, columns, conscience):
        Layer.__init__(self, nodes, bias)
        self.neighbourhoodType = neighbourhoodType
        self.rows = rows
        self.columns = columns
        self.conscience = conscience
        self.neighbourhoodFunction = NeighbourhoodFucntion()
        self.winFrequencies = {}
        self.nodesCount = len(self.nodes)
        for node in self.nodes :
            self.winFrequencies[node] = 1.0 / self.nodesCount
        self.stepsCount = 0
        
    def __str__(self):
        return Layer.__str__(self) + '- dimension - ' + str(self.rows) + ' x ' + str(self.columns)
    
    def learn(self, coefficient, conscienceCoefficient, neighbourhoodWidth):
        maximum = None
        for node in self.nodes:
            current_value = node.get_value() + conscienceCoefficient * (1.0 - float(self.nodesCount) * self.winFrequencies[node])
            if (maximum is None) or (current_value > maximum[0]):
                maximum = (current_value, node)

        if self.conscience:
            for node in self.nodes :
                self.winFrequencies[node] = self.winFrequencies[node] * self.stepsCount + 1 if node == maximum[1] else self.winFrequencies[node] * self.stepsCount
                self.winFrequencies[node] = self.winFrequencies[node] / (self.stepsCount + 1)
            self.stepsCount = self.stepsCount + 1
                    
        if self.neighbourhoodType:
            winnerIndex = self.nodes.index(maximum[1])
            for nodeIndex in range(len(self.nodes)) :
                if winnerIndex != nodeIndex :
                    self.nodes[nodeIndex].learn(coefficient * self.neighbourhoodFunction.calculate(self.distance(nodeIndex, winnerIndex), neighbourhoodWidth))
        maximum[1].learn(coefficient)
        self.winner = maximum[1]
        
    def distance(self, nodeIndex, winnerIndex):
        return abs(self.row(nodeIndex) - self.row(winnerIndex)) + abs(self.column(nodeIndex) - self.column(winnerIndex))
        
    def row(self, index):
        return index / self.rows
    
    def column(self, index):
        return index % self.rows
    
    def propagate(self, debug=False):
        maximum = None
        for node in self.nodes:
            if maximum is None or maximum.get_value() < node.get_value():
                maximum = node
        
        maximum.propagate(debug)
        self.bias.propagate()
예제 #2
0
 def __init__(self, nodes, bias, neighbourhoodType, rows, columns, conscience):
     Layer.__init__(self, nodes, bias)
     self.neighbourhoodType = neighbourhoodType
     self.rows = rows
     self.columns = columns
     self.conscience = conscience
     self.neighbourhoodFunction = NeighbourhoodFucntion()
     self.winFrequencies = {}
     self.nodesCount = len(self.nodes)
     for node in self.nodes :
         self.winFrequencies[node] = 1.0 / self.nodesCount
     self.stepsCount = 0