Directly reshaping A will unfortunately not work, as it would result in:
print(A.view(B.size()))
> tensor([[ 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3],
[ 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6],
[ 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 9, 9,… Answer from ptrblck on discuss.pytorch.org
PyTorch Forums
discuss.pytorch.org › t › how-to-properly-reshape-a-3d-tensor-to-a-2d-for-linear-layer-then-back-with-new-3ds-fibers-corresponding-to-the-old-3d › 103229
How to properly reshape a 3D tensor to a 2D for linear layer then back with new 3D's fibers corresponding to the old 3D - PyTorch Forums
November 18, 2020 - I have a 3D tensor of names that comes out of an LSTM that’s (batch size x name length x embedding size) I’ve been reshaping it to a 2D to put it through a linear layer, because linear layer requires (batch size, linear dimension size) by using the following y0 = output.contiguous().view(-1, output.size(-1)) this converts outputs to (batchsize * name length, number of possible characters) then once I put it through the linear layer let’s call the output y0 I do this y = y0.contiguous().view...
Reshape 3D/4D tensors to 2D?
In the original BERT Tensorflow code, they reshape matrices to always be 2D and they reference that it is done to speed up the performance on the TPU. Does this same logic apply when using XLA on T... More on github.com
python - How to convert 3D tensor to 2D tensor in pytorch? - Stack Overflow
I am new to pytorch. I have 3D tensor (32,10,64) and I want a 2D tensor (32, 64). I tried view() and used after passing to linear layer squeeze() which converted it to (32,10). More on stackoverflow.com
Expand a 2d tensor to 3d tensor
Let’s say I have a 2d tensor ... can I do this? I know that a vector can be expanded by using expand_as, but how do I expand a 2d tensor? Moreover, I want to reshape a 3d tensor. So for example, 2 x 3 x 4 tensor to 3 x 2 x 4. How can I do this?... More on discuss.pytorch.org
From 3D datas to 2D slices
Hi there I have faced a problem and then I have read some forums but I couldn`t get the correct answer. I would like to ask again, in Dataset part I would like to read my 3D data which has first 2 dimension same shape ([512,512,600] or [512,512,550]) and I would like to create like 600 slices ... More on discuss.pytorch.org
GitHub
github.com › pytorch › xla › issues › 2958
Reshape 3D/4D tensors to 2D? · Issue #2958 · pytorch/xla
May 19, 2021 - def reshape_to_matrix(input_tensor): """Reshapes a >= rank 2 tensor to a rank 2 tensor (i.e., a matrix).""" ndims = input_tensor.shape.ndims if ndims < 2: raise ValueError("Input tensor must have at least rank 2. Shape = %s" % (input_tensor.shape)) if ndims == 2: return input_tensor width = input_tensor.shape[-1] output_tensor = tf.reshape(input_tensor, [-1, width]) return output_tensor def reshape_from_matrix(output_tensor, orig_shape_list): """Reshapes a rank 2 tensor back to its original rank >= 2 tensor.""" if len(orig_shape_list) == 2: return output_tensor output_shape = get_shape_list(output_tensor) orig_dims = orig_shape_list[0:-1] width = output_shape[-1] return tf.reshape(output_tensor, orig_dims + [width])
PyTorch Forums
discuss.pytorch.org › uncategorized
Expand a 2d tensor to 3d tensor - Uncategorized - PyTorch Forums
November 7, 2017 - Let’s say I have a 2d tensor A A = [[0,1,2], [3,4,5], [6,7,8]] I want to copy each row 10 times and stack them, which will then give me a 3d tensor. So I will have 3 x 3 x 10 tensor. How can I do this? I know that a vector can be expanded by using expand_as, but how do I expand a 2d tensor?
PyTorch Forums
discuss.pytorch.org › t › from-3d-datas-to-2d-slices › 131488
From 3D datas to 2D slices - PyTorch Forums
September 9, 2021 - Hi there I have faced a problem and then I have read some forums but I couldn`t get the correct answer. I would like to ask again, in Dataset part I would like to read my 3D data which has first 2 dimension same shape ([512,512,600] or [512,512,550]) and I would like to create like 600 slices or 550 slices which has just [512,512] dimensions as 2d array. class TrainSet(Dataset): def __init__(self, image_dir, mask_dir): self.image_dir = image_dir self.mask_dir = mask_dir ...
PyTorch Forums
discuss.pytorch.org › t › converting-3-dimensional-matrix-into-two-dimensional-matrix › 13168
Converting 3-dimensional matrix into two dimensional matrix - PyTorch Forums
February 2, 2018 - I have the size of a is: torch.Size([260, 1152, 8]) My goal is to convert torch.Size([260, 1152, 8]) in to dimensional matrix [260 * n]. The new shape would be [260 x (1152 * 8)].
PyTorch Forums
discuss.pytorch.org › t › how-can-i-reduce-the-dimension-of-3d-tensor-to-2d-using-convolutional-filters › 166142
How can I reduce the dimension of 3d tensor to 2d using convolutional filters? - PyTorch Forums
November 16, 2022 - How can I reduce the dimension of 3d tensor to 2d using convolutional filters · How do I convert my model output torch.Size([2560, 128, 128]) to torch.Size([2560,128]) using convolutions · You could use an nn.Conv1d layer with a kernel size ...
PyTorch Forums
discuss.pytorch.org › t › how-to-reshape-tensors › 1926
How to reshape tensors? - PyTorch Forums
April 17, 2017 - I need to reshape a tensor with size [12, 1, 28, 28] now I need to flatten the last two and remove the second dimension so that the shape beocmes [12, 784] #28*28 -> 784 is there similar methods like reshape() as in n…
Top answer 1 of 2
4
You can permute the axes to the desired shape. (This is similar to numpy.moveaxis() operation).
In [90]: aa
Out[90]:
tensor([[[ 1, 2]],
[[ 3, 4]],
[[ 5, 6]]])
In [91]: aa.shape
Out[91]: torch.Size([3, 1, 2])
# pass the desired ordering of the axes as argument
# assign the result back to some tensor since permute returns a "view"
In [97]: permuted = aa.permute(1, 2, 0)
In [98]: permuted.shape
Out[98]: torch.Size([1, 2, 3])
In [99]: permuted
Out[99]:
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
2 of 2
0
This is one way to do it, still hope to see a solution with a single operation.
(Pdb) torch.transpose(aa, 0, 2).t()
tensor([[[ 1, 3, 5],
[ 2, 4, 6]]])
(Pdb) torch.transpose(aa, 0, 2).t().shape
torch.Size([1, 2, 3])
Codecademy
codecademy.com › docs › pytorch › tensors › .reshape()
PyTorch | tensors | .reshape() | Codecademy
September 23, 2024 - The torch.reshape() method reshapes a specified input tensor to a given shape while keeping the same data and number of elements.
PyTorch
docs.pytorch.org › docs › stable › generated › torch.reshape.html
torch.reshape — PyTorch 2.14 documentation
Redirecting… · Continue to ../../2.14/generated/torch.reshape.html
Top answer 1 of 11
56
Use torch.unsqueeze(input, dim, out=None):
>>> import torch
>>> a = torch.Tensor([1, 2, 3, 4, 5])
>>> a
1
2
3
4
5
[torch.FloatTensor of size 5]
>>> a = a.unsqueeze(0)
>>> a
1 2 3 4 5
[torch.FloatTensor of size 1x5]
2 of 11
23
you might use
a.view(1,5)
Out:
1 2 3 4 5
[torch.FloatTensor of size 1x5]
Reddit
reddit.com › r/mlquestions › how to assign 2d values to 3d tensor using 2d mask in pytorch?
r/MLQuestions on Reddit: How to assign 2d values to 3d tensor using 2d mask in pytorch?
October 24, 2020 -
x = torch.rand((32, 1000, 256)) lens = (torch.rand((32, 1))*1000).long() def mean_frame_dropout(x, lens, p=0.2):
x = x.clone() batch_size, length, features = x.size() mask = torch.distributions.Bernoulli( probs=(1 - p)).sample((batch_size, length)).to(x.device) mask = ~mask.bool() mean = x.detach().sum(1) / lens x[mask] = mean return x
With this function im getting error
RuntimeError: shape mismatch: value tensor of shape [32, 256] cannot be broadcast to indexing result of shape [6493, 256]
PyTorch Forums
discuss.pytorch.org › t › 2d-input-to-3d-expansion-using-stack-getting-expected-3d-tensor › 3305
2D input to 3D expansion using "stack", getting "expected 3D tensor" - PyTorch Forums
May 21, 2017 - I have a 2D input [Observations x Features] that I am trying to expand to 3D using nonlinear transformations so that I can use various convolution functions and architectures on it. I did some searching and found the “stack” function which seems to achieve this.
DZone
dzone.com › articles › reshaping-pytorch-tensors
Reshaping PyTorch Tensors - DZone
April 18, 2022 - A special case is two dimensions, 2d array is contiguous if the underlying data is stored row-wise sequentially. ... We can generalize that by saying the n-dimensional array is contiguous if its (n-1)-dimensional components are contiguous and stored sequentially in memory. Finally, we can say that a PyTorch tensor is contiguous if the n-dimensional array it represents is contiguous.