Spaces:
Runtime error
Runtime error
appo.py
Browse files
app.py
CHANGED
@@ -1,7 +1,80 @@
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
|
3 |
-
def
|
4 |
-
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import torch
|
3 |
import gradio as gr
|
4 |
|
5 |
+
def predict_score(x1, x2):
|
6 |
+
Theta0 = torch.tensor(-0.5738734424645411)
|
7 |
+
Theta1 = torch.tensor(2.1659122905141825)
|
8 |
+
Theta2 = torch.tensor(0.0)
|
9 |
+
y_actual = Theta0 + Theta1 * x1 + Theta2 * 23 # Adjust the constant value here if needed
|
10 |
+
return y_actual.item()
|
11 |
|
12 |
+
input1 = gr.inputs.Number(label='Number of New Students')
|
13 |
+
input2 = gr.inputs.Number(label='Number of Temperature')
|
14 |
+
|
15 |
+
output = gr.outputs.Textbox(label='Predicted Rooms')
|
16 |
+
|
17 |
+
gr.Interface(fn=predict_score, inputs=[input1, input2], outputs=output).launch()
|
18 |
+
|
19 |
+
def pred(y_actual,y_pred, x1, x2):
|
20 |
+
# Input data
|
21 |
+
x1 = torch.tensor([50, 60, 70, 80, 90])
|
22 |
+
x2 = torch.tensor([20, 21, 22, 23, 24])
|
23 |
+
y_actual = torch.tensor([30, 35, 40, 45, 50])
|
24 |
+
|
25 |
+
# Learning rate and maximum number of iterations
|
26 |
+
alpha = 0.01
|
27 |
+
max_iters = 1000
|
28 |
+
|
29 |
+
# Initial values for Theta0, Theta1, and Theta2
|
30 |
+
Theta0 = torch.tensor(0.0, requires_grad=True)
|
31 |
+
Theta1 = torch.tensor(0.0, requires_grad=True)
|
32 |
+
Theta2 = torch.tensor(0.0, requires_grad=True)
|
33 |
+
|
34 |
+
# Start the iteration counter
|
35 |
+
iter_count = 0
|
36 |
+
|
37 |
+
# Loop until convergence or maximum number of iterations
|
38 |
+
while iter_count < max_iters:
|
39 |
+
# Compute the predicted output
|
40 |
+
y_pred = Theta0 + Theta1 * x1 + Theta2 * x2
|
41 |
+
|
42 |
+
# Compute the errors
|
43 |
+
errors = y_pred - y_actual
|
44 |
+
|
45 |
+
# Compute the cost function
|
46 |
+
cost = torch.sum(errors ** 2) / (2 * len(x1))
|
47 |
+
|
48 |
+
# Print the cost function every 100 iterations
|
49 |
+
if iter_count % 100 == 0:
|
50 |
+
print("Iteration {}: Cost = {}, Theta0 = {}, Theta1 = {}, Theta2 = {}".format(iter_count, cost, Theta0.item(), Theta1.item(), Theta2.item()))
|
51 |
+
|
52 |
+
# Check for convergence (if the cost is decreasing by less than 0.0001)
|
53 |
+
if iter_count > 0 and torch.abs(cost - prev_cost) < 0.0001:
|
54 |
+
print("Converged after {} iterations".format(iter_count))
|
55 |
+
break
|
56 |
+
|
57 |
+
# Perform automatic differentiation to compute gradients
|
58 |
+
cost.backward()
|
59 |
+
|
60 |
+
# Update Theta0, Theta1, and Theta2 using gradient descent
|
61 |
+
with torch.no_grad():
|
62 |
+
Theta0 -= alpha * Theta0.grad
|
63 |
+
Theta1 -= alpha * Theta1.grad
|
64 |
+
Theta2 -= alpha * Theta2.grad
|
65 |
+
|
66 |
+
# Reset gradients for the next iteration
|
67 |
+
Theta0.grad.zero_()
|
68 |
+
Theta1.grad.zero_()
|
69 |
+
Theta2.grad.zero_()
|
70 |
+
|
71 |
+
# Update the iteration counter and previous cost
|
72 |
+
iter_count += 1
|
73 |
+
prev_cost = cost
|
74 |
+
|
75 |
+
gr.Interface(fn=predict_score, inputs=[input1, input2], outputs=output).launch()
|
76 |
+
|
77 |
+
# Print the final values of Theta0, Theta1, and Theta2
|
78 |
+
print("Final values: Theta0 = {}, Theta1 = {}, Theta2 = {}".format(Theta0.item(), Theta1.item(), Theta2.item()))
|
79 |
+
print("Final Cost: Cost = {}".format(cost.item()))
|
80 |
+
print("Final values: y_pred = {}, y_actual = {}".format(y_pred, y_actual))
|