|
| 1 | +from torch.autograd import Variable |
| 2 | +import torch |
| 3 | +import tkinter as tk |
| 4 | +from tkinter import ttk |
| 5 | +import numpy as np |
| 6 | +import matplotlib.pyplot as plt |
| 7 | +import seaborn as sns |
| 8 | +import pandas as pd |
| 9 | +from sklearn.model_selection import train_test_split |
| 10 | +sns.set() |
| 11 | + |
| 12 | +win = tk.Tk() |
| 13 | +win.resizable(width=0, height=0) |
| 14 | +win.title("Fibonnachi") |
| 15 | +n_label = ttk.Label(win, text="Enter the n:") |
| 16 | +n_label.grid(row=0, column=0, sticky=tk.W) |
| 17 | +n_value = tk.IntVar() |
| 18 | +n_value.set(10) |
| 19 | +n_entrybox = ttk.Entry(win, width=14, textvariable=n_value) |
| 20 | +n_entrybox.grid(row=0, column=1) |
| 21 | +a_label = ttk.Label(win, text="a:") |
| 22 | +a_label.grid(row=1, column=0, sticky=tk.W) |
| 23 | +a_value = tk.IntVar() |
| 24 | +a_value.set(1) |
| 25 | +a_entrybox = ttk.Entry(win, width=14, textvariable=a_value) |
| 26 | +a_entrybox.grid(row=1, column=1) |
| 27 | +b_label = ttk.Label(win, text="b:") |
| 28 | +b_label.grid(row=2, column=0, sticky=tk.W) |
| 29 | +b_value = tk.IntVar() |
| 30 | +b_value.set(2) |
| 31 | +b_entrybox = ttk.Entry( |
| 32 | + win, width=14, textvariable=b_value) |
| 33 | +b_entrybox.grid(row=2, column=1) |
| 34 | +n_out = 1 |
| 35 | +n = 0 |
| 36 | +A, B = [], [] |
| 37 | +for i in range(1, 10): |
| 38 | + for j in range(i, 10): |
| 39 | + n += 1 |
| 40 | + A.append(i) |
| 41 | + B.append(j) |
| 42 | +A, B = np.array(A), np.array(B) |
| 43 | +for i in range(n): |
| 44 | + if A[i] > B[i]: |
| 45 | + A[i], B[i] = B[i], A[i] |
| 46 | +data = pd.DataFrame({}) |
| 47 | +data["A"] = A |
| 48 | +data["B"] = B |
| 49 | +X = np.array(data) |
| 50 | +y = np.array(data["A"]+data["B"]).reshape(-1, 1) |
| 51 | +X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) |
| 52 | +print("X", X[:5], "\n\n", "y", y[:5]) |
| 53 | +dtype = torch.FloatTensor |
| 54 | +N, Din, Dout = n, 2, n_out |
| 55 | +x = Variable(torch.Tensor(X_train).type(dtype), requires_grad=False) |
| 56 | +x_test = Variable(torch.Tensor(X_test).type(dtype), requires_grad=False) |
| 57 | +y = Variable(torch.Tensor(y_train).type(dtype), requires_grad=False) |
| 58 | +y_test = Variable(torch.Tensor(y_test).type(dtype), requires_grad=False) |
| 59 | +w = Variable(torch.randn(Din, Dout).type(dtype), requires_grad=True) |
| 60 | +learning_rate = 0.00001 |
| 61 | +train_loss_list = [] |
| 62 | +test_loss_list = [] |
| 63 | +for t in range(1000): |
| 64 | + y_pred = x.mm(w) |
| 65 | + y_pred_test = x_test.mm(w) |
| 66 | + loss = (y_pred - y).pow(2).sum() |
| 67 | + loss_test = (y_pred_test-y_test).pow(2).sum() |
| 68 | + train_loss_list.append(loss.data) |
| 69 | + test_loss_list.append(loss_test) |
| 70 | + loss.backward() |
| 71 | + w.data -= learning_rate * w.grad.data |
| 72 | + w.grad.data.zero_() |
| 73 | +plt.plot(train_loss_list, label="train loss") |
| 74 | +plt.plot(test_loss_list, label="test_loss") |
| 75 | +plt.xlabel("number of iterations") |
| 76 | +plt.ylabel("cost") |
| 77 | +plt.legend() |
| 78 | +plt.show() |
| 79 | + |
| 80 | + |
| 81 | +def sum_new(a, b, w): |
| 82 | + return round(float(torch.Tensor([[a, b]]).mm(w)[0][0].data)) |
| 83 | + |
| 84 | + |
| 85 | +def fibo(w, a, b, n): |
| 86 | + if n == 1: |
| 87 | + return a |
| 88 | + a_sum_b = sum_new(a, b, w) |
| 89 | + return fibo(w, b, a_sum_b, n-1) |
| 90 | + |
| 91 | + |
| 92 | +def main(): |
| 93 | + a, b, n = a_value.get(), b_value.get(), n_value.get() |
| 94 | + for i in range(1, n+1): |
| 95 | + print(fibo(w, a, b, i), end=" ") |
| 96 | + print("\n") |
| 97 | + |
| 98 | + |
| 99 | +submit_button = tk.Button( |
| 100 | + win, text="Generate", command=main) |
| 101 | +submit_button.grid(row=3, columnspan=3) |
| 102 | +win.mainloop() |
0 commit comments