-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_trans.py
324 lines (265 loc) · 11.4 KB
/
train_trans.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
import matplotlib.pyplot as plt
import seaborn as sns
from rich import print, progress
import math
class PeptideDataset(Dataset):
def __init__(self, features, targets):
"""
features: numpy array of shape (n_samples, n_features)
targets: numpy array of shape (n_samples,)
"""
# Group features by property type
self.basicity = torch.FloatTensor(features[:, :20]) # First 20 features are basicity
self.helicity = torch.FloatTensor(features[:, 20:40]) # Next 20 are helicity
self.hydrophobicity = torch.FloatTensor(features[:, 40:60]) # Next 20 are hydrophobicity
self.pi = torch.FloatTensor(features[:, 60:76]) # Next 16 are pI
self.global_features = torch.FloatTensor(features[:, 76:]) # Last 2 are global features
self.targets = torch.FloatTensor(targets)
def __len__(self):
return len(self.targets)
def __getitem__(self, idx):
return {
'basicity': self.basicity[idx],
'helicity': self.helicity[idx],
'hydrophobicity': self.hydrophobicity[idx],
'pi': self.pi[idx],
'global': self.global_features[idx]
}, self.targets[idx]
class PeptideTransformer(nn.Module):
def __init__(self, d_model=256, nhead=4, num_encoder_layers=4, dropout=0.1):
super().__init__()
# Property embeddings
self.basicity_embedding = nn.Linear(20, d_model)
self.helicity_embedding = nn.Linear(20, d_model)
self.hydrophobicity_embedding = nn.Linear(20, d_model)
self.pi_embedding = nn.Linear(16, d_model)
self.global_embedding = nn.Linear(2, d_model)
# Positional encoding
self.pos_encoder = nn.Parameter(torch.randn(1, 5, d_model)) # 5 different feature types
# Transformer encoder
encoder_layer = nn.TransformerEncoderLayer(
d_model=d_model,
nhead=nhead,
dim_feedforward=d_model * 4,
dropout=dropout,
batch_first=True
)
self.transformer_encoder = nn.TransformerEncoder(encoder_layer, num_encoder_layers)
# Output projection
self.output_projection = nn.Sequential(
nn.Linear(d_model * 5, d_model), # Combine all feature types
nn.ReLU(),
nn.Dropout(dropout),
nn.Linear(d_model, 1)
)
def forward(self, features_dict):
# Create embeddings for each property type
basicity_emb = self.basicity_embedding(features_dict['basicity'])
helicity_emb = self.helicity_embedding(features_dict['helicity'])
hydro_emb = self.hydrophobicity_embedding(features_dict['hydrophobicity'])
pi_emb = self.pi_embedding(features_dict['pi'])
global_emb = self.global_embedding(features_dict['global'])
# Stack all embeddings
# Shape: (batch_size, 5, d_model)
x = torch.stack([
basicity_emb,
helicity_emb,
hydro_emb,
pi_emb,
global_emb
], dim=1)
# Add positional encodings
x = x + self.pos_encoder
# Apply transformer encoder
# Shape: (batch_size, 5, d_model)
transformed = self.transformer_encoder(x)
# Flatten and project to output
# Shape: (batch_size, 5 * d_model)
flattened = transformed.reshape(transformed.size(0), -1)
# Get final prediction
# Shape: (batch_size, 1)
output = self.output_projection(flattened)
return output.squeeze(-1)
def prepare_data(train_encoded, test_encoded, target='y_target', batch_size=32):
"""Prepare data for training"""
feature_cols = [col for col in train_encoded.columns
if col not in ['spectrum_id', 'b_target', 'y_target']]
X_train = train_encoded[feature_cols].values
y_train = train_encoded[target].values
X_test = test_encoded[feature_cols].values
y_test = test_encoded[target].values
# Scale features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
# Create datasets
train_dataset = PeptideDataset(X_train_scaled, y_train)
test_dataset = PeptideDataset(X_test_scaled, y_test)
# Create dataloaders
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
return train_loader, test_loader, scaler
def train_model(model, train_loader, test_loader, criterion, optimizer,
num_epochs=50, device='cuda', early_stopping_patience=5):
"""Train model with early stopping"""
model.to(device)
best_val_loss = float('inf')
patience_counter = 0
training_history = []
for epoch in range(num_epochs):
# Training phase
model.train()
train_loss = 0
train_predictions = []
train_actuals = []
for batch_features, batch_targets in progress.track(train_loader, description=f"Epoch {epoch+1}"):
# Move data to device
batch_features = {k: v.to(device) for k, v in batch_features.items()}
batch_targets = batch_targets.to(device)
# Forward pass
optimizer.zero_grad()
outputs = model(batch_features)
loss = criterion(outputs, batch_targets)
# Backward pass
loss.backward()
optimizer.step()
# Record results
train_loss += loss.item()
train_predictions.extend(outputs.detach().cpu().numpy())
train_actuals.extend(batch_targets.cpu().numpy())
# Validation phase
model.eval()
val_loss = 0
val_predictions = []
val_actuals = []
with torch.no_grad():
for batch_features, batch_targets in test_loader:
# Move data to device
batch_features = {k: v.to(device) for k, v in batch_features.items()}
batch_targets = batch_targets.to(device)
# Forward pass
outputs = model(batch_features)
val_loss += criterion(outputs, batch_targets).item()
# Record results
val_predictions.extend(outputs.cpu().numpy())
val_actuals.extend(batch_targets.cpu().numpy())
# Calculate metrics
train_loss = train_loss / len(train_loader)
val_loss = val_loss / len(test_loader)
train_correlation = np.corrcoef(train_actuals, train_predictions)[0, 1]
val_correlation = np.corrcoef(val_actuals, val_predictions)[0, 1]
# Record history
training_history.append({
'epoch': epoch + 1,
'train_loss': train_loss,
'val_loss': val_loss,
'train_correlation': train_correlation,
'val_correlation': val_correlation
})
# Print progress
print(f'\nEpoch {epoch+1}/{num_epochs}:')
print(f'Training Loss: {train_loss:.4f}, Correlation: {train_correlation:.4f}')
print(f'Validation Loss: {val_loss:.4f}, Correlation: {val_correlation:.4f}')
# Early stopping check
if val_loss < best_val_loss:
best_val_loss = val_loss
torch.save(model.state_dict(), 'best_transformer_model.pth')
patience_counter = 0
else:
patience_counter += 1
if patience_counter >= early_stopping_patience:
print(f'\nEarly stopping triggered after {epoch + 1} epochs')
break
return pd.DataFrame(training_history)
def evaluate_model(model, test_loader, device='cuda'):
"""Evaluate model and return predictions"""
model.eval()
predictions = []
actuals = []
with torch.no_grad():
for batch_features, batch_targets in test_loader:
# Move data to device
batch_features = {k: v.to(device) for k, v in batch_features.items()}
# Get predictions
outputs = model(batch_features)
# Record results
predictions.extend(outputs.cpu().numpy())
actuals.extend(batch_targets.numpy())
return np.array(predictions), np.array(actuals)
def plot_results(history, predictions, actuals):
"""Plot training history and prediction results"""
fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(18, 5))
# Plot losses
ax1.plot(history['epoch'], history['train_loss'], label='Training Loss')
ax1.plot(history['epoch'], history['val_loss'], label='Validation Loss')
ax1.set_xlabel('Epoch')
ax1.set_ylabel('Loss')
ax1.set_title('Training and Validation Loss')
ax1.legend()
# Plot correlations
ax2.plot(history['epoch'], history['train_correlation'], label='Training Correlation')
ax2.plot(history['epoch'], history['val_correlation'], label='Validation Correlation')
ax2.set_xlabel('Epoch')
ax2.set_ylabel('Correlation')
ax2.set_title('Training and Validation Correlation')
ax2.legend()
# Plot predictions vs actuals
ax3.scatter(actuals, predictions, alpha=0.1)
ax3.plot([0, 1], [0, 1], 'r--')
ax3.set_xlabel('Actual Values')
ax3.set_ylabel('Predicted Values')
ax3.set_title(f'Predictions vs Actuals\nr={np.corrcoef(actuals, predictions)[0,1]:.3f}')
plt.tight_layout()
plt.show()
def main():
# Load prepared data
print("Loading data...")
train_encoded = pd.read_feather("fragmentation-nist-humanhcd20160503-parsed-trainval-encoded.feather")
test_encoded = pd.read_feather("fragmentation-nist-humanhcd20160503-parsed-test-encoded.feather")
# Prepare data
print("Preparing data...")
train_loader, test_loader, scaler = prepare_data(
train_encoded, test_encoded, target='y_target', batch_size=32
)
# Initialize model and training parameters
print("Initializing transformer model...")
model = PeptideTransformer(
d_model=256,
nhead=8,
num_encoder_layers=6,
dropout=0.1
)
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.0001)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# Train model
print("Training model...")
history = train_model(
model=model,
train_loader=train_loader,
test_loader=test_loader,
criterion=criterion,
optimizer=optimizer,
num_epochs=50,
device=device,
early_stopping_patience=5
)
# Load best model and evaluate
print("Evaluating model...")
model.load_state_dict(torch.load('best_transformer_model.pth'))
predictions, actuals = evaluate_model(model, test_loader, device)
# Plot results
print("Plotting results...")
plot_results(history, predictions, actuals)
# Print final correlation
correlation = np.corrcoef(actuals, predictions)[0, 1]
print(f'\nFinal Test Set Correlation: {correlation:.4f}')
return predictions, actuals, history
if __name__ == "__main__":
predictions, actuals, history = main()