If you really enforce a single thread, then it's all up to the BLAS library which does the heavy lifting. Both numpy and pytorch are compatible with the most common libs, so it's a matter of which lib they are linked against respectively. Numpy with MKL will most likely beat pytorch with OpenBLAS for most workloads. Answer from Frankelstner on reddit.com
🌐
Reddit
reddit.com › r/python › i understand machine learning with numpy and pytorch better since i started focusing on the basics
r/Python on Reddit: I Understand Machine Learning with Numpy and PyTorch Better Since I Started Focusing on the Basics
October 12, 2024 -

I've recently started appreciating ML in Python more since I began looking at the concepts from the ground up.

For example, I took a closer look at the basics of classification neural networks, and now I have a better understanding of how more complex networks work. The foundation here is logistic regression, and understanding that has really helped me grasp the overall concepts better. It also helped me implementing the code in Numpy and in PyTorch.

If you're also interested in Machine Learning with Python and sometimes feel overwhelmed by all the complicated topics, I really recommend going back to the basics. I've made a video where I explain logistic regression step by step using a simple example.

The video will be attached here: https://youtu.be/EB4pqThgats?si=Z-lXOjuNKEP5Yehn

I'd be happy if you could take a look and give me some feedback! I'm curious to hear what you think of my approach and if you have any tips on how to make it even clearer.

🌐
Reddit
reddit.com › r/pytorch › ask for help: what is the best way to have code both support torch and numpy?
r/pytorch on Reddit: Ask for help: what is the best way to have code both support torch and numpy?
February 22, 2023 -

I want to implement the code with the same functionality ( by numpy and torch). I don't know how to support both numpy and torch with only once implemention.

For example, I want to implement these:

def fun_torch(a):
    return torch.sin(a) + torch.cos(a)

def fun_np(a):
    return np.sin(a) + np.cos(b)

I want to implement them in only one function, but I dont want this:

def func(a):
    if isintance(a, torch.Tensor):
        return torch.sin(a) + torch.cos(a)
   elif isinatance(a, np.ndarray):
        return np.sin(a) + np.cos(b)        
🌐
Reddit
reddit.com › r/machinelearning › [p] using pytorch + numpy? a bug that plagues thousands of open-source ml projects.
r/MachineLearning on Reddit: [P] Using PyTorch + NumPy? A bug that plagues thousands of open-source ML projects.
April 10, 2021 -

Using NumPy’s random number generator with multi-process data loading in PyTorch causes identical augmentations unless you specifically set seeds using the worker_init_fn option in the DataLoader. I didn’t and this bug silently regressed my model’s accuracy.

How many others has this bug done damage to? Curious, I downloaded over a hundred thousand repositories from GitHub that import PyTorch, and analysed their source code. I kept projects that define a custom dataset, use NumPy’s random number generator with multi-process data loading, and are more-or-less straightforward to analyse using abstract syntax trees. Out of these, over 95% of the repositories are plagued by this problem. It’s inside PyTorch's official tutorial, OpenAI’s code, and NVIDIA’s projects. Even Karpathy admitted falling prey to it.

For example, the following image shows the duplicated random crop augmentations you get when you blindly follow the official PyTorch tutorial on custom datasets:

You can read more details here.

🌐
Reddit
reddit.com › r/machinelearning › [d] pytorch implementation best practices
r/MachineLearning on Reddit: [D] PyTorch implementation best practices
April 12, 2019 -

Hi r/MachineLearning! Let's discuss PyTorch best practices.

I recently finished a PyTorch re-implementation (with help from various sources) for the paper Zero-shot User Intent Detection via Capsule Neural Networks, which originally had Python 2 code for TensorFlow.

I'd like to request perhaps a critique on the code I've written so far (it's not perfect, yet!) and any suggestions if there are best practices specifically in PyTorch, for implementing directly from research papers as well as converting them from other frameworks.

Some thoughts I had while programming (feel free to raise more!):

  1. I've been implementing a Dataset class and custom batch functions for every dataset I've been working with. Is this the PyTorch best practice?

  2. Where is the optimal place to shift Tensors to .cuda()? I've been doing this in the training loop, just before feeding it into the model.

  3. How to manage the use of both numpy and torch, seeing as PyTorch aims to reinvent many of the basic operations in numpy?

If you're a fellow PyTorch user/contributor please share a little!

Top answer
1 of 5
12
I've been implementing a Dataset class and custom batch functions for every dataset I've been working with. Is this the PyTorch best practice? Not sure whether people would consider it best practice, but I do it as well because it's most convenient. Where is the optimal place to shift Tensors to .cuda()? You can do it immediately after loading the data in your training loop. Could be the first line after your for loop over the dataset, except that there is some computation you want to perform on the CPU before you pass it to the model. Btw, since ~1/2 year ago, .cuda() has been deprecated I think. The recommendation is to use .to(torch.device('cuda:0')) for example. How to manage the use of both numpy and torch, seeing as PyTorch aims to reinvent many of the basic operations in numpy? I don't use NumPy anymore when I write PyTorch code because, like you said, you can do most of it in PyTorch. Very rarely, there is something I need to do in NumPy/SciPy (a recent example was the cdf of the beta distribution, which is not implemented in PyTorch, yet). Btw. you have to be careful when using NumPy in your model because its operations are not tracked by autograd.
2 of 5
7
As a new adopter of pytorch I have been running into deadlock scenarios trying to use hdf5 or opencv during the dataloader. I have to reopen the hdf5 file in every batch versus keeping an open instance on instantiation of the dataset class. Using pillow rather than opencv is pretty annoying as well. Other than that so far I love pytorch, it behaves alot like numpy and I'm enjoying learning it.
🌐
PyTorch Forums
discuss.pytorch.org › t › torch-is-slow-compared-to-numpy › 117502
Torch is slow compared to numpy - PyTorch Forums
April 15, 2021 - In this benchmark I implemented the same algorithm in numpy/cupy, pytorch and native cpp/cuda. The benchmark is attached below. In all tests numpy was significantly faster than pytorch.
Find elsewhere
🌐
Kaggle
kaggle.com › code › amirmotefaker › pytorch-vs-numpy
PyTorch vs NumPy
July 14, 2024 - Explore and run AI code with Kaggle Notebooks | Using data from No attached data sources
🌐
Quora
quora.com › What-are-the-benefits-of-PyTorch-over-NumPy
What are the benefits of PyTorch over NumPy? - Quora
Answer: PyTorch is a deep learning focused library while NumPy is for scientific computing. Both of these libraries are made with different goals in mind: * If you want to just do basic matrix operations/transformation and array operations then ...
🌐
Reddit
reddit.com › r/machinelearning › [d] shocking confusing speed / timing results of algorithms (sklearn, numpy, scipy, pytorch, numba) | prelim results | hyperlearn
r/MachineLearning on Reddit: [D] Shocking Confusing Speed / Timing results of Algorithms (Sklearn, Numpy, Scipy, Pytorch, Numba) | Prelim results | HyperLearn
September 1, 2018 -

So you might or might not know, I was working on HyperLearn --> a faster optimized ML package designed to make everything at least 50% (I hope) faster.

Thanks so much for all the support Redditors for HyperLearn! https://github.com/danielhanchen/hyperlearn [Made it to the Trending Github list for Jup Notebooks!! yayy!]

Anyways, I didn't update the code a lot, but that's because I was busily testing and finding out which algos were the most stable and best.

Key findings for N = 5,000 P = 6,000 [more features than N near square matrix]

  1. For pseudoinverse, (used in Linear Reg, Ridge Reg, lots of other algos), JIT, Scipy MKL, PinvH, Pinv2 and HyperLearn's Pinv are very similar. PyTorch's is clearly problematic, having close to over x4 slower than Scipy MKL.

  2. For Eigh (used in PCA, LDA, QDA, other algos), Sklearn's PCA utilises SVD. Clearly, not a good idea, since it is much better to compute the eigenvec / eigenval on XTX. JIT Eigh is the clear winner at 14.5 seconds on XTX, whilst Numpy is 2x slower. Torch likewise is slower once again...

  3. So, for PCA, a speedup of 3 times is seem if using JIT compiled Eigh when compared to Sklearn's PCA

  4. To solve X*theta = y, Torch GELS is super unstable. Like really. If you use Torch GELS, don't forget to call theta_hat[np.isnan(theta_hat) | np.isinf(theta_hat)] = 0, or else results are problematic. All other algos have very similar MSEs, and HyperLearn's Regularized Cholesky Solve takes a mere 0.635 seconds when compared to say using Sklearn's next fastest Ridge Solve (via cholesky) by over 100% (after considering matrix multiplication time) --> HyperLearn 2.89s vs 4.53s Sklearn.

So to conclude:

  1. HyperLearn's Pseudoinverse has no speed improvement

  2. HyperLearn's PCA will have over 2 times speed boost. (200% improvement)

  3. HyperLearn's Linear Solvers will be over 1 times faster. (100% improvement)

Help make HyperLearn better! All contributors are welcome, as this is truly an overwhelming project... https://github.com/danielhanchen/hyperlearn

Lower Time == better
🌐
PyTorch Forums
discuss.pytorch.org › t › pytorch-tensor-constructor-speed-vs-numpy › 191487
Pytorch tensor constructor speed vs numpy - PyTorch Forums
November 8, 2023 - So I was comparing the performance of the tensor constructor to the numpy array constructor For pytorch torch.inference_mode() total_time = 0.0 iterations = 10000 for _ in range(iterations): data = np.random.normal(0, 1, (1000, 10)).tolist() # Use numpy for RNG # but convert back to python ...
🌐
Reddit
reddit.com › r/programming › i don’t like numpy
r/programming on Reddit: I don’t like NumPy
August 31, 2025 - Many uses of numpy have moved over to pytorch. There's tons of investment in it. > I think the strongest complaint is the lack of composibility, that if you write a custom function you can't treat it as a black-box for the purpose of vectorizing it. pytorch doesn't fix this, but there is a large and impressive backend with torch...
🌐
Reddit
reddit.com › r/learnmachinelearning › tensorflow or pytorch?
r/learnmachinelearning on Reddit: tensorflow or pytorch?
December 16, 2025 -

i read the hands on machine learning book (the tensorflow one) and i am a first year student. i came to know a little later that the pytorch one is a better option. is it possible that on completing this book and getting to know about pytorch the skills are transferrable.

sorry if this might sound stupid or obvious but i dont really know

🌐
Medium
medium.com › @yunjiangster › comparing-speed-of-torch-and-numpy-f22b11aabfcf
Comparing Speed of Torch and Numpy | by Yunjiang Jiang | Medium
June 19, 2023 - I knew from experience working with FAISS library that if implemented well, inner product on gpu can be wicked fast. However I wasn’t so sure about torch. As a counter datapoint, I knew that training a 3-layer MLP (multi-layer perceptron, aka feed-forward neural net) on gpu isn’t much faster than on cpu, back in the tensorflow days.
🌐
Medium
medium.com › @ashish.iitr2015 › comparison-between-pytorch-tensor-and-numpy-array-de41e389c213
Comparison between Pytorch Tensor and Numpy Array | by Ashish Singh | Medium
August 11, 2020 - Numpy arrays are mainly used in typical machine learning algorithms (such as k-means or Decision Tree in scikit-learn) whereas pytorch tensors are mainly used in deep learning which requires heavy matrix computation.
🌐
PyTorch Forums
discuss.pytorch.org › t › numerical-differences-between-numpy-and-pytorch › 89607
Numerical differences between numpy and pytorch? - PyTorch Forums
July 17, 2020 - Hello all, When computing mean and std on numpy or pytorch tensors, they yield different results. How come ? rng = np.random.RandomState(1) dataset1 = rng.uniform(low=-0.01, high=0.01, size=(1000, 20)) dataset2 = torch.from_numpy(data_numpy) print(dataset1.mean(), dataset1.std()) ...
🌐
Reddit
reddit.com › r/cpp › c++ desperately needs something like numpy
r/cpp on Reddit: C++ desperately needs something like numpy
September 6, 2023 -

Anybody else agree? At this point, I don’t even care if it doesn’t support expression templates for performance. A library like that allows you to be SO MUCH more productive when doing neural network stuff, computer vision, pre-processing and post-processing data. It takes years to standardise something like mdspan and that’s miles off numpy. We are literally going to have to wait 100 years.