Skip to content

Commit cb83810

Browse files
committed
initial training
1 parent 7248271 commit cb83810

File tree

5 files changed

+12
-7
lines changed

5 files changed

+12
-7
lines changed

nids/model.pth

77.7 KB
Binary file not shown.

nids/model.py

+12-7
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@ def forward(self, x):
2020
return x
2121

2222
def train_model(X_train, y_train, X_test, y_test, epochs):
23+
# Check for GPU availability
24+
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
25+
print(f'Training on device: {device}')
26+
2327
# Convert data to NumPy arrays
2428
X_train_np = X_train.to_numpy()
2529
y_train_np = y_train.to_numpy()
2630
X_test_np = X_test.to_numpy()
2731
y_test_np = y_test.to_numpy()
2832

29-
# Convert data to PyTorch tensors
30-
X_train_tensor = torch.tensor(X_train_np, dtype=torch.float32)
31-
y_train_tensor = torch.tensor(y_train_np, dtype=torch.long)
32-
X_test_tensor = torch.tensor(X_test_np, dtype=torch.float32)
33-
y_test_tensor = torch.tensor(y_test_np, dtype=torch.long)
33+
# Convert data to PyTorch tensors and move to device
34+
X_train_tensor = torch.tensor(X_train_np, dtype=torch.float32).to(device)
35+
y_train_tensor = torch.tensor(y_train_np, dtype=torch.long).to(device)
36+
X_test_tensor = torch.tensor(X_test_np, dtype=torch.float32).to(device)
37+
y_test_tensor = torch.tensor(y_test_np, dtype=torch.long).to(device)
3438

3539
# Create DataLoader
3640
train_dataset = torch.utils.data.TensorDataset(X_train_tensor, y_train_tensor)
@@ -40,7 +44,7 @@ def train_model(X_train, y_train, X_test, y_test, epochs):
4044
num_classes = len(y_train.unique())
4145

4246
# Initialize the model, loss function, and optimizer
43-
model = Net(X_train.shape[1], num_classes)
47+
model = Net(X_train.shape[1], num_classes).to(device)
4448
criterion = nn.CrossEntropyLoss()
4549
optimizer = optim.Adam(model.parameters(), lr=0.001)
4650

@@ -73,7 +77,7 @@ def train_model(X_train, y_train, X_test, y_test, epochs):
7377
with torch.no_grad():
7478
test_outputs = model(X_test_tensor)
7579
_, test_predicted = torch.max(test_outputs, 1)
76-
test_accuracy = accuracy_score(y_test_tensor, test_predicted) * 100
80+
test_accuracy = accuracy_score(y_test_tensor.cpu(), test_predicted.cpu()) * 100
7781
test_accuracies.append(test_accuracy)
7882

7983
print(f'Epoch {epoch+1}/{epochs}, Loss: {loss.item()}, Training Accuracy: {train_accuracy:.2f}%, Test Accuracy: {test_accuracy:.2f}%')
@@ -87,6 +91,7 @@ def train_model(X_train, y_train, X_test, y_test, epochs):
8791
plt.title('Training and Test Accuracy over Epochs')
8892
plt.legend()
8993
plt.grid(True)
94+
plt.savefig('nids/training_test_accuracy.png') # Save the plot as a PNG file
9095
plt.show()
9196

9297
return model, num_classes

nids/model_metadata.pkl

49 Bytes
Binary file not shown.

nids/scaler.pkl

3.85 KB
Binary file not shown.

nids/training_test_accuracy.png

50.6 KB
Loading

0 commit comments

Comments
 (0)