Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_ReLu_With_Matrix_Test.py ZZZ unable to make this "learn"
- import tkinter as tk
- import random
- import math
- import string
- ww = 1200
- hh = 600
- root = tk.Tk()
- root.title("tk ReLu With Matrix Test")
- root.geometry(f"{ww}x{hh}+10+10")
- scrollbar = tk.Scrollbar(root)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- canvas = tk.Canvas(root, width=ww, height=hh, bg="black", yscrollcommand=scrollbar.set)
- canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
- scrollbar.config(command=canvas.yview)
- def dot(a, b):
- return [[sum(x * y for x, y in zip(row, col)) for col in zip(*b)] for row in a]
- def transpose(matrix):
- return list(zip(*matrix))
- def leaky_relu(matrix):
- return [[max(0.01 * x, x) for x in row] for row in matrix]
- def square_diff(matrix1, matrix2):
- return sum((x - y) ** 2 for row1, row2 in zip(matrix1, matrix2) for x, y in zip(row1, row2))
- def create_random_matrix(rows, cols):
- return [[random.uniform(-1, 1) for _ in range(cols)] for _ in range(rows)]
- def backpropagation(x, y):
- global grad_w2, grad_w1
- h = dot(x, w1)
- h_relu = leaky_relu(h)
- y_pred = dot(h_relu, w2)
- grad_y_pred = [[2 * (yp - yt) for yp, yt in zip(yp_row, yt_row)] for yp_row, yt_row in zip(y_pred, y)]
- grad_w2 = dot(transpose(h_relu), grad_y_pred)
- grad_h_relu = dot(grad_y_pred, transpose(w2))
- grad_h = [[ghr if h_val > 0 else 0.01 * ghr for ghr, h_val in zip(ghr_row, h_row)] for ghr_row, h_row in zip(grad_h_relu, h)]
- grad_w1 = dot(transpose(x), grad_h)
- def compute_gain(x, y):
- global h_relu, h, y_pred, gain
- h = dot(x, w1)
- h_relu = leaky_relu(h)
- y_pred = dot(h_relu, w2)
- gain = square_diff(y_pred, y)
- gain_col.append(gain)
- encode_factor = 1 / 62
- encode_index = dict((s, ((i + 1) * encode_factor)) for (i, s) in enumerate(string.digits + string.ascii_letters))
- def encode_input(data):
- encoded = []
- for char in data:
- encoded.append(encode_index[char])
- return encoded
- def softmax(x):
- max_val = max(x)
- exps = [math.exp(val - max_val) for val in x]
- sum_exps = sum(exps)
- return [exp / sum_exps for exp in exps]
- def euclidean_distance(v1, v2):
- return math.sqrt(sum((a - b) ** 2 for a, b in zip(v1, v2)))
- def predict(data):
- encoded_data = encode_input(data)
- x = [encoded_data]
- h = dot(x, w1)
- h_relu = leaky_relu(h)
- y_pred = dot(h_relu, w2)[0]
- normalized_predictions = softmax(y_pred)
- min_distance = float('inf')
- nearest_category = None
- distances = []
- for category in range(1, 6):
- target_vector = all_target_weights[category]
- distance = euclidean_distance(normalized_predictions, target_vector)
- distances += [(100 - distance, category)]
- if distance < min_distance:
- min_distance = distance
- nearest_category = category
- prediction = nearest_category
- confidence = max(normalized_predictions)
- return prediction, confidence, distances
- N, D_in, H, D_out = 10, 25, 12, 5 # Adjusted D_out for 5 categories
- w1 = create_random_matrix(D_in, H)
- w2 = create_random_matrix(H, D_out)
- learning_rate = 0.002
- gain_col = []
- w1_changes = [[0 for _ in range(H)] for _ in range(D_in)]
- w2_changes = [[0 for _ in range(D_out)] for _ in range(H)]
- j = 255 / 400
- a = [int(i * j) for i in range(400)]
- b = [0 for _ in range(300)]
- red = a[::-1] + b + b
- green = b + b + a
- blue = b + a + b
- get_color = [f'#{r:02X}{g:02X}{b:02X}' for r, g, b in zip(red, green, blue)]
- def get_color_from_weight(weight, target_weight):
- diff = abs(weight - target_weight)
- diff = 1 - max(0, min(1, diff))
- return get_color[int(diff * 999)]
- # Training Data
- def shuffle_mixed_case(s):
- mixed = [c.lower() if random.random() < 0.5 else c.upper() for c in s]
- random.shuffle(mixed)
- return ''.join(mixed)
- def category_1():
- digits = ''.join(random.choices(string.digits, k=7))
- combined = digits + 'yellow'
- combined += ''.join(random.choices(string.ascii_letters, k=(25 - len(combined))))
- return shuffle_mixed_case(combined)
- def category_2():
- digits = ''.join(random.choices(string.digits, k=9))
- combined = digits + 'helloworld'
- combined += ''.join(random.choices(string.ascii_letters, k=(25 - len(combined))))
- return shuffle_mixed_case(combined)
- def category_3():
- combined = 'python3407'
- combined += ''.join(random.choices(string.ascii_letters + string.digits, k=(25 - len(combined))))
- return shuffle_mixed_case(combined)
- def category_4():
- combined = 'thankyou12345'
- combined += ''.join(random.choices(string.ascii_letters + string.digits, k=(25 - len(combined))))
- return shuffle_mixed_case(combined)
- def category_5():
- s = []
- for i in range(12):
- s.append(random.choice(string.ascii_letters))
- s.append(random.choice(string.digits))
- s.append(random.choice(string.ascii_letters))
- return ''.join(s)
- def generate_training_data(category=None):
- if category not in (1, 2, 3, 4, 5):
- category = random.randint(1, 5)
- if category == 1:
- return category_1()
- elif category == 2:
- return category_2()
- elif category == 3:
- return category_3()
- elif category == 4:
- return category_4()
- elif category == 5:
- return category_5()
- def update_canvas(i):
- cell_width = 20
- cell_height = 20
- num_cols = 25
- encoded_data = encode_input(data)
- target_weights = [all_target_weights[correct_category][N] for N in range(25)]
- for j, char in enumerate(data):
- color_value = get_color_from_weight(encoded_data[j], target_weights[j])
- x0 = j * cell_width
- y0 = i * cell_height
- x1 = x0 + cell_width
- y1 = y0 + cell_height
- canvas.create_rectangle(x0, y0, x1, y1, fill=color_value, outline="gray")
- canvas.create_text(x0 + 10, y0 + 10, text=char, fill="white")
- is_correct = prediction == correct_category
- result_text = f"Y [{correct_category}] " if is_correct else f"N [{correct_category}] "
- progress.append(100 if is_correct else 0)
- progress.pop(0)
- gain = sum(progress) / 500
- gain_text = f"{gain:.2f}% gain"
- canvas.create_text(num_cols * cell_width + 30, i * cell_height + 10, text=result_text, fill="white", anchor=tk.W)
- canvas.create_text(num_cols * cell_width + 80, i * cell_height + 10, text=gain_text, fill="white", anchor=tk.W)
- k = 2
- for distance, idx in distances:
- canvas.create_text(num_cols * cell_width + (95 * k), i * cell_height + 10, text=f"{idx}: {distance:.5f}%", fill="white", anchor=tk.W)
- k += 1
- canvas.config(scrollregion=(0, 0, ww, (i + 1) * cell_height + cell_height))
- root.update()
- number_of_data = 5000
- categories = [1, 2, 3, 4, 5] * (number_of_data // 5)
- random.shuffle(categories)
- training_data = [(generate_training_data(category), category) for category in categories]
- training_inputs = [encode_input(data[0]) for data in training_data]
- training_labels = [[1 if i + 1 == data[1] else 0 for i in range(D_out)] for data in training_data]
- progress = [0 for i in range(500)]
- all_target_weights = {
- 1: [0.6 for _ in range(25)],
- 2: [0.3 for _ in range(25)],
- 3: [0.00 for _ in range(25)],
- 4: [-0.3 for _ in range(25)],
- 5: [-0.6 for _ in range(25)]
- }
- batch_size = 30
- num_batches = len(categories) // batch_size
- # Training loop
- line = 0
- for epoch in range(100): # Number of epochs
- for batch in range(num_batches):
- x_batch = training_inputs[batch * batch_size:(batch + 1) * batch_size]
- y_batch = training_labels[batch * batch_size:(batch + 1) * batch_size]
- compute_gain(x_batch, y_batch)
- backpropagation(x_batch, y_batch)
- for i in range(batch_size):
- data, correct_category = training_data[batch * batch_size + i]
- prediction, confidence, distances = predict(data)
- update_canvas(line)
- line += 1
- for n in range(25):
- all_target_weights[correct_category][n] += learning_rate * (training_labels[batch * batch_size + i][correct_category - 1] - sum(w1[n]))
- for i in range(D_in):
- for j in range(H):
- w1[i][j] -= learning_rate * grad_w1[i][j]
- w1_changes[i][j] += learning_rate * grad_w1[i][j]
- for i in range(H):
- for j in range(D_out):
- w2[i][j] -= learning_rate * grad_w2[i][j]
- w2_changes[i][j] += learning_rate * grad_w2[i][j]
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement