classification pt1

August 16, 2023

here's a simple script I'll be working with to understand some of the concepts of classification

  •   import matplotlib.pyplot as plt
      from sklearn.datasets import make_circles
    
      n_Samples = 1000
    
      X, y = make_circles(n_samples=n_samples,
                         noise=0.03,
                         random_state=42)
    
      X = torch.from_numpy(X).type(torch.float)
      y = torch.from_numpy(y).type(torch.float)
    
      X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
    
      # Build model with non-linear activation function
      from torch import nn
      class CircleModelV2(nn.Module):
          def __init__(self):
              super().__init__()
              self.layer_1 = nn.Linear(in_features=2, out_features=10)
              self.layer_2 = nn.Linear(in_features=10, out_features=10)
              self.layer_3 = nn.Linear(in_features=10, out_features=1)
              self.relu = nn.ReLU()
          def forward(self, x):
             return self.layer_3(self.relu(self.layer_2(self.relu(self.layer_1(x)))))
    
      model_3 = CircleModelV2().to(device)
    
      # Setup loss and optimizer
      loss_fn = nn.BCEWithLogitsLoss()
      optimizer = torch.optim.SGD(model_3.parameters(), lr=0.1)
    
      # Fit the model
      torch.manual_seed(42)
      epochs = 10000
    
      # Put all data on target device
      X_train, y_train = X_train.to(device), y_train.to(device)
      X_test, y_test = X_test.to(device), y_test.to(device)
    
      for epoch in range(epochs):
          # 1. Forward pass
          y_logits = model_3(X_train).squeeze()
          y_pred = torch.round(torch.sigmoid(y_logits)) # logits -> prediction probabilities -> prediction labels
    
          # 2. Calculate loss and accuracy
          loss = loss_fn(y_logits, y_train) # BCEWithLogitsLoss calculates loss using logits
          acc = accuracy_fn(y_true=y_train,
                            y_pred=y_pred)
    
          # 3. Optimizer zero grad
          optimizer.zero_grad()
    
          # 4. Loss backward
          loss.backward()
    
          # 5. Optimizer step
          optimizer.step()
    
          ### Testing
          model_3.eval()
          with torch.inference_mode():
            # 1. Forward pass
            test_logits = model_3(X_test).squeeze()
            test_pred = torch.round(torch.sigmoid(test_logits)) # logits -> prediction probabilities -> prediction labels
            # 2. Calcuate loss and accuracy
            test_loss = loss_fn(test_logits, y_test)
            test_acc = sklearn.metrics.accuracy_score(y_test, test_pred)
    
          # Print out what's happening
          if epoch % 100 == 0:
              print(f"Epoch: {epoch} | Loss: {loss:.5f} | Test Loss: {test_loss:.5f}")

    I'll split this into small parts, so the knowledge is more composable for the future