PyTorch torch.std_mean Function


Pytorch torch 参考手册Pytorch torch Reference Manual

torch.std_meanis a function in PyTorch that returns both the standard deviation and mean of a tensor simultaneously. It is an efficient function that can compute the standard deviation and mean in one operation.

Function Definition

torch.std_mean(input, dim, unbiased, keepdim=False)

Usage Example

Example

import torch

x = torch.tensor([1.0, 2.0, 3.0, 4.0, 5.0])

# Return the standard deviation and mean simultaneously
std, mean = torch.std_mean(x)
print("Standard deviation:", std)
print("Mean:", mean)

# Return the standard deviation and mean simultaneously along dim=0
y = torch.tensor([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])
std0, mean0 = torch.std_mean(y, dim=0)
print("dim=0 standard deviation:", std0)
print("dim=0 mean:", mean0)

The output result is:

标准差: tensor(1.5811)
均值: tensor(3.)
dim=0 标准差: tensor([1.5000, 1.5000, 1.5000])
dim=0 均值: tensor([2.5000, 3.5000, 4.5000])

Pytorch torch 参考手册Pytorch torch Reference Manual

Other Extensions