Repository navigation
Expand file tree
/
Copy pathFCNN.py
More file actions
210 lines (175 loc) · 7.46 KB
/
Copy pathFCNN.py
File metadata and controls
210 lines (175 loc) · 7.46 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import matplotlib.pyplot as plt
import torch
import torch.nn as nn
import torch.utils.data as Data
import numpy as np
import h5py
# Loss Function
# Using BCE with logits loss for classification
class Lossfunc(object):
def __init__(self):
self.criterion = nn.BCEWithLogitsLoss()
def __call__(self, output, target):
return self.criterion(output, target)
# Data Reader
# Reads the Eiffel data for the certification task.
class MatRead(object):
def __init__(self, file_path):
self.file_path = file_path
self.data = h5py.File(self.file_path, 'r')
def get_pressure(self):
# Assume pressure is stored as a 2D array of shape (20, 1000)
pressure = np.array(self.data['load_apply'])
# Transpose to shape (1000, 20): one sample per row
pressure = pressure.T
return torch.tensor(pressure, dtype=torch.float32)
def get_label(self):
# Assume labels are stored as a 1D array of length 1000
label = np.array(self.data['result'])
# Reshape to (1000, 1) to match the output of the network
label = label.reshape(-1, 1)
return torch.tensor(label, dtype=torch.float32)
# Data Normalizer
class DataNormalizer(object):
def __init__(self):
self.mean = None
self.std = None
def fit(self, data):
# Data is 2D: [n_samples, features]
self.mean = data.mean(dim=0, keepdim=True)
self.std = data.std(dim=0, keepdim=True)
# Avoid division by zero
self.std[self.std < 1e-8] = 1.0
def transform(self, data):
return (data - self.mean) / self.std
def inverse_transform(self, data):
return data * self.std + self.mean
# Neural Network Model
# FCNN for classification: input -> [64] -> output (logit)
class Cert_Net(nn.Module):
def __init__(self, input_dim, hidden_dims, output_dim):
super(Cert_Net, self).__init__()
layers = []
# First hidden layer
layers.append(nn.Linear(input_dim, hidden_dims[0]))
layers.append(nn.ReLU())
# Additional hidden layers if specified
for i in range(1, len(hidden_dims)):
layers.append(nn.Linear(hidden_dims[i - 1], hidden_dims[i]))
layers.append(nn.ReLU())
# Output layer (no activation here as BCEWithLogitsLoss applies sigmoid)
layers.append(nn.Linear(hidden_dims[-1], output_dim))
self.net = nn.Sequential(*layers)
def forward(self, x):
return self.net(x)
# Main Script
if __name__ == '__main__':
# Set random seed for reproducibility
torch.manual_seed(42)
# Data Loading: update the path as needed
data_path = 'Problem_2_student/Data/Eiffel_data.mat'
data_reader = MatRead(data_path)
pressure = data_reader.get_pressure() # shape: [1000, 20]
labels = data_reader.get_label() # shape: [1000, 1]
# Split data: 70% training, 15% validation, 15% test
num_samples = pressure.shape[0]
indices = torch.randperm(num_samples, generator=torch.Generator().manual_seed(42))
n_train = int(0.7 * num_samples)
n_val = int(0.15 * num_samples)
n_test = num_samples - n_train - n_val
train_indices = indices[:n_train]
val_indices = indices[n_train:n_train + n_val]
test_indices = indices[n_train + n_val:]
train_pressure = pressure[train_indices]
train_labels = labels[train_indices]
val_pressure = pressure[val_indices]
val_labels = labels[val_indices]
test_pressure = pressure[test_indices]
test_labels = labels[test_indices]
# Normalize the input pressure data (not the labels)
normalizer = DataNormalizer()
normalizer.fit(train_pressure)
train_pressure_norm = normalizer.transform(train_pressure)
val_pressure_norm = normalizer.transform(val_pressure)
test_pressure_norm = normalizer.transform(test_pressure)
# Create DataLoaders
batch_size = 20
train_dataset = Data.TensorDataset(train_pressure_norm, train_labels)
train_loader = Data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
val_dataset = Data.TensorDataset(val_pressure_norm, val_labels)
val_loader = Data.DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
# Define Neural Network: input=20, two hidden layers of 64 neurons each, output=1
input_dim = train_pressure_norm.size(1) # 20 features
hidden_dims = [64, 64] # hidden layers: two layers of 64 neurons
output_dim = 1 # For binary classification
net = Cert_Net(input_dim=input_dim, hidden_dims=hidden_dims, output_dim=output_dim)
n_params = sum(p.numel() for p in net.parameters() if p.requires_grad)
print('Number of parameters: %d' % n_params)
# Define Loss Function and Optimizer
loss_func = Lossfunc()
learning_rate = 1e-1
optimizer = torch.optim.Adam(net.parameters(), lr=learning_rate, weight_decay=1e-5)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=50, gamma=0.5)
# Training Loop
epochs = 210
loss_train_list = []
loss_val_list = []
test_acc_list = [] # List to store test accuracy at each epoch
print("Start training for {} epochs...".format(epochs))
for epoch in range(epochs):
net.train()
train_loss = 0.0
for batch_inputs, batch_targets in train_loader:
optimizer.zero_grad()
outputs = net(batch_inputs)
loss = loss_func(outputs, batch_targets)
loss.backward()
optimizer.step()
train_loss += loss.item()
train_loss /= len(train_loader)
# Validation loss
net.eval()
val_loss = 0.0
with torch.no_grad():
for val_inputs, val_targets in val_loader:
outputs = net(val_inputs)
loss = loss_func(outputs, val_targets)
val_loss += loss.item()
val_loss /= len(val_loader)
scheduler.step()
# Evaluate test accuracy at the current epoch
with torch.no_grad():
outputs_test = net(test_pressure_norm)
probs_test = torch.sigmoid(outputs_test)
predictions_test = (probs_test >= 0.5).float()
correct = (predictions_test == test_labels).sum().item()
test_acc = correct / float(test_labels.size(0))
test_acc_list.append(test_acc)
if epoch % 10 == 0:
print(f"Epoch: {epoch}, Train Loss: {train_loss:.6f}, Val Loss: {val_loss:.6f}, Test Acc: {test_acc:.4f}")
loss_train_list.append(train_loss)
loss_val_list.append(val_loss)
print("Final Train Loss: {:.6f}".format(train_loss))
print("Final Validation Loss: {:.6f}".format(val_loss))
print("Final Test Accuracy: {:.4f}".format(test_acc_list[-1]))
# Plot Train and Validation Loss vs Epochs
plt.figure(figsize=(8, 5))
plt.plot(loss_train_list, label='Train Loss')
plt.plot(loss_val_list, label='Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('BCE Loss')
plt.title('Train and Validation Loss vs Epochs')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Plot Test Accuracy vs Epochs
plt.figure(figsize=(8, 5))
plt.plot(test_acc_list, label='Test Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Test Accuracy')
plt.title('Test Accuracy vs Epochs')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()