Hi all, I am trying to convert TensorFlow code to PyTorch. But I don’t have any prior knowledge with TensorFlow, I will be grateful if someone can help with this situation. Here is the code

If the input tensor becomes empty torch.max(), will give an error vs tf.reduce_max will give -inf.

Is there someway we can retain the same behavior as tf.

Example:
torch.max(torch.tensor([]))
RuntimeError: max(): Expected reduction dim to be specified for input.numel() == 0. Specify the reduction dim with the ‘dim’ argument.

Could you explain why a -Inf return value makes sense for the max operation of en empty tensor?
I can see why raising an error makes sense, but I’m unsure how the -Inf is defined.

Couple of things IMHO, may be they are not strong enough reasons.

a.
Other operations on empty tensor:
torch.mean(torch.tensor([]))
tensor(nan)
torch.sum(torch.tensor([]))
tensor(0.)

The behaviour for these reduction operations is not consistent with max.

b.
Like in sum output is zero, if you use this further with other reduction method or use the value then it will be consistent, without any side effects.

Similarly, for max → -Inf means max is smallest possible value and if we use this with lets say another max(max(empty), [1,2,3]) then the output will be consistent without any side effects.

Same logic for min → +Inf

c. I faced this issue while migrating network/training from TensorFlow to PyTorch, in such cases if we do not have identical behaviour then there are high chances of the migrated code having side effects, as we need to modify the code elsewhere to ensure we get similar behaviour.

Thanks for the explanation. I believe PyTorch sticks to the numpy reference, which shows the same behavior:

np.mean(np.array([]))
# nan
np.sum(np.array([]))
# 0.0
np.max(np.array([]))
# ValueError: zero-size array to reduction operation maximum which has no identity

In any case, I think you should create a feature request on GitHub as your use case of having a “consistent” user interface makes sense.