tensor

Optimization of pytorch function to eliminate for loop

Optimization of pytorch function to eliminate for loop Question: lately I have been developing a function capable of dealing with tensors with dimension: torch.Size([51, 265, 23, 23]) where the first dim is time, the second is pattern and the last 2 are pattern size. Each individual pattern can have a maximum of 3 states: [-1,0,1], …

Total answers: 1

TensorDataSet "size mismatch between tensors"

TensorDataSet "size mismatch between tensors" Question: I have 4 matrices of size 64×64 that were stacked (Torch.Stack) to create a size of [4,64,64] and are meant to be the inputs for my TensorDataSet. I have 1 matrix of 64×64 that is meant to be output for my TensorDataSet. When I load these into the TensorDataSet(inputs,outputs), …

Total answers: 1

How to replace this naive code with scaled_dot_product_attention() in Pytorch?

How to replace this naive code with scaled_dot_product_attention() in Pytorch? Question: Consider a code fragment from Crossformer: def forward(self, queries, keys, values): B, L, H, E = queries.shape _, S, _, D = values.shape scale = self.scale or 1./sqrt(E) scores = torch.einsum("blhe,bshe->bhls", queries, keys) A = self.dropout(torch.softmax(scale * scores, dim=-1)) V = torch.einsum("bhls,bshd->blhd", A, values) …

Total answers: 1

Pytorch – mat1 and mat2 shapes cannot be multiplied (3328×13 and 9216×4096)

Pytorch – mat1 and mat2 shapes cannot be multiplied (3328×13 and 9216×4096) Question: I’m a beginner to pytorch and I’m trying to extract activations from 7 layers for the pre-trained alexnet model. Getting this error when trying to extract activations from the fc6 layer: image of the error Here is my code for looping over …

Total answers: 1

Understanding Pytorch Tensor Slicing

Understanding Pytorch Tensor Slicing Question: Let a and b be two PyTorch tensors with a.shape=[A,3] and b.shape=[B,3]. Further b is of type long. Then I know there are several ways slicing a. For example, c = a[N1:N2:jump,[0,2]] # N1<N2<A would return c.shape = [2,2] for N1=1 and N2=4 and jump=2. But the below should have …

Total answers: 2

Pytorch: How to reorder the tensor by given sorted indices

Pytorch: How to reorder the tensor by given sorted indices Question: Given a tensor A shape (d0, d1, …, dn, dn+1) and a tensor of sorted indices I with shape (d0, d1, …, dn) I want to reorder the indices of A using the sorted indices in I. The first n dimensions of tensors A …

Total answers: 1

Constrain elements in a PyTorch tensor to be equal

Constrain elements in a PyTorch tensor to be equal Question: I have a PyTorch tensor and would like to impose equality constraints on its elements while optimizing. An example tensor of 2 * 9 is shown below, where the same color indicates the elements should always be equal. Let’s make a minimal example of 1 …

Total answers: 2

ImportError: cannot import name 'OrderedDict' from 'typing'

ImportError: cannot import name 'OrderedDict' from 'typing' Question: C:Usersjpala.condaenvstfpython.exe C:UsersjpalaDocumentsMLtrain.py Traceback (most recent call last): File "C:UsersjpalaDocumentsMLtrain.py", line 5, in <module> import tensorflow as tf File "C:Usersjpala.condaenvstflibsite-packagestensorflow__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "C:Usersjpala.condaenvstflibsite-packagestensorflowpython__init__.py", line 42, in <module> from tensorflow.python import data File "C:Usersjpala.condaenvstflibsite-packagestensorflowpythondata__init__.py", line 21, in <module> from tensorflow.python.data …

Total answers: 1

Slice 1D tensor in PyTorch with tensors of start and end indexes

Slice 1D tensor in PyTorch with tensors of start and end indexes Question: I am trying to create a 2D tensor of even slices from a 1D tensor in PyTorch. Say we have a 1D data tensor and tensors of indexes as: >>> data = torch.arange(10) >>> data tensor([0, 1, 2, 3, 4, 5, 6, …

Total answers: 2