Element-Wise Max Between Two Tensors?

vitchyr: <p>Easy there a way to take the element-wise max between two tensors, as in <code>tf.maximum</code>? My current work-around is</p>
<pre><code class="lang-auto">def max(t1, t2):
    combined = torch.cat((t1.unsqueeze(2), t2.unsqueeze(2)), dim=2)
    return torch.max(combined, dim=2)[0].squeeze(2)
</code></pre>
<p>but it’s a bit clunky.</p>

iamalbert: <p><a href="http://pytorch.org/docs/torch.html#torch.max" class="onebox" target="_blank" rel="nofollow noopener">http://pytorch.org/docs/torch.html#torch.max</a></p>
<p>The third version of <code>torch.max</code> is exactly what you want.</p>

vitchyr: <p>Thanks! I guess I just hadn’t scrolled down far enough.</p>

youkaichao: <p>it doesn’t support usage like <code>torch.min(tensor1, 0.0)</code></p>

moskomule: <p><code>torch.min(tensor1, torch.zeros_like(tensor1))</code></p>

I-Love-U: <p>Now, wo can use the <code>torch.max</code></p>
<p>like this: <a href="https://pytorch.org/docs/stable/torch.html#torch.max" rel="nofollow noopener">https://pytorch.org/docs/stable/torch.html#torch.max</a></p>
<pre><code class="lang-python">&gt;&gt;&gt; a = torch.randn(4)
&gt;&gt;&gt; a
tensor([ 0.2942, -0.7416,  0.2653, -0.1584])
&gt;&gt;&gt; b = torch.randn(4)
&gt;&gt;&gt; b
tensor([ 0.8722, -1.7421, -0.4141, -0.5055])
&gt;&gt;&gt; torch.max(a, b)
tensor([ 0.8722, -0.7416,  0.2653, -0.1584])
</code></pre>

Umair_Javaid: <p>How to do elementwise max between multiple filters? getting the following error for 3 tensors</p>
<pre><code class="lang-auto">a = torch.ones(1,4,1,1)*2

b = torch.ones(1,4,1,1)*3

c = torch.ones(1,4,1,1)*4

#d = torch.ones(1,4,1,1)*5

max_ = torch.max(a,b,c)

print(max_)
</code></pre>
<pre><code class="lang-auto">TypeError: max() received an invalid combination of arguments - got (Tensor, Tensor, Tensor), but expected one of:
 * (Tensor input)
 * (Tensor input, name dim, bool keepdim, *, tuple of Tensors out)
 * (Tensor input, Tensor other, *, Tensor out)
 * (Tensor input, int dim, bool keepdim, *, tuple of Tensors out)
</code></pre>

I-Love-U: <p>Here is a simple method:</p>
<pre><code class="lang-python"># 1 reduce
import functools
max_tensor = functools.reduce(torch.max, [a, b, c])

# 2 for
tensor_list = [a, b, c]
max_tensor = tensor_list[0]
for tensor in tensor_list[1:]:
    max_tensor = torch.max(max_tensor, tensor)
</code></pre>

Sayak_Paul: <p>Thanks for your solution <a class="mention" href="/u/i-love-u">@I-Love-U</a>. But how should one extend this to an arbitrary number of arrays? The first method does not work for 4 arrays.</p>

ariG23498: <p><a href="https://pytorch.org/docs/stable/generated/torch.maximum.html#torch.maximum" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/stable/generated/torch.maximum.html#torch.maximum</a></p>
<p>Does this help here?</p>

I-Love-U: <p>A overload implementation of  <code>torch.max</code>  is the same as <code>torch.maximum</code>: <a href="https://pytorch.org/docs/stable/generated/torch.max.html?highlight=torch%20max#torch.max" class="inline-onebox" rel="noopener nofollow ugc">torch.max — PyTorch 1.8.1 documentation</a></p>
<p>So, we can focus on <code>torch.max</code> <img src="https://discuss.pytorch.org/images/emoji/apple/smile.png?v=9" title=":smile:" class="emoji" alt=":smile:"></p>

I-Love-U: <p><img src="https://discuss.pytorch.org/images/emoji/apple/confused.png?v=9" title=":confused:" class="emoji" alt=":confused:"> Can you give more information about the error?</p>
<p>Here are some examples and as we can see that these code can work well.</p>
<pre><code class="lang-python">tensor_list = [torch.randn(2, 3) for _ in range(5)]
tensor_list
Out[9]: 
[tensor([[-0.6082, -0.9290, -0.4921],
         [ 0.3344, -0.9338, -0.8563]]),
 tensor([[-0.3530, -0.5673,  2.6954],
         [ 1.5262,  2.3859,  0.3481]]),
 tensor([[ 0.5392,  0.9646, -1.5962],
         [-2.2931,  0.6707, -0.4896]]),
 tensor([[-1.3532, -0.5953,  1.6039],
         [ 0.2937,  0.3643,  1.3153]]),
 tensor([[ 1.1544,  0.7681, -1.0410],
         [-0.1305, -0.8855, -0.3516]])]

import functools
functools.reduce(torch.max, tensor_list)
Out[14]: 
tensor([[1.1544, 0.9646, 2.6954],
        [1.5262, 2.3859, 1.3153]])

max_tensor = tensor_list[0]
for tensor in tensor_list[1:]:
    max_tensor = torch.max(max_tensor, tensor) 

max_tensor
Out[19]: 
tensor([[1.1544, 0.9646, 2.6954],
        [1.5262, 2.3859, 1.3153]])
</code></pre>