app.py
Browse files
app.py
CHANGED
@@ -1,60 +1,14 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
# Start the iteration counter
|
16 |
-
iter_count = 0
|
17 |
-
|
18 |
-
# Loop until convergence or maximum number of iterations
|
19 |
-
while iter_count < max_iters:
|
20 |
-
# Compute the predicted output
|
21 |
-
y_pred = Theta0 + Theta1 * x1 + Theta2 * x2
|
22 |
-
|
23 |
-
# Compute the errors
|
24 |
-
errors = y_pred - y_actual
|
25 |
-
|
26 |
-
# Compute the cost function
|
27 |
-
cost = torch.sum(errors ** 2) / (2 * len(x1))
|
28 |
-
|
29 |
-
# Print the cost function every 100 iterations
|
30 |
-
if iter_count % 100 == 0:
|
31 |
-
print("Iteration {}: Cost = {}, Theta0 = {}, Theta1 = {}, Theta2 = {}".format(iter_count, cost, Theta0.item(), Theta1.item(),
|
32 |
-
Theta2.item()))
|
33 |
-
|
34 |
-
# Check for convergence (if the cost is decreasing by less than 0.0001)
|
35 |
-
if iter_count > 0 and torch.abs(cost - prev_cost) < 0.0001:
|
36 |
-
print("Converged after {} iterations".format(iter_count))
|
37 |
-
break
|
38 |
-
|
39 |
-
# Perform automatic differentiation to compute gradients
|
40 |
-
cost.backward()
|
41 |
-
|
42 |
-
# Update Theta0, Theta1, and Theta2 using gradient descent
|
43 |
-
with torch.no_grad():
|
44 |
-
Theta0 -= alpha * Theta0.grad
|
45 |
-
Theta1 -= alpha * Theta1.grad
|
46 |
-
Theta2 -= alpha * Theta2.grad
|
47 |
-
|
48 |
-
# Reset gradients for the next iteration
|
49 |
-
Theta0.grad.zero_()
|
50 |
-
Theta1.grad.zero_()
|
51 |
-
Theta2.grad.zero_()
|
52 |
-
|
53 |
-
# Update the iteration counter and previous cost
|
54 |
-
iter_count += 1
|
55 |
-
prev_cost = cost
|
56 |
-
|
57 |
-
# Print the final values of Theta0, Theta1, and Theta2
|
58 |
-
print("Final values: Theta0 = {}, Theta1 = {}, Theta2 = {}".format(Theta0.item(), Theta1.item(), Theta2.item()))
|
59 |
-
print("Final Cost: Cost = {}".format(cost.item()))
|
60 |
-
print("Final values: y_pred = {}, y_actual = {}".format(y_pred, y_actual))
|
|
|
1 |
+
pip install numpy
|
2 |
+
pip install gradio
|
3 |
+
import numpy as np
|
4 |
+
import gradio as gr
|
5 |
+
#function to predict the input hours
|
6 |
+
def predict_score(hours):
|
7 |
+
#hours = np.array(hours)
|
8 |
+
pred_score = -0.5738734424645411 + 2.1659122905141825*hours
|
9 |
+
return pred_score #np.round(pred_score[0], 2)
|
10 |
+
input = gr.inputs.Number(label='Number of Hours studied')
|
11 |
+
output = gr.outputs.Textbox(label='Predicted Score')
|
12 |
+
gr.Interface( fn=predict_score,
|
13 |
+
inputs=input,
|
14 |
+
outputs=output).launch();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|