Skip to content

Commit 1897b54

Browse files
authored
Merge pull request #15 from ankitaharwal/master
Fibonachi Series
2 parents 02d78d2 + 97bdbcd commit 1897b54

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

assignment_2/ankit/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Problem Statement:
2+
Train a model to print the first `n` elements of the Fibonacci sequence starting at integers `a` and `b`. Note that `a` < `b`, and `n` <= 100 are user supplied arguments. You may not use the standard summing operation to print the next element.Example I/O:
3+
Input: a=5, b=7, n=5
4+
Output: 5, 7, 12, 19, 31
5+
Input: a=7, b=41, n=6
6+
Output: 7, 41, 49, 90, 139, 229
7+
8+
## Submission Guidelines
9+
1. Make a proper readme.md including the approach, dataset, model, experiments, results and steps to run the project.
10+
2. Include requirement.txt file.
11+
3. Make a predict.py file in which a user will input a,b,n and get the results.
12+
13+
## Experiments
14+
1. I inserted little GUI in it because I recently learned little GUI and want to implement it
15+
16+
## Approach
17+
1. We just have to make a model that can add two number.
18+
2. Our model will take a,b and return a+b
19+
3. We will make a function that will print fibonacci series
20+
21+
## Dataset
22+
1. X has two features a,b and very big length
23+
2. y has a+b and same length as of X
24+
25+
## Model
26+
1. We trained our model using simple linear regression using pytorch
27+
2. After training our model gives w=[[1],[1]] it is used for adding a,b

assignment_2/ankit/predict.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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()

assignment_2/ankit/requirement

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
1. python
2+
2. torch
3+
3. tkinter
4+
4. numpy
5+
5. matplotlib
6+
6. seaborn

0 commit comments

Comments
 (0)