Hot answers tagged

2 votes
Accepted

Is there a way to install pytorch on python 3.12.0?

Its possible now if you use the nightly version. Instructions here https://pytorch.org/, but pasted here for completeness pip3 install --pre torch torchvision torchaudio --index-url https://download....
Rick Deckard's user avatar
2 votes

Cant disable beta transforms warnings in PyTorch

It looks like to disable v2 warning you need to call disable_beta_transforms_warning() first then import the v2 transform. For example, this code won't disable the warning: from torchvision.transforms ...
Eleazer Audwin's user avatar
2 votes

Is there a way to save the pytorch model for environment without pytorch package?

You can export the trained model to ONNX and run inference without pytorch
Karl's user avatar
  • 1,400
2 votes
Accepted

How can I implement a torch.linalg.matmul operation in the forward function for batch sizes larger than 1?

I think index_select does what you need. Consider this, based on your example: class NN(nn.Module): def __init__(self, dim_x: int, max_k: int, *args, **kwargs): super().__init__(*args, **...
Yakov Dan's user avatar
  • 2,742
1 vote

Torch Tensor flatten, pad and unflatten again

This would do it: import torch from torch.nn.functional import pad a = torch.tensor([[1, 2, 3], [4, 5, 6]]) desired_shape = torch.tensor([2, 5]) padded = pad(a.flatten(), (0, ...
Nicolas Gervais's user avatar
1 vote

NotImplementedError: Module [ModuleList] is missing the required "forward" function

A ModuleList is just a list that tracks pytorch objects/parameters. You can't call it because it has no forward method. I'm not sure why you are putting a single pytorch module inside a ModuleList - ...
Karl's user avatar
  • 1,400
1 vote

Both PyTorch model and tensor on GPU but getting all tensors not on same device error

In CNN_MLP the fc_layers member needs to be an nn.Module because it contains other modules. When you call .to on your model, .to will be called recursively on all the other members that inhereit nn....
jodag's user avatar
  • 20.6k
1 vote

using "in" operator with set and list yields different results

Part of the explanation is pytorch specific. That's perhaps the less interesting part, so I put that in the end. The first part of the explanation is more broadly about how the in operator works in ...
Niko Fohr's user avatar
  • 29.3k
1 vote
Accepted

Using Multiple GPUs in PyTorch (Model Parallelization)

The most popular way of parallelizing computation across multiple GPUs is data parallelism (DP), where the model is copied across devices and the batch is split so that each part runs on a different ...
Valentin Goldité's user avatar
1 vote

Pytorch, When I use backward() in the loop, the message failed on the second loop

PyTorch prompts the need to retrace the graph during the backward pass because the lagrange_multiplier variable depends on all previous iterations of the for loop. If lagrange_multiplier were ...
Domenico's user avatar
  • 138
1 vote
Accepted

How can I read hdf5 files stored as 1-D array. and view them as images?

First, the f['datasets']['car'] object is a DATASET, not a GROUP. Second, based this output, I think your dataset is a dataset with rows of variable length arrays (aka "ragged" array). # ...
kcw78's user avatar
  • 7,371
1 vote

How can I read hdf5 files stored as 1-D array. and view them as images?

import h5py import numpy as np from PIL import Image # Open the HDF5 file with h5py.File('data/images.hdf5', 'r') as f: # Access the dataset containing images data = f['datasets']['car'] ...
Manoj Bhosle's user avatar
1 vote
Accepted

Transformers: Cross Attention Tensor Shapes During Inference Mode

You've got the order mixed up. For cross attention in an encoder/decoder transformer, the query comes from the decoder, and the key/value come from the decoder. You can check this in the original ...
Karl's user avatar
  • 1,400
1 vote

PyTorch For Loop Optimisations and Speedup techniques

In addition to the optimized version in my other answer, this is a vectorized version for your problem: import torch # sample data with a duplicate at index 0 and 1 t = torch.Tensor([[1, 2, 3], [1, 2,...
Klops's user avatar
  • 1,154
1 vote

PyTorch For Loop Optimisations and Speedup techniques

I could not come up with a vectorized version straigt away, but I fixed a bug and reduced the loop runs by more than 50%. Also, torch.unbind is not needed here, you can iterate an array (or tensor) ...
Klops's user avatar
  • 1,154
1 vote

Can PyTorch GPU Use Shared GPU Memory (from RAM, shows in Windows Task Manage)?

For Windows 10 and 11 and newer operating systems, Microsoft introduced GPU shared memory, which uses 50% of physical memory for uniform addressing by default. For CUDA, if you use Nvidia driver ...
uniartisan's user avatar
1 vote

GPU memory is empty, but CUDA out of memory error occurs

I believe this could be due to memory fragmentation that occurs in certain cases in CUDA when allocating and deallocation of memory. Try torch.cuda.empty_cache() after model training or set ...
Rathod's user avatar
  • 31
1 vote

Best practice to pass PyTorch device name to model

I'm not 100% sure this will apply to your case but you can also use .to(device) after the model has been initialized: device = torch.device("cuda:0" if torch.cuda.is_available() else "...
NickBraunagel's user avatar

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