Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_learnique.py
- import tkinter as tk
- import random
- import string
- import math
- import collections
- ww = 1200
- hh = 600
- root = tk.Tk()
- root.title("# tk_learnique.py")
- 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)
- len_nodes = 300
- def get_nodes(data):
- node = []
- for idx1, s1 in enumerate(data):
- for idx2, s2 in enumerate(data[idx1+1:]):
- s = f"{s1}:{idx1}:{s2}:{idx1+idx2}"
- node += [s]
- # print(len(node)) # gets len_nodes
- return node
- def sum_weights(nodes):
- w = {}
- node_sums = set()
- for category in range(1, 6):
- w[category] = 0
- for node in nodes:
- s1, idx1, s2, idx2 = node.split(':')
- try:
- t = weights[category][node]
- w[category] += t
- except:
- weights[category][node] = 0
- t = 0
- node_sums.add((s1, idx1))
- w = [(w[cat], cat) for cat in w]
- w.sort(reverse=True)
- return w, node_sums
- def gym():
- if prediction == -1:
- for node in nodes:
- weights[correct_category][node] = weights[correct_category][node] + 100
- else:
- for node in nodes:
- weights[correct_category][node] = min(99, weights[prediction][node] + 1)
- def predict():
- nodes = get_nodes(data)
- sorted_sums, node_sums = sum_weights(nodes)
- prediction = sorted_sums[0][1]
- return prediction, sorted_sums, node_sums, nodes
- def update_canvas(i):
- cell_width = 20
- cell_height = 20
- num_cols = 25
- for char, j in node_sums:
- k = str_idx[char, j, correct_category]
- str_idx[char, j, correct_category] += 50
- color_value = get_color[min(999, k)] # ZZZ
- x0 = int(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")
- result_text = f"Y [{correct_category}] " if is_correct else f"N [{correct_category}] "
- progress.append(1 if is_correct else 0)
- progress[:] = progress[-100:]
- gain = sum(progress)
- gain_text = f"{gain}% 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 = 0
- for wt, idx in sorted_sums:
- canvas.create_text(650 + (60 * k), i * cell_height + 10, text=f"{idx}: {int(wt)}", fill="white", anchor=tk.W)
- k += 1
- canvas.create_text(650 + (60 * k), i * cell_height + 10, text=line+1, fill="white", anchor=tk.W)
- canvas.config(scrollregion=(0, 0, ww, (i + 1) * cell_height + cell_height))
- root.update()
- progress = []
- weights = {i+1: collections.defaultdict(int) for i in range(5)}
- j = 256 / 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 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)
- category_functions = {
- 1: category_1,
- 2: category_2,
- 3: category_3,
- 4: category_4,
- 5: category_5
- }
- def generate_training_data(category):
- return category_functions[category]()
- def toggle_auto_scroll(event):
- global auto_scroll
- auto_scroll = not auto_scroll
- root.bind("<space>", toggle_auto_scroll)
- 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]
- str_idx = {}
- for cat in range(1,6):
- for s in string.ascii_letters + string.digits:
- for idx in range(25):
- str_idx[s, str(idx), cat] = 0
- line = 0
- auto_scroll = True
- while 1:
- data, correct_category = training_data[line % number_of_data]
- prediction, sorted_sums, node_sums, nodes = predict()
- is_correct = prediction == correct_category
- if not sorted_sums[0][0]:
- prediction = -1
- if not is_correct:
- gym()
- update_canvas(line)
- if auto_scroll:
- canvas.yview_moveto(1.0)
- line += 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement