Repository navigation
Expand file tree
/
Copy pathRes_net.py
More file actions
212 lines (181 loc) · 7.31 KB
/
Copy pathRes_net.py
File metadata and controls
212 lines (181 loc) · 7.31 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
211
212
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):
pressure = np.array(self.data['load_apply'])
pressure = pressure.T # shape: (1000, 20)
return torch.tensor(pressure, dtype=torch.float32)
def get_label(self):
label = np.array(self.data['result'])
label = label.reshape(-1, 1) # shape: (1000, 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):
self.mean = data.mean(dim=0, keepdim=True)
self.std = data.std(dim=0, keepdim=True)
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
# Residual Block
class ResidualBlock(nn.Module):
def __init__(self, dim):
"""
A simple residual block with two fully connected layers.
Both layers keep the same dimension so that a skip connection can be added.
"""
super(ResidualBlock, self).__init__()
self.fc1 = nn.Linear(dim, dim)
self.relu = nn.ReLU()
self.fc2 = nn.Linear(dim, dim)
def forward(self, x):
identity = x
out = self.fc1(x)
out = self.relu(out)
out = self.fc2(out)
out += identity # Skip connection
out = self.relu(out)
return out
# ResNet Based Neural Network Model
class ResNetCertNet(nn.Module):
def __init__(self, input_dim, hidden_dim, num_blocks, output_dim):
super(ResNetCertNet, self).__init__()
# Initial layer to map input to feature space
self.initial = nn.Sequential(
nn.Linear(input_dim, hidden_dim),
nn.ReLU()
)
# Stack of residual blocks
self.res_blocks = nn.Sequential(
*[ResidualBlock(hidden_dim) for _ in range(num_blocks)]
)
# Output layer
self.output_layer = nn.Linear(hidden_dim, output_dim)
def forward(self, x):
out = self.initial(x)
out = self.res_blocks(out)
out = self.output_layer(out)
return out
# 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 using the ResNet based architecture
input_dim = train_pressure_norm.size(1) # 20 features
hidden_dim = 64 # number of neurons in hidden layers
num_blocks = 2 # number of residual blocks
output_dim = 1 # For binary classification
net = ResNetCertNet(input_dim, hidden_dim, num_blocks, 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()
# Starting with a learning rate that you can tune (e.g., 1e-2 here)
learning_rate = 1e-3
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 = 300
loss_train_list = []
loss_val_list = []
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()
if epoch % 10 == 0:
print(f"Epoch: {epoch}, Train Loss: {train_loss:.6f}, Val Loss: {val_loss:.6f}")
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))
# 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 Loss vs Epochs (ResNet based model)')
plt.legend()
plt.grid(True)
plt.tight_layout()
plt.show()
# Testing Accuracy
net.eval()
with torch.no_grad():
outputs = net(test_pressure_norm)
probs = torch.sigmoid(outputs)
predictions = (probs >= 0.5).float()
correct = (predictions == test_labels).sum()
accuracy = correct / float(test_labels.size(0))
print("Test Accuracy: {:.4f}".format(accuracy))