Advertisement
here2share

# tk_pattern_function_indexify2.py

Dec 24th, 2022
1,932
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.30 KB | None | 0 0
  1. # tk_pattern_function_indexify2.py
  2.  
  3. import tkinter as tk
  4.  
  5. def neural_network(pattern, index):
  6.     # define the neural network function
  7.     def fn(x):
  8.         # use the weighing nodes to determine the value at the given index
  9.         output = 0
  10.         for i, p in enumerate(pattern):
  11.             output += ord(p) * (x**i)
  12.         return output
  13.  
  14.     return fn
  15.  
  16.  
  17. def train():
  18.     # run the neural network in a loop until the result matches the expected result
  19.     global go
  20.     go = True
  21.    
  22.     # retrieve the user-entered values from the entry widgets
  23.     pattern = pattern_entry.get()
  24.     index = int(index_entry.get())
  25.     expected_result = expected_result_entry.get()
  26.  
  27.     # find the pattern string by training the neural network
  28.     fn = neural_network(pattern, index)
  29.     result = fn(index)
  30.     while go and (result != expected_result):
  31.         # adjust the weights of the nodes
  32.         for i, p in enumerate(pattern):
  33.             pattern[i] = chr(max(0, ord(p)))
  34.         fn = neural_network(pattern, index)
  35.         result = fn(index)
  36.        
  37.     if not go:
  38.         print("Cancel Was Pressed\n")
  39.  
  40.     # create the fn_code template string
  41.     fn_code = "def fn(x):\n"
  42.     for i, p in enumerate(pattern):
  43.         fn_code += f"    output += {ord(p)} * (x**{i})\n"
  44.     fn_code += "    return output"
  45.  
  46.     # print the fn_code template string and the fn function
  47.     print(fn_code)
  48.     print(fn)
  49.  
  50. def stop_neural_network():
  51.     # stop the neural network loop
  52.     global go
  53.     go = False
  54.  
  55. # create the Tkinter GUI
  56. root = tk.Tk()
  57. root.title("tk_pattern_function_indexify")
  58.  
  59. # create the entry widgets for the pattern, index, and expected result
  60. pattern_label = tk.Label(root, text="Enter The Incomplete Pattern:")
  61. pattern_entry = tk.Entry(root, width=200)
  62. index_label = tk.Label(root, text="Enter A Test Index:")
  63. index_entry = tk.Entry(root, width=200)
  64. expected_result_label = tk.Label(root, text="Enter An Expected Result By The Test Index:")
  65. expected_result_entry = tk.Entry(root, width=200)
  66.  
  67. # create the train and stop buttons
  68. train_button = tk.Button(root, text="Train", command=train)
  69. stop_button = tk.Button(root, text="Cancel", command=stop_neural_network)
  70.  
  71. # pack the entry widgets and buttons into the GUI
  72. pattern_label.pack()
  73. pattern_entry.pack()
  74. index_label.pack()
  75. index_entry.pack()
  76. expected_result_label.pack()
  77. expected_result_entry.pack()
  78. train_button.pack()
  79. stop_button.pack()
  80.  
  81. # run the Tkinter event loop
  82. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement