Optimize train_CSL_graph_classification.py #84
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In both the training and evaluation loops, there are unnecessary calls to
loss.detach().item()
. You can calculate the loss without detaching it and only detach it if necessary at a later stage. In the train_epoch_dense function, you can remove the manual batch handling using(iter % batch_size)
and instead rely on thebatch_size
parameter of thedata_loader
. The DataLoader automatically handles the batch iteration for you. In both thetrain_epoch_sparse
andtrain_epoch_dense
functions, you can move theoptimizer.zero_grad()
call outside the loop, just before the loop starts. This will avoid unnecessary repeated calls tozero_grad()
.