Pytorch expand_as function; expand_as function
expand()
expand(*sizes)
Tensor will be extended to the same size and parameter sizes.
Parameters:
sizes (torch.Size or int): the need to expand the size. It must be a tuple consisting of ints.
>>> x = torch.Tensor([[1], [2], [3]])
>>> y = x.expand(3, 3)
>>> print(x)
tensor([[1.],
[2.],
[3.]])
>>> print(y)
tensor([[1., 1., 1.],
[2., 2., 2.],
[3., 3., 3.]])
>>> print(x.shape)
torch.Size([3, 1])
>>> print(y.shape)
torch.Size([3, 3])
expand_as()
expand_as(tensor)
The tensor expanded to the size of the parameters of tensor.
>>> x = torch.randn(1, 3, 1, 1)
>>> y = torch.randn(1, 3, 3, 3)
>>> z = x.expand_as(y)
>>> print(x)
tensor([[[[ 0.4383]],
[[-1.5909]],
[[ 0.0814]]]])
>>> print(z)
tensor([[[[ 0.4383, 0.4383, 0.4383],
[ 0.4383, 0.4383, 0.4383],
[ 0.4383, 0.4383, 0.4383]],
[[-1.5909, -1.5909, -1.5909],
[-1.5909, -1.5909, -1.5909],
[-1.5909, -1.5909, -1.5909]],
[[ 0.0814, 0.0814, 0.0814],
[ 0.0814, 0.0814, 0.0814],
[ 0.0814, 0.0814, 0.0814]]]])