Last active
July 3, 2026 16:03
-
-
Save thomasnield/382d09459f433ec8e4b3b83d9a640ab1 to your computer and use it in GitHub Desktop.
deep_learning_from_scratch_EXERCISE_1.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
| """ | |
| Neural network prediction on a maintenance dataset using PyTorch. | |
| Predicts whether a part needs replacement (1) or not (0). | |
| Uses 3 nodes in the hidden layer and ReLU as the activation function. | |
| """ | |
| import pandas as pd | |
| import torch | |
| import torch.nn as nn | |
| from torch.utils.data import DataLoader, TensorDataset | |
| # Hyperparameters | |
| LEARNING_RATE = 0.001 | |
| EPOCHS = ? | |
| BATCH_SIZE = 32 | |
| # Data loading | |
| df = pd.read_csv('https://bit.ly/3wlFsb4') | |
| # Extract and scale input variables (all columns except last) | |
| X = df.values[:, :-1] / 1000.0 | |
| # Extract output column (last column) | |
| Y = df.values[:, -1] | |
| # Convert to PyTorch tensors | |
| X_tensor = torch.tensor(X, dtype=torch.float32) | |
| Y_tensor = torch.tensor(Y, dtype=torch.float32).unsqueeze(1) # shape: (N, 1) | |
| dataset = TensorDataset(X_tensor, Y_tensor) | |
| dataloader = DataLoader(dataset, batch_size=BATCH_SIZE, shuffle=True) | |
| # Model declaration | |
| n_features = X.shape[1] | |
| model = nn.Sequential( | |
| nn.Linear(n_features, ?), # hidden layer: n_features → 3 nodes | |
| ?, # ReLU activation | |
| nn.Linear(?, ?), # output layer: 3 → 1 node | |
| ? # Sigmoid for binary classification | |
| ) | |
| # loss function and optimization | |
| loss_fn = nn.MSELoss() | |
| optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE) | |
| # training | |
| model.train() | |
| for epoch in range(EPOCHS): | |
| epoch_loss = 0.0 | |
| for X_batch, Y_batch in dataloader: | |
| optimizer.zero_grad() | |
| predictions = model(X_batch) | |
| loss = loss_fn(predictions, Y_batch) | |
| loss.backward() | |
| optimizer.step() | |
| epoch_loss += loss.item() * len(X_batch) | |
| if (epoch + 1) % 10 == 0: | |
| avg_loss = epoch_loss / len(dataset) | |
| print(f"Epoch {epoch + 1:>3}/{EPOCHS} | Loss: {avg_loss:.4f}") | |
| # evaluate performance | |
| # model.eval() | |
| with torch.no_grad(): | |
| all_preds = model(X_tensor) | |
| # Round probabilities to 0 or 1 for accuracy calculation | |
| binary_preds = (all_preds >= 0.5).float() | |
| accuracy = (binary_preds == Y_tensor).float().mean().item() | |
| print(f"\nDataset Score (Accuracy): {accuracy:.4f}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment