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