Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_pattern_function_indexify2.py
- import tkinter as tk
- def neural_network(pattern, index):
- # define the neural network function
- def fn(x):
- # use the weighing nodes to determine the value at the given index
- output = 0
- for i, p in enumerate(pattern):
- output += ord(p) * (x**i)
- return output
- return fn
- def train():
- # run the neural network in a loop until the result matches the expected result
- global go
- go = True
- # retrieve the user-entered values from the entry widgets
- pattern = pattern_entry.get()
- index = int(index_entry.get())
- expected_result = expected_result_entry.get()
- # find the pattern string by training the neural network
- fn = neural_network(pattern, index)
- result = fn(index)
- while go and (result != expected_result):
- # adjust the weights of the nodes
- for i, p in enumerate(pattern):
- pattern[i] = chr(max(0, ord(p)))
- fn = neural_network(pattern, index)
- result = fn(index)
- if not go:
- print("Cancel Was Pressed\n")
- # create the fn_code template string
- fn_code = "def fn(x):\n"
- for i, p in enumerate(pattern):
- fn_code += f" output += {ord(p)} * (x**{i})\n"
- fn_code += " return output"
- # print the fn_code template string and the fn function
- print(fn_code)
- print(fn)
- def stop_neural_network():
- # stop the neural network loop
- global go
- go = False
- # create the Tkinter GUI
- root = tk.Tk()
- root.title("tk_pattern_function_indexify")
- # create the entry widgets for the pattern, index, and expected result
- pattern_label = tk.Label(root, text="Enter The Incomplete Pattern:")
- pattern_entry = tk.Entry(root, width=200)
- index_label = tk.Label(root, text="Enter A Test Index:")
- index_entry = tk.Entry(root, width=200)
- expected_result_label = tk.Label(root, text="Enter An Expected Result By The Test Index:")
- expected_result_entry = tk.Entry(root, width=200)
- # create the train and stop buttons
- train_button = tk.Button(root, text="Train", command=train)
- stop_button = tk.Button(root, text="Cancel", command=stop_neural_network)
- # pack the entry widgets and buttons into the GUI
- pattern_label.pack()
- pattern_entry.pack()
- index_label.pack()
- index_entry.pack()
- expected_result_label.pack()
- expected_result_entry.pack()
- train_button.pack()
- stop_button.pack()
- # run the Tkinter event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement