Example #1
0
def trainNeuron(df, maxError = 0.4, maxIter = 1000):
    neuron = Perceptron()
    neuron.af = af.linear
    acc = 0
    iter = 0
    range = 5
    while (1 - acc > maxError) and (iter < maxIter):
        # Generate random weights and biases:
        neuron.weights = (rd.uniform(-range, range), rd.uniform(-range,range))
        neuron.bias = rd.uniform(-range, range)

        # Calculate accuracy
        acc = calculateAccuracy(df, neuron)
        iter += 1

    return {'weights': tuple(neuron.weights), 'bias': neuron.bias, 'acc': acc, 'iter': iter}
Example #2
0
def trainPerceptron(df, minLearningRate=0.005, maxIter=100):
    neuron = Perceptron()
    acc = 0
    iter = 0
    learningRate = 1
    lastAccuracies = list()
    for i in range(10):
        lastAccuracies.append(0)
        lastAccuracies.append(1)

    # Track error during training
    errorEvolution = list()

    # Generate random weights and biases:
    radius = 5
    neuron.weights = list()
    for i in range(df.shape[1] - 1):
        neuron.weights.append(rd.uniform(-radius, radius))

        #rd.uniform(-radius, radius), rd.uniform(-radius,radius)]
    neuron.bias = rd.uniform(-radius, radius)

    while (learningRate > minLearningRate) and (iter < maxIter):
        # Loop over dataset once, update weights and return accuracy
        acc = iteratePerceptronTraining(df, neuron)

        # Update learningRate
        del lastAccuracies[0]
        lastAccuracies.append(acc)
        learningRate = max(lastAccuracies) - min(lastAccuracies)

        # Update error
        errorEvolution.append(1 - acc)
        # Update iteration counter
        iter += 1

    return neuron, errorEvolution