Spaces:
Runtime error
Runtime error
| import torch | |
| def predict_score(x1, x2): | |
| Theta0 = torch.tensor(-0.5738734424645411) | |
| Theta1 = torch.tensor(2.1659122905141825) | |
| Theta2 = torch.tensor(0.0) | |
| y_actual = Theta0 + Theta1 * x1 + Theta2 * 23 # Adjust the constant value here if needed | |
| return y_actual.item() | |
| def gradient_descent(): | |
| # Input data | |
| x1 = torch.tensor([50, 60, 70, 80, 90]) | |
| x2 = torch.tensor([20, 21, 22, 23, 24]) | |
| y_actual = torch.tensor([30, 35, 40, 45, 50]) | |
| # Learning rate and maximum number of iterations | |
| alpha = 0.01 | |
| max_iters = 1000 | |
| # Initial values for Theta0, Theta1, and Theta2 | |
| Theta0 = torch.tensor(0.0, requires_grad=True) | |
| Theta1 = torch.tensor(0.0, requires_grad=True) | |
| Theta2 = torch.tensor(0.0, requires_grad=True) | |
| # Start the iteration counter | |
| iter_count = 0 | |
| # Loop until convergence or maximum number of iterations | |
| while iter_count < max_iters: | |
| # Compute the predicted output | |
| y_pred = Theta0 + Theta1 * x1 + Theta2 * x2 | |
| # Compute the errors | |
| errors = y_pred - y_actual | |
| # Compute the cost function | |
| cost = torch.sum(errors ** 2) / (2 * len(x1)) | |
| # Print the cost function every 100 iterations | |
| if iter_count % 100 == 0: | |
| print("Iteration {}: Cost = {}, Theta0 = {}, Theta1 = {}, Theta2 = {}".format(iter_count, cost, Theta0.item(), Theta1.item(), Theta2.item())) | |
| # Check for convergence (if the cost is decreasing by less than 0.0001) | |
| if iter_count > 0 and torch.abs(cost - prev_cost) < 0.0001: | |
| print("Converged after {} iterations".format(iter_count)) | |
| break | |
| # Perform automatic differentiation to compute gradients | |
| cost.backward() | |
| # Update Theta0, Theta1, and Theta2 using gradient descent | |
| with torch.no_grad(): | |
| Theta0 -= alpha * Theta0.grad | |
| Theta1 -= alpha * Theta1.grad | |
| Theta2 -= alpha * Theta2.grad | |
| # Reset gradients for the next iteration | |
| Theta0.grad.zero_() | |
| Theta1.grad.zero_() | |
| Theta2.grad.zero_() | |
| # Update the iteration counter and previous cost | |
| iter_count += 1 | |
| prev_cost = cost | |
| # Print the final values of Theta0, Theta1, and Theta2 | |
| print("Final values: Theta0 = {}, Theta1 = {}, Theta2 = {}".format(Theta0.item(), Theta1.item(), Theta2.item())) | |
| print("Final Cost: Cost = {}".format(cost.item())) | |
| print("Final values: y_pred = {}, y_actual = {}".format(y_pred, y_actual)) | |
| # Launch the prediction interface | |
| while True: | |
| x1 = float(input("Enter the number of new students: ")) | |
| x2 = float(input("Enter the number of temperature: ")) | |
| predicted_rooms = predict_score(x1, x2) | |
| print("Predicted rooms:", predicted_rooms) | |
| print() | |
| choice = input("Do you want to predict again? (y/n): ") | |
| if choice.lower() != 'y': | |
| break | |
| gradient_descent() | |