Created
July 2, 2026 14:30
-
-
Save thomasnield/d4fe4faa4e443264c2b5881da456f97b to your computer and use it in GitHub Desktop.
deep_learning_from_scratch_exercise_2.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import pandas as pd | |
| import torch | |
| """ | |
| COMPLETE THE CODE BELOW BY REPLACING THE QUESTION MARKS ?'s | |
| SO FORWARD PROPAGATION IS COMPLETE | |
| """ | |
| url = "https://raw.githubusercontent.com/thomasnield/machine-learning-demo-data/master/classification/maintenance_predict.csv" | |
| df = pd.read_csv(url) | |
| # Extract the input columns, scale down by 255, and convert to PyTorch tensors | |
| # Using float() ensures compatibility with weights during matrix multiplication | |
| X = torch.tensor(df.iloc[:, 0:3].values, dtype=torch.float32) / 255.0 | |
| Y = torch.tensor(df.iloc[:, -1].values, dtype=torch.float32) | |
| # Build neural network with weights and biases | |
| # with random initialization | |
| w_hidden = torch.rand(3, 3) | |
| w_output = torch.rand(1, 3) | |
| b_hidden = torch.rand(3, 1) | |
| b_output = torch.rand(1, 1) | |
| # Activation functions | |
| relu = lambda x: torch.relu(x) | |
| logistic = lambda x: torch.sigmoid(x) | |
| # Runs inputs through the neural network to get predicted outputs | |
| def forward_prop(X): | |
| Z1 = ? @ X + b_hidden | |
| A1 = relu(?) | |
| Z2 = ? @ ? + b_output | |
| A2 = logistic(?) | |
| return Z1, A1, Z2, A2 | |
| # Calculate accuracy | |
| # Note: X.t() is the shorthand transpose method for 2D tensors in PyTorch | |
| test_predictions = forward_prop(X.t())[3] | |
| # .flatten() works similarly; .to(torch.int) replaces .astype(int) | |
| test_comparisons = torch.eq( | |
| (test_predictions >= 0.5).flatten().to(torch.int), Y.to(torch.int) | |
| ) | |
| accuracy = torch.sum(test_comparisons.to(torch.float32)) / X.shape[0] | |
| print("ACCURACY: ", accuracy.item()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment