@@ -102,8 +102,11 @@ def train_model(model, criterion, optimizer, scheduler, metrics, datamodule, num
102102 preds = torch .softmax (preds , dim = 1 )
103103 else :
104104 labels = labels .float ()
105-
106- loss = criterion (preds , labels )
105+ if not isinstance (criterion , nn .CosineEmbeddingLoss ):
106+ loss = criterion (preds , labels )
107+ else :
108+ target = torch .ones (preds .shape [0 ]).cuda ()
109+ loss = criterion (preds , labels , target )
107110 optimizer .zero_grad ()
108111 loss .backward ()
109112 optimizer .step ()
@@ -121,12 +124,11 @@ def train_model(model, criterion, optimizer, scheduler, metrics, datamodule, num
121124 preds = torch .stack ([model (glycan ) for glycan in batch ["IUPAC" ]])
122125 labels = batch .y .squeeze ().cuda () if hasattr (batch , "y" ) else batch .y_oh .cuda ()
123126
124- if isinstance (criterion , nn .CrossEntropyLoss ):
125- preds = torch . softmax (preds , dim = 1 )
127+ if not isinstance (criterion , nn .CosineEmbeddingLoss ):
128+ loss = criterion (preds , labels )
126129 else :
127- labels = labels .float ()
128-
129- loss = criterion (preds , labels )
130+ target = torch .ones (preds .shape [0 ]).cuda ()
131+ loss = criterion (preds , labels , target )
130132 val_losses .append (loss .item ())
131133 val_metrics .update (preds .detach ().cpu (), labels .detach ().cpu ().long ())
132134 val_losses .append (np .mean (val_losses ))
@@ -172,14 +174,16 @@ def main(base: Path, task: str):
172174 config = {"name" : "Taxonomy_Kingdom" , "task" : "classification" , "num_classes" : 13 }
173175 metrics = get_metrics ("multilabel" , n_outputs = 13 )
174176 elif task == "spectrum" :
175- config = {"name" : "Spectrum" , "task" : "regression " , "num_classes" : 2048 }
176- metrics = get_metrics ("regression " , n_outputs = 2048 )
177+ config = {"name" : "Spectrum" , "task" : "spectrum " , "num_classes" : 2048 }
178+ metrics = get_metrics ("spectrum " , n_outputs = 2048 )
177179 else :
178180 raise ValueError (f"Unknown task { task } " )
179181
180- data_config = get_dataset (config , "/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data_new_256" )
182+ data_config = get_dataset (config , "/scratch/chair_kalinina/s8rojoer/GIFFLAR/data_new_256" )
183+ # data_config = get_dataset(config, "/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data_new_256")
181184 datamodule = DownstreamGDM (
182- root = "/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data_new_256" ,
185+ root = "/scratch/chair_kalinina/s8rojoer/GIFFLAR/data_new_256" ,
186+ # root="/scratch/SCRATCH_SAS/roman/Gothenburg/GIFFLAR/data_new_256",
183187 filename = data_config ["filepath" ],
184188 hash_code = "e2301aa9" ,
185189 batch_size = 64 ,
@@ -195,18 +199,23 @@ def main(base: Path, task: str):
195199 model = RNN (input_size = len (libr ) + 1 , hidden_size = 256 , num_classes = data_config ["num_classes" ])
196200 print ("SweetTalk has" , sum (p .numel () for p in model .parameters () if p .requires_grad ), "trainable parameters" )
197201 model .cuda ()
198- criterion = nn .CrossEntropyLoss () if task == "glycosylation" else nn .BCEWithLogitsLoss ()
202+ if task == "glycosylation" :
203+ criterion = nn .CrossEntropyLoss ()
204+ elif task == "spectrum" :
205+ criterion = nn .CosineEmbeddingLoss ()
206+ else :
207+ criterion = nn .BCEWithLogitsLoss ()
199208 optimizer = optim .Adam (model .parameters (), lr = 0.001 )
200209 scheduler = optim .lr_scheduler .StepLR (optimizer , step_size = 20 , gamma = 0.5 )
201210 model , train_metrics , val_metrics = train_model (
202- model ,
203- criterion ,
204- optimizer ,
205- scheduler ,
211+ model ,
212+ criterion ,
213+ optimizer ,
214+ scheduler ,
206215 metrics ,
207216 datamodule ,
208- num_epochs = 2 ,
209- padding = False
217+ num_epochs = 50 ,
218+ padding = False ,
210219 )
211220 torch .save (model .state_dict (), version / "model.pth" )
212221 pd .concat ([pd .DataFrame (train_metrics ), pd .DataFrame (val_metrics )], axis = 1 ).to_csv (version / "metrics.csv" , index = False )
0 commit comments