PyTorch is an open-source machine learning library developed by Facebook. It is used for deep neural network and natural language processing purposes.
The function torch.zeros() returns a tensor filled with the scalar value 0, with the shape defined by the variable argument size.
Syntax: torch.zeros(size, out=None)
Parameters:
size: a sequence of integers defining the shape of the output tensor
out (Tensor, optional): the output tensorReturn type: A tensor filled with scalar value 0, of same shape as size.
Code #1:
# Importing the PyTorch library import torch # Applying the zeros function and # storing the resulting tensor in 't' a = torch.zeros([3, 4]) print("a = ", a) b = torch.zeros([1, 5]) print("b = ", b) c = torch.zeros([5, 1]) print("c = ", c) d = torch.zeros([3, 3, 2]) print("d = ", d) |
Output:
a = tensor([[0., 0., 0., 0.],
[0., 0., 0., 0.],
[0., 0., 0., 0.]])
b = tensor([[0., 0., 0., 0., 0.]])
c = tensor([[0.],
[0.],
[0.],
[0.],
[0.]])
d = tensor([[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]],
[[0., 0.],
[0., 0.],
[0., 0.]]])
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Python | PyTorch sin() method
- Python | PyTorch sinh() method
- Python | PyTorch cosh() method
- Python | PyTorch tanh() method
- Python | PyTorch cos() method
- Python | PyTorch tan() method
- Python | PyTorch asin() method
- Python | PyTorch acos() method
- Python | PyTorch atan() method
- Python Pytorch ones() method
- Python Pytorch arange() method
- Python Pytorch linspace() method
- Python Pytorch range() method
- Python Pytorch logspace() method
- Python Pytorch eye() method
- Python Pytorch empty() method
- Python Pytorch full() method
- Python - PyTorch floor() method
- Python - PyTorch fmod() method
- Python - PyTorch log() method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

