# Neural Networks

Nov 27th, 2021
965
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. import sys
2. import math
3.
4. dot_product = lambda X, Y: sum(map(lambda x, y: x * y, X, Y))
5.
6. def sigma(t):
7.     return 1 / (1 + math.exp(-t))
8.
9. def sigma_prime(t):
10.     sigma_value = sigma(t)
11.     return sigma_value * (1 - sigma_value)
12.
13. initial_weight = 0
14.
16.     training_file = open(training_file_name, 'r')
17.     attributes = []
18.     examples = []
19.     correct_values = []
21.     for i in range(len(raw_data)):
22.         line_split = raw_data[i].split('\t')
23.         if i == 0:
24.             for datum in line_split:
25.                 attributes.append(datum)
26.         else:
27.             examples.append([int(line_split[x]) for x in range(len(line_split) - 1)])
28.             correct_values.append(int(line_split[len(line_split) - 1]))
29.     return (attributes, examples, correct_values)
30.
31. def calculate_weights(weights, examples, correct_values, iteration, learning_rate):
32.     global dot_product
33.
34.     example = examples[iteration]
35.     correct_value = correct_values[iteration]
36.     predicted_value = sigma(dot_product(weights, example))
37.     delta = correct_value - predicted_value
38.     predicted_value_slope = sigma_prime(dot_product(weights, example))
39.
40.     correction = [x * learning_rate * delta * predicted_value_slope for x in example]
41.     return [sum(w) for w in zip(weights, correction)]
42.
43. def print_iteration(attributes, weights, examples, iteration):
44.     example = examples[iteration]
45.     predicted_value = sigma(dot_product(weights, example))
46.
47.     print('After iteration ' + str(iteration + 1) + ': ', end='')
48.     for i in range(len(weights)):
49.         print('w(' + attributes[i] + ') = %.4f, ' % weights[i], end='')
50.     print('output = %.4f' % predicted_value)
51.
52. if len(sys.argv) != 5:
53.     print('The program must be run with the training file, the test file, the learning rate, and the number of iterations as input.')
54. else:
55.     training_file = sys.argv[1]
56.     test_file = sys.argv[2]
57.     learning_rate = float(sys.argv[3])
58.     iterations = int(sys.argv[4])
59.
60.     attributes, examples, correct_values = read_training_file(training_file)
61.     weights = [initial_weight] * len(examples[0])
62.
63.     for iteration in range(iterations):
64.         weights = calculate_weights(weights, examples, correct_values, iteration, learning_rate)
65.         print_iteration(attributes, weights, examples, iteration)
RAW Paste Data