Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy
- MAGIC_CONSTANT = 7
- INPUT_DATA = [
- (numpy.matrix([[-1], [ 1], [ 1], [ 1], [ 1], [-1], [ 1], [ 1], [ 1], [ 1], [-1], [ 1], [ 1], [ 1], [ 1], [-1], [ 1], [ 1], [ 1], [ 1], [-1], [-1], [-1], [-1], [-1]]), numpy.matrix([[ 1.],[-1.], [-1.], [-1.]])),#l
- (numpy.matrix([[-1], [-1], [-1], [-1], [-1], [-1], [ 1], [-1], [ 1], [-1], [-1], [ 1], [-1], [ 1], [-1], [-1], [ 1], [-1], [ 1], [-1], [-1], [ 1], [-1], [ 1], [-1]]), numpy.matrix([[-1.], [ 1.], [-1.], [-1.]])),#m
- (numpy.matrix([[-1], [-1], [-1], [-1], [-1], [-1], [ 1], [ 1], [ 1], [ 1], [-1], [-1], [-1], [-1], [ 1], [-1], [ 1], [ 1], [ 1], [ 1], [-1], [-1], [-1], [-1], [-1]]), numpy.matrix([[-1.], [-1.], [ 1.], [-1.]])),#e
- (numpy.matrix([[-1], [ 1], [ 1], [ 1], [-1], [ 1], [-1], [ 1], [-1], [ 1], [ 1], [ 1], [-1], [ 1], [ 1], [ 1], [-1], [ 1], [-1], [ 1], [-1], [ 1], [ 1], [ 1], [-1]]), numpy.matrix([[-1.], [-1.], [-1.], [ 1.]]))]#x
- def add_one_to_input():
- def decorator(func):
- def inner(*args, **kwargs):
- if 'examples' in kwargs:
- kwargs['examples'] = [(numpy.insert(input, 0, 1, axis=0), output) for
- input, output in kwargs['examples']]
- return func(*args, **kwargs)
- return inner
- return decorator
- def is_correct_weight(input_matrix, output_matrix, weights):
- result = (weights.dot(input_matrix) > 0) * 2 - 1
- return numpy.array_equal(result, output_matrix)
- @add_one_to_input()
- def hebb(examples):
- input, output = examples[0]
- input_matrix = numpy.concatenate([input for input, _ in examples], axis=1)
- output_matrix = numpy.concatenate([output for _, output in examples], axis=1)
- weights = numpy.matrix(numpy.zeros(shape=(output.shape[0], input.shape[0])))
- id = 0
- for _ in range(len(examples)**2 + MAGIC_CONSTANT):
- for input, output in examples:
- id+=1
- if is_correct_weight(input_matrix, output_matrix, weights):
- return weights
- weights += output * input.T
- print("Веса после {0}-й итерации".format(str(id)))
- correct_print_weights(weights)
- def correct_print_weights(weigts):
- for i, row in enumerate(weigts):
- print("Значение для выхода {}".format(str(i+1)))
- print(' '.join([str(e) for e in row.tolist()]))
- w = hebb(examples=INPUT_DATA)
- print("Ответ")
- correct_print_weights(w)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement