Maximum between two tensors of different shape

You could also use broadcasting on both tensors:

t1 = torch.randn(3, 4)
t2 = torch.randn(5, 4)

maxs = []

for line in t1:
  # Current maximum has shape of (5, 4)
  curr_max = torch.maximum(line, t2)
  maxs.append(curr_max)

# 'maxs' will have shape of (3, 5, 4)
maxs = torch.stack(maxs)

# use broadcasting
max_broadcast = torch.maximum(t1.unsqueeze(1), t2.unsqueeze(0))
print((maxs - max_broadcast).abs().max())
> tensor(0.)
1 Like