How to find Min over a dimension of a Variable?

Hi all, I got a question about how to find Min over a dimension of a Variable. I’ve tried to use torch.min() but it returned a tuple.

If you read the documentation more carefully, you’ll found that the tuple contains the argmin and min value.

Returns the minimum value of each row of the input Tensor in the given dimension dim. The second return value is the index location of each minimum value found (argmin).

1 Like

You’re correct. However, I would like to use the minimum value for a further operation and it must be compatible with autograd. I’m not sure if I make some operations on the Tensor of the tuple, the connectivity of the layer to my graph would be changed or not.

If you use part of the returned tuple, e.g. result[0], it will not change your computation graph. You could check this by visualizing the grad of previous layers.

I see. Thank for your help :smiley:

Try this:

min_val, _ = torch.min(input, dim=1, keepdim=True)

1 Like