コード例 #1
0
    def __init__(self, dim_list, eta = 0.1):
        """
        Constructor for network.
        Params:
        dim_list: a list of the number of dimension for each layer.
        eta: learning rate for each gradient descent step
        """
        depth = len(dim_list)
        self.depth = depth
        self.dim_list = dim_list
        self.eta = eta

        # 1. Initiate each layer: output, partial_output and weight,
        #    although partial_output is useless for the input layer, similarly
        #    weight and bias are useless for the output layer.
        #
        # 2. Partial_weight is an internal variable and will not be stored in
        #    a layer.
        #
        self.layers = [ {'output':Vector.fromIterable(0 for i in xrange(dim_list[l])),
            'partial_output':Vector.fromIterable(0 for i in xrange(dim_list[l])),
            'weight':Matrix.fromRandom(dim_list[l + 1], dim_list[l]),
            'bias':Vector.fromRandom(dim_list[l + 1])}
            for l in xrange(depth - 1) ]
        
        # output layer
        self.layers.append({'output':Vector.fromList([0] * dim_list[depth - 1]),
            'partial_output':Vector.fromList([0] * dim_list[depth - 1]),
            'weight': None, 'bias': None})
コード例 #2
0
def main(mnist_path):
    puttime('start loading')

    network = feedforward_network.FeedForwardNetwork(
            dim_list = [2, 2, 2],
            eta = 0.5
            )

    network.layers[0]['weight'] = Matrix(2, 2, [.15, .20, .25, .30])
    network.layers[0]['bias'] = Vector.fromList([.35, .35])
    network.layers[1]['weight'] = Matrix(2, 2, [.40, .45, .50, .55])
    network.layers[1]['bias'] = Vector.fromList([.60, .60])

    x = Vector([.05, .10])
    y = Vector([.01, .99])

    def generator(x, y):
        yield (x, y)

    # start training
    puttime('start training')
    network.train(generator(x, y), puttime, limit = 1)

    # testing
    out = network.inference(x)
    print x, '->', out

    # start training
    puttime('start training')
    network.train(generator(x, y), puttime, limit = 1)

    # testing
    out = network.inference(x)
    print x, '->', out
コード例 #3
0
 def inference(self, vector):
     self._forward(vector)
     output = self.layers[self.depth - 1]['output']
     maxpos, maxval = 0, 0
     for i in xrange(len(output)):
         if maxval < output[i]:
             maxpos, maxval = i, output[i]
     rtn = Vector.fromList([0] * len(output))
     rtn[maxpos] = 1
     return rtn