Skip to content

Libtorch tensor.std(0) gets confused about which overload to use (expected vector, not scalar) #40287

@BryanWBear

Description

@BryanWBear

🐛 Bug

Tensor.std(0) returns a scalar instead of a vector.

To Reproduce

Steps to reproduce the behavior:

float data[] = { 1, 2, 3, 4, 5, 6 };
torch::Tensor f = torch::from_blob(data, {2, 3});
std::cout << f.mean(0) << std::endl;
// yields a 1-d tensor as expected.
// 2.5000
// 3.5000
// 4.5000
// [ CPUFloatType{3} ]
std::cout << f.std(0) << std::endl;
// yields a scalar, unexpected.
// 1.70783
// [ CPUFloatType{} ]

Expected behavior

Tensor.std(0) should return a vector to be consistent with other methods. As @ptrblck pointed out in this thread https://discuss.pytorch.org/t/tensor-std-0-returns-a-scalar-instead-of-a-vector/85974, the reason this is happening is because 0 is being interpreted as the unbiased argument here, and suggests f.std(true, 0) as a workaround. However, even f.std(true, 0) actually collapses dimension 1, which is also unintuitive.

Ideally, these behaviors should be fixed or listed in the documentation.

Environment

Libtorch 1.4.0 on XCode 11.4

Additional context

cc @ezyang @gchanan @zou3519 @yf225 @glaringlee @bhosmer @smessmer @ljk53 @ssnl

Metadata

Metadata

Assignees

Labels

has workaroundhigh prioritymodule: bc-breakingRelated to a BC-breaking changemodule: cppRelated to C++ APImodule: internalsRelated to internal abstractions in c10 and ATentriagedThis issue has been looked at a team member, and triaged and prioritized into an appropriate module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions