|
| 1 | +from lnn.perceptron import init_perceptron, print_perceptron, normalize_input_vectors, Perceptron, train_dataset |
| 2 | +from lpdraw import Line, Circle, Display, Clear |
| 3 | +from lpython import i32, f64, Const |
| 4 | +from numpy import empty, int32 |
| 5 | + |
| 6 | + |
| 7 | +def compute_decision_boundary(p: Perceptron, x: f64) -> f64: |
| 8 | + bias: f64 = p.weights[-1] |
| 9 | + slope: f64 = (-p.weights[0] / p.weights[1]) |
| 10 | + intercept: f64 = (-bias / p.weights[1]) |
| 11 | + return slope * x + intercept |
| 12 | + |
| 13 | +def plot_graph(p: Perceptron, input_vectors: list[list[f64]], outputs: list[i32]): |
| 14 | + Width: Const[i32] = 500 # x-axis limits [0, 499] |
| 15 | + Height: Const[i32] = 500 # y-axis limits [0, 499] |
| 16 | + Screen: i32[Height, Width] = empty((Height, Width), dtype=int32) |
| 17 | + Clear(Height, Width, Screen) |
| 18 | + |
| 19 | + x1: f64 = 2.0 |
| 20 | + y1: f64 = compute_decision_boundary(p, x1) |
| 21 | + x2: f64 = -2.0 |
| 22 | + y2: f64 = compute_decision_boundary(p, x2) |
| 23 | + |
| 24 | + # center the graph using the following offset |
| 25 | + scale_offset: f64 = Width / 4 |
| 26 | + shift_offset: f64 = Width / 2 |
| 27 | + x1 *= scale_offset |
| 28 | + y1 *= scale_offset |
| 29 | + x2 *= scale_offset |
| 30 | + y2 *= scale_offset |
| 31 | + |
| 32 | + # print (x1, y1, x2, y2) |
| 33 | + Line(Height, Width, Screen, i32(x1 + shift_offset), i32(y1 + shift_offset), i32(x2 + shift_offset), i32(y2 + shift_offset)) |
| 34 | + |
| 35 | + i: i32 |
| 36 | + point_size: i32 = 5 |
| 37 | + for i in range(len(input_vectors)): |
| 38 | + input_vectors[i][0] *= scale_offset |
| 39 | + input_vectors[i][1] *= scale_offset |
| 40 | + input_vectors[i][0] += shift_offset |
| 41 | + input_vectors[i][1] += shift_offset |
| 42 | + if outputs[i] == 1: |
| 43 | + x: i32 = i32(input_vectors[i][0]) |
| 44 | + y: i32 = i32(input_vectors[i][1]) |
| 45 | + Line(Height, Width, Screen, x - point_size, y, x + point_size, y) |
| 46 | + Line(Height, Width, Screen, x, y - point_size, x, y + point_size) |
| 47 | + else: |
| 48 | + Circle(Height, Width, Screen, i32(input_vectors[i][0]), i32(input_vectors[i][1]), f64(point_size)) |
| 49 | + |
| 50 | + Display(Height, Width, Screen) |
| 51 | + |
| 52 | +def main0(): |
| 53 | + p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0) |
| 54 | + init_perceptron(p, 2, 0.05, 10000, 90.0) |
| 55 | + print_perceptron(p) |
| 56 | + print("=================================") |
| 57 | + |
| 58 | + input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0]] |
| 59 | + outputs: list[i32] = [1, 1, 1, -1] |
| 60 | + |
| 61 | + normalize_input_vectors(input_vectors) |
| 62 | + train_dataset(p, input_vectors, outputs) |
| 63 | + print_perceptron(p) |
| 64 | + |
| 65 | + assert p.cur_accuracy > 50.0 |
| 66 | + assert p.epochs_cnt > 1 |
| 67 | + |
| 68 | + plot_graph(p, input_vectors, outputs) |
| 69 | + |
| 70 | +def main1(): |
| 71 | + p: Perceptron = Perceptron(0, [0.0], 0.0, 0, 0.0, 0.0, 0) |
| 72 | + init_perceptron(p, 2, 0.05, 10000, 90.0) |
| 73 | + print_perceptron(p) |
| 74 | + print("=================================") |
| 75 | + |
| 76 | + input_vectors: list[list[f64]] = [[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0], [1.5, 1.0]] |
| 77 | + outputs: list[i32] = [1, 1, -1, 1, -1] |
| 78 | + |
| 79 | + normalize_input_vectors(input_vectors) |
| 80 | + train_dataset(p, input_vectors, outputs) |
| 81 | + print_perceptron(p) |
| 82 | + |
| 83 | + assert p.cur_accuracy > 50.0 |
| 84 | + assert p.epochs_cnt > 1 |
| 85 | + |
| 86 | + plot_graph(p, input_vectors, outputs) |
| 87 | + |
| 88 | +main0() |
| 89 | +main1() |
0 commit comments