Skip to main content
24 votes
Accepted

Intersection over Union for rotated rectangles

1. Review The large bounty suggests that getting a review of this code is important. But there are some other things you could have done to make reviewing this code more productive: Give us some more ...
Gareth Rees's user avatar
  • 50.1k
9 votes

Intersection over Union for rotated rectangles

There are two main reasons why your program is slow and using huge amounts of memory: You are using a 768×768 pixel image for each overlap calculation You are checking each anchor box against each red ...
G. Sliepen's user avatar
  • 69.5k
7 votes

A .py utility file for neural network learing rate policies

Overall I rather like this. It's a nice way to think about "configurable functions". Minor stuff: Due to order of operations, the parens around ...
Reinderien's user avatar
  • 71.2k
5 votes
Accepted

Class for validating code in different Conda environments with PyTorch

version hardcodes To dry up run_test_old_and_new_code(), it looks like we want a helper that accepts a version number such as ...
J_H's user avatar
  • 43.3k
5 votes
Accepted

Computing inverse of modified matrix using Sherman-Morrison formula

I'll be disregarding torch, as this doesn't have a lot to do with machine learning specifically. Let's instead develop a demonstration in more generic numerical libraries, Numpy and Scipy. Most of the ...
Reinderien's user avatar
  • 71.2k
5 votes
Accepted

PyTorch Unit-testing in Python

Since you are asking about PyTorch's capabilities you are not taking advantage of, you might want to use: torch.linspace(-10,0,300) instead of ...
Hlib Babii's user avatar
4 votes
Accepted

PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index

I think a different approach is needed to achieve a better performance. The current approach recomputes the Jaccard similarity from scratch for each possible threshold value. However, going from one ...
GZ0's user avatar
  • 2,361
4 votes
Accepted

Loops in PyTorch Implementation

I don't have many improvements to offer-- just one major one. Like you suspected, your implementation is not efficient. This is because using a double for loop to set a Torch/NumPy array is not the ...
Zchpyvr's user avatar
  • 376
4 votes

Pytorch code running slow for Deep Q learning (Reinforcement Learning)

What I can do for you and give you some general suggestions: Use library like Nuba or similar; try Pypy is a JIT compiler; if is possible use C or C++ modules. and here the code with some ...
AsrtoMichi's user avatar
4 votes

Managing a global backend/device state when making a deep learning framework

extra enum ...
J_H's user avatar
  • 43.3k
3 votes

Transformer based on pytorch nn.Transformer to predict protein secondary structure

Here are some minor coding style suggestions. Documentation The PEP 8 style guide recommends adding docstrings for classes and functions. Simpler The following: ...
toolic's user avatar
  • 16.4k
3 votes
Accepted

String art program in Python using PyTorch

Strategies to speed up the code: Instead of computing all the lines in one large array, then picking one, you should compute one line at the time, and keep only the best one iteratively. Loops in ...
Cris Luengo's user avatar
  • 7,041
3 votes

Kaggle Notebook for Identifying House Plants

module docstring First, in any language, avoid repeated "----" or "====" ASCII art in comments. Prefer to use whatever tools the language offers in order to better organize your ...
J_H's user avatar
  • 43.3k
3 votes
Accepted

Kaggle Notebook for Identifying House Plants

Overview The code layout and organization is good, the function docstrings are helpful and you used meaningful names for functions and variables. Comments It is good that you added comments at the top ...
toolic's user avatar
  • 16.4k
3 votes
Accepted

Randomly rotate an image through right angles

types are for documentation tried to include types You did great. Please understand that type hint annotations are entirely optional, though the Gentle Reader will love them. The cPython interpreter ...
J_H's user avatar
  • 43.3k
3 votes
Accepted

increase efficiency of loops and element-wise operations in PyTorch implementation

A few hints for improving efficiency: When W[i, j] == 0, B equals to W so ...
GZ0's user avatar
  • 2,361
2 votes

PyTorch Vectorized Implementation for Thresholding and Computing Jaccard Index

Thanks to the answer of @GZ0, the performance of this code snippet is now around 0.0344s on a GPU and around 0.2511s on a CPU. The implementation of @GZ0's algorithm is attached. Please do not ...
yiping's user avatar
  • 73
2 votes

Iterating over symmetric matrix

It's not clear whether you need to iterate over these values just once, or in several different places in the code. If the latter, then you probably want to encapsulate the logic in a reusable ...
Toby Speight's user avatar
  • 88.7k
2 votes

Loss function in python is a bit cumbersome

It looks like you need to calculate the same thing over and over again. Instead of doing this separately for the positive, the negative and all labels (not sure that is the correct word), you could do ...
Graipher's user avatar
  • 41.7k
2 votes

resnet with pytorch

import torchvision.transforms as transforms To make your code easier to read, use from imports: ...
Gaslight Deceive Subvert's user avatar
2 votes

Neural network that determines the gender of a word

Efficiency In the main function while loop, the 3 separate if statements should be combined ...
toolic's user avatar
  • 16.4k
2 votes
Accepted

One-layer linear neural network to solve a regression problem in PyTorch

Should I import torch and its submodules in every file? Is it good practice to import a library so many times? Yes. Yes. If you need it, import it. Don't worry, ...
J_H's user avatar
  • 43.3k
2 votes

I have a pytorch module that takes in some parameters and predicts the difference between one of it inputs and the target

computing a discarded result Please don't write code like this: def greet(name): 42 name + " is cool." print(f"Hello {name}!") Yes, ...
J_H's user avatar
  • 43.3k
2 votes
Accepted

Video Frame-by-Frame Deraining with MFDNet

I don't really see the need to use numpy as a buffer between ffmpeg and torch as ...
301_Moved_Permanently's user avatar
2 votes

Class for validating code in different Conda environments with PyTorch

The previous answer was thorough, but here are some additional suggestions. DRY Your LOGGER info and ...
toolic's user avatar
  • 16.4k
1 vote
Accepted

Follow up - Deep Learning Project for House Plant Identification on Kaggle Notebook

In function train_step: for images, labels in dataloader: images, labels = images.to(device), labels.to(device) it seems ...
toolic's user avatar
  • 16.4k
1 vote

Video Frame-by-Frame Deraining with MFDNet

Documentation It is great that you have a docstring describing the function. It would be good to add a description of the model_restoration argument to the doctring,...
toolic's user avatar
  • 16.4k
1 vote

Neural network that determines the gender of a word

These three tests are supposed to be exhaustive: ...
Toby Speight's user avatar
  • 88.7k
1 vote
Accepted

Set min value of each row of a tensor to zero without using explicit loops

Not much of a code review, but this should work: def zero_min(x): y = x.clone() y[torch.arange(x.shape[0]), torch.argmin(x, dim=1)] = 0 return y In ...
Eman Yalpsid's user avatar
  • 1,569
1 vote

Iterating over symmetric matrix

I have a symmetric matrix, and I want to loop over its elements Well... no. You only want to loop over its lower triangle (equivalent to its upper triangle), and there are more direct ways: ...
Reinderien's user avatar
  • 71.2k

Only top scored, non community-wiki answers of a minimum length are eligible