Maximum between two tensors of different shape

Hi,
I have a small issue that I’m struggling with, and for which I didn’t find an adequate solution.

Having two two-dimensional tensors of different shape, I want to get the maximum between each element of the first tensor and all elements of the second tensor. As an example, I define two tensors:

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

In the code below, I illustrate that I can accomplish this task partially (via broadcasting) using only the first line of t1. That is, each line of t2 (which has 4 values) is compared with the first line of t1 (which has also 4 values).

# 't1[0]' has shape of (4,) 
# Output will have shape of (5, 4)
torch.maximum(t1[0], t2)

But what I want is to generalize this concept across all t1 lines. I tried the code below, but it doesn’t work:

# Doesn't work, but I want to get an output shape of (3, 5, 4)
torch.maximum(t1, t2)

Just to be more precise, I want a vectorized version of the code below:

# 'maxs' will contain the maximum between each line of 't1' with 't2'
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)

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