Is there a way to clip with a tensor?

Felipe_Mello: <p>Hi everyone,</p>
<p>Suppose I have a tensor</p>
<p><code>mytensor = torch.tensor([10, 20, 30, 40)]</code></p>
<p>Each node has a different threshold. Lets say that min thresholds are:</p>
<p><code>mythreshold = torch.tensor([2, 50, 25, 100])</code></p>
<p>I wanted to do <code>torch.clamp(mytensor, min = mythreshold )</code> and  get as output</p>
<p><code>torch.tensor([10, 50, 30, 100]).</code></p>
<p>Is it possible?</p>
<p>Thanks!</p>

ptrblck: <p><code>torch.where</code> should work:</p>
<pre><code class="lang-python">mytensor = torch.tensor([10, 20, 30, 40])
mythreshold = torch.tensor([2, 50, 25, 100])

torch.where(mytensor &lt; mythreshold, mythreshold, mytensor)
&gt; tensor([10, 50, 30, 100])
</code></pre>

Felipe_Mello: <p>Worked like a charm. Thanks for your awesome work here at the forum <a class="mention" href="/u/ptrblck">@ptrblck</a></p>

saba: <p>Hi Ptrblck,</p>
<p>I want to use Border22 with the values just higher than 0, i tried x[x&gt;value] or x[:,x&gt;value], but does not work. I need to have values just higher than zero and then pass it to the backward.()<br>
this ( Border22[Border22&gt;0]) give me again 0 value too.</p>
<pre><code class="lang-auto">        Border=torch.ones(fake.shape)
        Border[:,:,4-2:4+3,4-2:4+3]=0
        Border22=torch.mul(Border,fake).view(-1)
        Border22[Border22&gt;0]
        Ones=torch.ones(1)
        loss2=criterion2()(Border22,Ones)
       loss2.bachward.()</code></pre>

ptrblck: <p>It seems you are missing the assignment of the indexing operation:</p>
<pre><code class="lang-python">Border22 = Border22[Border22&gt;0]
</code></pre>

saba: <p>Hi Ptrblck,</p>
<p>I change my cod ein different ways to solve the error. The current code is as follow, but it gave me zero gradients. Would you please help me with that? L1 is 0.8</p>
<pre><code class="lang-auto">
        Thresholddefalut=nn.Threshold(0.98,0)
                                
        bbb=fake.squeeze(1)
        bbb1=MASKGaussy.squeeze(1)

        zzz=Thresholddefalut(bbb)
        zzz1=Thresholddefalut(bbb1)

        L1=nn.L1Loss()(zzz,zzz1)

        loss2=L1
            
        loss2.backward()
        print(netG.l3[0].weight.grad)
## -----------------

        class Generator994(nn.Module):
    def __init__(self,ngpu,nz,ngf):
        super(Generator994, self).__init__()
        self.ngpu=ngpu
        self.nz=nz
        self.ngf=ngf
        self.l1= nn.Sequential(

            nn.ConvTranspose2d(self.nz, self.ngf * 8, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 8),
            nn.ReLU(True),)
 
        self.l2=nn.Sequential(nn.ConvTranspose2d(self.ngf * 8, self.ngf * 4, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 4),
            nn.ReLU(True),)

        self.l3=nn.Sequential(nn.ConvTranspose2d( self.ngf * 4, self.ngf * 2, 3, 1, 0, bias=False),
            nn.BatchNorm2d(self.ngf * 2),
             nn.ReLU(True),)
6
        self.l4=nn.Sequential(nn.ConvTranspose2d( self.ngf*2, 1, 3, 1, 0, bias=False),nn.Sigmoid()

        )

    def forward(self, input):
        out=self.l1(input)
        out=self.l2(out)
        out=self.l3(out)
        out=self.l4(out)

        return out
</code></pre>

saba: <p>I would appreciate your response</p>

ptrblck: <p>Your current code snippet uses undefined methods, so I cannot debug it.<br>
As usual, could you post an executable code snippet, which would reproduce this issue?</p>

saba: <p>Hi Ptrblck,</p>
<p>I solved the problem of this code. Now I have another , never finish. I need to do binary thresholding for x&gt;value =1 and x&lt;value=0, but in the way that after thresholding I can pass it to the backward.().<br>
For example  for the above code zzz and zzz1 be the binary maps and use in L1 loss. How define zzz and zzz1?</p>

ptrblck: <p>Based on your description it seems you want to use a step function, which would have a zero gradient everywhere (except exactly as <code>x==value</code>), so it won’t be useful for your loss and gradient calculation.<br>
If this would work, you could directly optimize the model predictions without e.g. using <code>sigmoid</code> or <code>softmax</code>.</p>