Answers for "pytorch squeeze"

1

what is squeeze function in pytorch?

import torch
t = torch.tensor([[1,17,7,
                     3,9,10]])
print(t)
>>>tensor([[1,17,7,3,9,10]])
print(torch.squeeze(t))
>>>tensor([ 1, 17,  7,  3,  9, 10])

t = torch.tensor([[[1,17,7,
                     3,9,10]]])
print(t)
>>>tensor([[[1,17,7,3,9,10]]])
print(torch.squeeze(t))
>>>tensor([ 1, 17,  7,  3,  9, 10])
Posted by: Guest on November-26-2020
0

pytorch squeeze

x = torch.zeros(2, 1, 2, 1, 2)
x.size()
>>> torch.Size([2, 1, 2, 1, 2])

y = torch.squeeze(x) # remove 1
y.size()
>>> torch.Size([2, 2, 2])

y = torch.squeeze(x, 0)
y.size()
>>> torch.Size([2, 1, 2, 1, 2])

y = torch.squeeze(x, 1)
y.size()
>>> torch.Size([2, 2, 1, 2])
Posted by: Guest on October-27-2020
0

pytorch unsqueeze

ft = torch.Tensor([0, 1, 2])
print(ft.shape)
>>> torch.Size([3])

print(ft.unsqueeze(0)) # 0 means first dimension
print(ft.unsqueeze(0).shape)
>>> tensor([[0., 1., 2.]])
>>> torch.Size([1, 3])
Posted by: Guest on October-27-2020

Python Answers by Framework

Browse Popular Code Answers by Language