Computes square of x element-wise

username: Md_Mahfujur_Rahman_0
cooked: <p>I want to use similar kind of square function in tensorflow</p>
<p>In tensorflow tf.square is given. But I cannot find in pytorch. Thanks in advanced.<br>
tf.square</p>
<p>square(<br>
x,<br>
name=None<br>
)</p>

username: WERush
cooked: <p>Maybe torch.mul(x,x) works for you.</p>

username: Md_Mahfujur_Rahman_0
cooked: <p>Thanks. I need to compute square of x element-wise.</p>
<p>i.e., y=x*x=x^2</p>

username: WERush
cooked: <p>Yes, <code>torch.mul()</code> is the element-wise multiplication</p>

username: ptrblck
cooked: <p><code>x**2</code> should work as well.</p>
<pre><code>a = torch.randn(2, 2)
print a

&gt;&gt; 0.4616  0.2764
&gt;&gt; -0.4579  0.9157
&gt;&gt; [torch.FloatTensor of size 2x2]

print a**2

&gt;&gt; 0.2131  0.0764
&gt;&gt; 0.2096  0.8384
&gt;&gt; [torch.FloatTensor of size 2x2]
</code></pre>
<p>Is this what you are looking for?</p>
<p>As <span class="mention">@Xinge</span> mentioned, <code>torch.mul(a, a)</code> gives the same result.</p>

username: Md_Mahfujur_Rahman_0
cooked: <p>Thank you. I got it.</p>

username: wangg12
cooked: <p>several ways to do this:</p>
<pre><code class="lang-auto">torch.pow(x, 2)
x**2
x*x
torch.mul(x, x)
</code></pre>

username: Yaroslav_Bulatov
cooked: <aside class="onebox githubissue">
  <header class="source">
      <a href="https://github.com/pytorch/pytorch/issues/30524" target="_blank" rel="nofollow noopener">github.com/pytorch/pytorch</a>
  </header>
  <article class="onebox-body">
    <div class="github-row">
  <div class="github-icon-container" title="Issue">
	  <svg width="60" height="60" class="github-icon" viewbox="0 0 14 16" aria-hidden="true"><path d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg>
  </div>

  <div class="github-info-container">
    <h4>
      <a href="https://github.com/pytorch/pytorch/issues/30524" target="_blank" rel="nofollow noopener">add torch.square</a>
    </h4>

    <div class="github-info">
      <div class="date">
        opened <span class="discourse-local-date" data-format="ll" data-date="2019-11-27" data-time="18:45:25" data-timezone="UTC">06:45PM - 27 Nov 19 UTC</span>
      </div>


      <div class="user">
        <a href="https://github.com/yaroslavvb" target="_blank" rel="nofollow noopener">
          <img alt="yaroslavvb" src="https://avatars3.githubusercontent.com/u/23068?v=4" class="onebox-avatar-inline" width="20" height="20">
          yaroslavvb
        </a>
      </div>
    </div>
  </div>
</div>

<div class="github-row">
  <p class="github-content">torch.square would be useful when you need to do x*x but x is a large expression
np.square([1,2,3]) # =&gt; array([1, 4, 9])
tf.square([1,2,3]).eval()...</p>
</div>

<div class="labels">
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">enhancement</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">module: operators</span>
    <span style="display:inline-block;margin-top:2px;background-color: #B8B8B8;padding: 2px;border-radius: 4px;color: #fff;margin-left: 3px;">triaged</span>
</div>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>