ium_464863/IUM_05.ipynb

9.2 KiB

IUM_05

Wymagane zależności

import numpy as np
import pandas as pd
import torch
import torch.nn as nn
import torch.optim as optim

import os

from sklearn.metrics import classification_report

Model

# Neural Network
class NeuralNetwork(nn.Module):
    """
    Neural network model for classification problem.
    """

    def __init__(self, input_size, hidden_size):
        super(NeuralNetwork, self).__init__()

        self.fc1 = nn.Linear(input_size, hidden_size)
        self.fc2 = nn.Linear(hidden_size, hidden_size // 2)
        self.fc3 = nn.Linear(hidden_size // 2, 1)

        self.relu = nn.ReLU()
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        out = self.fc1(x)
        out = self.relu(out)
        out = self.fc2(out)
        out = self.relu(out)
        out = self.fc3(out)
        out = self.sigmoid(out)
        return out
# Seed for reproducibility
torch.manual_seed(1234)
<torch._C.Generator at 0x226dc7744f0>

Wczytywanie danych

# Load data
train = pd.read_csv('datasets/train.csv')
test = pd.read_csv('datasets/test.csv')

# Split data
X_train = train.drop(columns=['id', 'diagnosis']).values
y_train = train['diagnosis'].values

X_test = test.drop(columns=['id', 'diagnosis']).values
y_test = test['diagnosis'].values

# Convert data to PyTorch tensors
X_train = torch.FloatTensor(X_train)
y_train = torch.FloatTensor(y_train).view(-1, 1)

X_test = torch.FloatTensor(X_test)
y_test = torch.FloatTensor(y_test).view(-1, 1)

Parametry modelu

# Parameters
input_size = X_train.shape[1]
hidden_size = 128
learning_rate = 0.001
weight_decay = 0.001
num_epochs = 1000
# Model initialization
model = NeuralNetwork(input_size, hidden_size)
# Loss function and optimizer
criterion = nn.BCELoss()
optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=weight_decay)

Trenowanie modelu

# Training loop
for epoch in range(num_epochs):
    # Zero the gradients
    optimizer.zero_grad()

    # Forward pass
    outputs = model(X_train)

    # Compute loss
    loss = criterion(outputs, y_train)

    # Backward pass
    loss.backward()

    # Update weights
    optimizer.step()

    # Print loss
    if (epoch + 1) % 100 == 0:
        print(f'Epoch [{epoch + 1}/{num_epochs}], Loss: {loss.item()}')
Epoch [100/1000], Loss: 0.08850393444299698
Epoch [200/1000], Loss: 0.039436809718608856
Epoch [300/1000], Loss: 0.031037550419569016
Epoch [400/1000], Loss: 0.026670493185520172
Epoch [500/1000], Loss: 0.023590415716171265
Epoch [600/1000], Loss: 0.02146584913134575
Epoch [700/1000], Loss: 0.019706938415765762
Epoch [800/1000], Loss: 0.018304765224456787
Epoch [900/1000], Loss: 0.017177913337945938
Epoch [1000/1000], Loss: 0.016160517930984497

Ewaluacja modelu

# Test the model
with torch.no_grad():
    y_pred = model(X_test)
    y_pred = np.where(y_pred > 0.5, 1, 0)
    print(classification_report(y_test, y_pred, target_names=['B', 'M']))    
              precision    recall  f1-score   support

           B       0.97      1.00      0.99        34
           M       1.00      0.96      0.98        23

    accuracy                           0.98        57
   macro avg       0.99      0.98      0.98        57
weighted avg       0.98      0.98      0.98        57

Zapisywanie modelu do pliku

# If directory models does not exist, create it
if not os.path.exists('./models'):
    os.makedirs('./models')

# Save the model
torch.save(model, './models/model.pth')