all AI news
sum() and prod() in PyTorch
April 13, 2024, 10:05 p.m. | Super Kai (Kazuya Ito)
DEV Community dev.to
sum() can get one or more sum values as shown below:
*Memos:
sum() can be called both from torch and a tensor.- The 2nd argument is one or more dimensions with torch.
- The 1st argument is one or more dimensions with a tensor.
import torch
my_tensor = torch.tensor([[5, 4, 7, 7],
[6, 5, 3, 5],
[3, 8, 9, 3]])
torch.sum(my_tensor)
my_tensor.sum()
# tensor(65)
torch.sum(my_tensor, 0)
my_tensor.sum(0)
torch.sum(my_tensor, (0,))
my_tensor.sum((0,))
torch.sum(my_tensor, -2)
my_tensor.sum(-2)
torch.sum(my_tensor, (-2,))
my_tensor.sum((-2,))
# tensor([14, 17, 19, 15]) …
dimensions function import memos prod pytorch sum tensor torch values
More from dev.to / DEV Community
Jobs in AI, ML, Big Data
Artificial Intelligence – Bioinformatic Expert
@ University of Texas Medical Branch | Galveston, TX
Lead Developer (AI)
@ Cere Network | San Francisco, US
Research Engineer
@ Allora Labs | Remote
Ecosystem Manager
@ Allora Labs | Remote
Founding AI Engineer, Agents
@ Occam AI | New York
AI Engineer Intern, Agents
@ Occam AI | US