In PyTorch what is the difference between new_ones()
vs ones()
. For example,
x2.new_ones(3,2, dtype=torch.double)
vs
torch.ones(3,2, dtype=torch.double)
ones_like(input, out=output) is equivalent to torch. ones(input. size(), out=output) . input (Tensor) – the size of input will determine size of the output tensor. dtype ( torch.
To compare two tensors element-wise in PyTorch, we use the torch. eq() method. It compares the corresponding elements and returns "True" if the two elements are same, else it returns "False".
For the sake of this answer, I am assuming that your x2
is a previously defined torch.Tensor
. If we then head over to the PyTorch documentation, we can read the following on new_ones()
:
Returns a Tensor of size
size
filled with1
. By default, the returned Tensor has the sametorch.dtype
andtorch.device
as this tensor.
Whereas ones()
Returns a tensor filled with the scalar value 1, with the shape defined by the variable argument sizes.
So, essentially, new_ones
allows you to quickly create a new torch.Tensor
on the same device and data type as a previously existing tensor (with ones), whereas ones()
serves the purpose of creating a torch.Tensor
from scratch (filled with ones).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With