jueves, 22 de noviembre de 2012

[Reporte] 3. Perceptrón que reconoce un "and"

threshold = 0.3     # umbral default: 0.5
learning_rate = 0.5 # taza de aprendizaje default: 0.1
weights = [0, 0, 0] # Pesos iniciales default: 0,0,0
training_set = [((1, 0, 0), 0), ((1, 0, 1), 0), ((1, 1, 0), 0), ((1, 1, 1), 1)]
 
def sum_function(values):
    return sum(value * weight for value, weight in zip(values, weights))
 
while True:
    print '-' * 60
    error_count = 0
    for input_vector, desired_output in training_set:
        print weights
        result = 1 if sum_function(input_vector) > threshold else 0
        error = desired_output - result
        if error != 0:
            error_count += 1
            for index, value in enumerate(input_vector):
                weights[index] += learning_rate * error * value
    if error_count == 0:
        break
Output for threshold = 0.3, learning_rate = 0.5
------------------------------------------------------------
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
------------------------------------------------------------
[0.5, 0.5, 0.5]
[0.0, 0.5, 0.5]
[-0.5, 0.5, 0.0]
[-0.5, 0.5, 0.0]
------------------------------------------------------------
[0.0, 1.0, 0.5]
[0.0, 1.0, 0.5]
[-0.5, 1.0, 0.0]
[-1.0, 0.5, 0.0]
------------------------------------------------------------
[-0.5, 1.0, 0.5]
[-0.5, 1.0, 0.5]
[-0.5, 1.0, 0.5]
[-1.0, 0.5, 0.5]
------------------------------------------------------------
[-0.5, 1.0, 1.0]
[-0.5, 1.0, 1.0]
[-1.0, 1.0, 0.5]
[-1.0, 1.0, 0.5]
------------------------------------------------------------
[-1.0, 1.0, 0.5]
[-1.0, 1.0, 0.5]
[-1.0, 1.0, 0.5]
[-1.0, 1.0, 0.5]
Output for threshold = 0.7, learning_rate = 0.4, weights = [0.6, 0.6, 0.6]
------------------------------------------------------------
[0.59999999999999998, 0.59999999999999998, 0.59999999999999998]
[1.0, 0.59999999999999998, 0.59999999999999998]
[1.0, 0.59999999999999998, 0.59999999999999998]
[1.0, 0.59999999999999998, 0.59999999999999998]
------------------------------------------------------------
[0.59999999999999998, 0.19999999999999996, 0.19999999999999996]
[1.0, 0.19999999999999996, 0.19999999999999996]
[1.0, 0.19999999999999996, 0.19999999999999996]
[1.0, 0.19999999999999996, 0.19999999999999996]
------------------------------------------------------------
[0.59999999999999998, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]
------------------------------------------------------------
[1.0, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]
[1.0, -0.20000000000000007, -0.20000000000000007]

No hay comentarios:

Publicar un comentario

Les agradeceremos sus comentarios, sera de buena ayuda para nuestro trabajo.