-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_network.py
180 lines (150 loc) · 6.05 KB
/
neural_network.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import os
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader, TensorDataset
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, LabelEncoder
from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.decomposition import PCA
#load the preprocessed training and testing data
X_train = pd.read_csv("X_train_preprocessed.csv").values
X_test = pd.read_csv("X_test_preprocessed.csv").values
y_train = pd.read_csv("y_train_preprocessed.csv").values.ravel() #convert to 1D array
y_test = pd.read_csv("y_test_preprocessed.csv").values.ravel() #convert to 1D array
#preprocess labels(encode)
label_encoder = LabelEncoder()
y_train_encoded = label_encoder.fit_transform(y_train)
y_test_encoded = label_encoder.transform(y_test)
#feature Scaling
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
#reshape data for CNN (CNN expects a 3D input:[batch_size, channels, features])
X_train_tensor = torch.tensor(X_train_scaled.reshape(-1, 1, X_train_scaled.shape[1]), dtype=torch.float32)
y_train_tensor = torch.tensor(y_train_encoded, dtype=torch.long)
X_test_tensor = torch.tensor(X_test_scaled.reshape(-1, 1, X_test_scaled.shape[1]), dtype=torch.float32)
y_test_tensor = torch.tensor(y_test_encoded, dtype=torch.long)
#create PyTorch datasets and dataloaders
train_dataset = TensorDataset(X_train_tensor, y_train_tensor)
test_dataset = TensorDataset(X_test_tensor, y_test_tensor)
train_loader = DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=32, shuffle=False)
#build the CNN Model
class CNN(nn.Module):
def __init__(self, num_classes):
super(CNN, self).__init__()
self.conv1 = nn.Conv1d(1, 32, kernel_size=3, padding=1)
self.bn1 = nn.BatchNorm1d(32)
self.relu1 = nn.ReLU()
self.pool1 = nn.MaxPool1d(kernel_size=2)
self.conv2 = nn.Conv1d(32, 64, kernel_size=3, padding=1)
self.bn2 = nn.BatchNorm1d(64)
self.relu2 = nn.ReLU()
self.pool2 = nn.MaxPool1d(kernel_size=2)
self.conv3 = nn.Conv1d(64, 128, kernel_size=3, padding=1)
self.bn3 = nn.BatchNorm1d(128)
self.relu3 = nn.ReLU()
self.pool3 = nn.MaxPool1d(kernel_size=2)
self.fc1 = nn.Linear(128 * (X_train_tensor.size(2) // 8), 128)
self.dropout = nn.Dropout(0.5)
self.fc2 = nn.Linear(128, num_classes)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.bn2(x)
x = self.relu2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.bn3(x)
x = self.relu3(x)
x = self.pool3(x)
x = x.view(x.size(0), -1) #flatten the output for the fully connected layer
x = self.fc1(x)
x = self.dropout(x)
x = self.fc2(x)
return x
num_classes = len(np.unique(y_train_encoded))
model = CNN(num_classes)
#define Loss and Optimizer
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
#train the Model
num_epochs = 50
train_losses = []
val_losses = []
for epoch in range(num_epochs):
model.train()
running_loss = 0.0
for i, (inputs, labels) in enumerate(train_loader):
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
train_loss = running_loss / len(train_loader)
train_losses.append(train_loss)
#validation Loss
model.eval()
val_loss = 0.0
with torch.no_grad():
for inputs, labels in test_loader:
outputs = model(inputs)
loss = criterion(outputs, labels)
val_loss += loss.item()
val_loss = val_loss / len(test_loader)
val_losses.append(val_loss)
print(f'Epoch [{epoch+1}/{num_epochs}], Train Loss: {train_loss:.4f}, Val Loss: {val_loss:.4f}')
#evaluate the Model
model.eval()
y_pred_list = []
with torch.no_grad():
for inputs, _ in test_loader:
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
y_pred_list.append(preds)
y_pred = torch.cat(y_pred_list).cpu().numpy()
accuracy = accuracy_score(y_test_encoded, y_pred)
print(f"Accuracy: {accuracy:.2f}")
print("\nClassification Report:")
print(classification_report(y_test_encoded, y_pred, target_names=label_encoder.classes_))
#create a directory for saving visualizations
visualization_dir = "visualizations"
os.makedirs(visualization_dir, exist_ok=True)
#visualize Confusion Matrix
conf_matrix = confusion_matrix(y_test_encoded, y_pred)
plt.figure(figsize=(10, 8))
sns.heatmap(conf_matrix, annot=True, fmt='d', cmap='Blues', xticklabels=label_encoder.classes_, yticklabels=label_encoder.classes_)
plt.title("Confusion Matrix (CNN - PyTorch)")
plt.xlabel("Predicted Label")
plt.ylabel("True Label")
plt.savefig(os.path.join(visualization_dir, "pytorch_cnn_confusion_matrix.png"))
plt.close()
#PCA Visualization
pca = PCA(n_components=2)
X_test_pca = pca.fit_transform(X_test_scaled)
plt.figure(figsize=(10, 6))
sns.scatterplot(x=X_test_pca[:, 0], y=X_test_pca[:, 1], hue=y_test_encoded, palette='Set1')
plt.title("PCA of Test Set (CNN - PyTorch)")
plt.xlabel("PCA Component 1")
plt.ylabel("PCA Component 2")
plt.savefig(os.path.join(visualization_dir, "pytorch_cnn_pca_visualization.png"))
plt.close() #close the figure to avoid displaying it
#plot Training History
plt.figure(figsize=(10, 6))
plt.plot(train_losses, label='Train Loss')
plt.plot(val_losses, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.savefig(os.path.join(visualization_dir, "pytorch_cnn_training_loss.png"))
plt.close()
print("CNN model development and evaluation complete with PyTorch. Visualizations saved in the 'visualizations' folder.")