File size: 5,893 Bytes
e048d40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pytorch_lightning as pl
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import DataLoader
import pandas as pd
from tqdm import tqdm
import pickle
import torch
import esm
import numpy as np
import matplotlib.pyplot as plt
import random
import io

from transformers import EsmModel, EsmTokenizer, EsmConfig, AutoTokenizer
from sklearn.metrics import roc_auc_score

#one-hot MLP model (input 1280 (esm-2))
class ProteinMLPOneHot(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(20, 8),
            nn.ReLU(),
            nn.LayerNorm(8),
            nn.Dropout(0.2),
            nn.Linear(8, 4),
            nn.ReLU(),
            nn.LayerNorm(4),
            nn.Dropout(0.2),
            nn.Linear(4, 1)
        )

    def forward(self, x):
        x = self.network(x)
        return x  #pass x through linear layers with activation functions, dropout, and layernorm

    def training_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()  #get batch
        y_hat = self(x).squeeze(-1)  #get prediction from batch
        loss = F.mse_loss(y_hat, y)  #calc loss from prediction and dimension of each
        self.log('train_loss', loss, on_epoch=True, prog_bar=True, logger=True)
        return loss

    def validation_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()
        y_hat = self(x).squeeze(-1)
        val_loss = F.mse_loss(y_hat, y)
        self.log('val_loss', val_loss, on_epoch=True, prog_bar=True, logger=True)
        return val_loss

    def test_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()
        y_hat = self(x).squeeze(-1)
        test_loss = F.mse_loss(y_hat, y)
        self.log('test_loss', test_loss, on_epoch=True, prog_bar=True, logger=True)
        return test_loss

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=0.0003)
        return optimizer

    # def on_train_epoch_end(self):
    #     train_loss = self.trainer.callback_metrics['train_loss']
    #     print(f"Epoch {self.current_epoch + 1} - Training Loss: {train_loss:.4f}")
    #     wandb.log({'train_loss': train_loss, 'epoch': self.current_epoch + 1})

    # def on_validation_epoch_end(self):
    #     val_loss = self.trainer.callback_metrics['val_loss']
    #     print(f"Epoch {self.current_epoch + 1} - Validation Loss: {val_loss:.4f}")
    #     wandb.log({'val_loss': val_loss, 'epoch': self.current_epoch + 1})

    # def on_test_epoch_end(self):
    #     test_loss = self.trainer.callback_metrics['test_loss']
    #     print(f"Test Loss: {test_loss:.4f}")
    #     wandb.log({'test_loss': test_loss})

#regular MLP model (input 1280 (esm-2))
class ProteinMLPESM(pl.LightningModule):
    def __init__(self):
        super().__init__()
        self.network = nn.Sequential(
            nn.Linear(1280, 640),
            nn.ReLU(),
            nn.LayerNorm(640),
            nn.Dropout(0.2),
            nn.Linear(640, 320),
            nn.ReLU(),
            nn.LayerNorm(320),
            nn.Dropout(0.2),
            nn.Linear(320, 1)
        )

    def forward(self, x):
        x = self.network(x)
        return x  #pass x through linear layers with activation functions, dropout, and layernorm

    def training_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()  #get batch
        y_hat = self(x).squeeze(-1)  #get prediction from batch
        loss = F.mse_loss(y_hat, y)  #calc loss from prediction and dimension of each
        self.log('train_loss', loss, on_epoch=True, prog_bar=True, logger=True)
        return loss

    def validation_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()
        y_hat = self(x).squeeze(-1)
        val_loss = F.mse_loss(y_hat, y)
        self.log('val_loss', val_loss, on_epoch=True, prog_bar=True, logger=True)
        return val_loss

    def test_step(self, batch, batch_idx):
        x, y = batch['Protein Input'], batch['Dimension'].float()
        y_hat = self(x).squeeze(-1)
        test_loss = F.mse_loss(y_hat, y)
        self.log('test_loss', test_loss, on_epoch=True, prog_bar=True, logger=True)
        return test_loss

    def configure_optimizers(self):
        optimizer = torch.optim.Adam(self.parameters(), lr=0.0003)
        return optimizer

    # def on_train_epoch_end(self):
    #     train_loss = self.trainer.callback_metrics['train_loss']
    #     print(f"Epoch {self.current_epoch + 1} - Training Loss: {train_loss:.4f}")
    #     wandb.log({'train_loss': train_loss, 'epoch': self.current_epoch + 1})

    # def on_validation_epoch_end(self):
    #     val_loss = self.trainer.callback_metrics['val_loss']
    #     print(f"Epoch {self.current_epoch + 1} - Validation Loss: {val_loss:.4f}")
    #     wandb.log({'val_loss': val_loss, 'epoch': self.current_epoch + 1})

    # def on_test_epoch_end(self):
    #     test_loss = self.trainer.callback_metrics['test_loss']
    #     print(f"Test Loss: {test_loss:.4f}")
    #     wandb.log({'test_loss': test_loss})
    
    
class LossTrackerCallback(pl.Callback):
    def __init__(self):
        self.train_losses = []
        self.val_losses = []

    def on_train_epoch_end(self, trainer, pl_module):
        # Access the most recent training loss from the logger
        train_loss = trainer.callback_metrics.get('train_loss')
        if train_loss:
            self.train_losses.append(train_loss.item())

    def on_validation_epoch_end(self, trainer, pl_module):
        # Access the most recent validation loss from the logger
        val_loss = trainer.callback_metrics.get('val_loss')
        if val_loss:
            self.val_losses.append(val_loss.item())