TypeError: new() received an invalid combination of arguments - got (Tensor, Tensor)

username: muhibkhan
cooked: <p>Hey mates. Hope you are in good health. I need some guidance. I’m rebuilding an embedding model from TensorFlow to PyTorch. Here is the link:</p>
<aside class="onebox githubblob" data-onebox-src="https://github.com/Sujit-O/pykg2vec/blob/702e0d4012195dd8b807ec9f412887a7f3c02c9e/pykg2vec/core/TransM.py#L124">
  <header class="source">

      <a href="https://github.com/Sujit-O/pykg2vec/blob/702e0d4012195dd8b807ec9f412887a7f3c02c9e/pykg2vec/core/TransM.py#L124" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/Sujit-O/pykg2vec/blob/702e0d4012195dd8b807ec9f412887a7f3c02c9e/pykg2vec/core/TransM.py#L124" target="_blank" rel="noopener nofollow ugc">Sujit-O/pykg2vec/blob/702e0d4012195dd8b807ec9f412887a7f3c02c9e/pykg2vec/core/TransM.py#L124</a></h4>



    <pre class="onebox"><code class="lang-py">
      <ol class="start lines" start="114" style="counter-reset: li-counter 113 ;">
          <li></li>
          <li>    score_pos = pos_r_theta*self.distance(pos_h_e, pos_r_e, pos_t_e)</li>
          <li>    score_neg = neg_r_theta*self.distance(neg_h_e, neg_r_e, neg_t_e)</li>
          <li></li>
          <li>    self.loss = tf.reduce_sum(tf.maximum(score_pos + self.config.margin - score_neg, 0))</li>
          <li></li>
          <li>def test_batch(self):</li>
          <li>    """Function that performs batch testing for the algorithm.</li>
          <li></li>
          <li>       Returns:</li>
          <li class="selected">           Tensors: Returns ranks of head and tail.</li>
          <li>    """</li>
          <li>    head_vec, rel_vec, tail_vec = self.embed(self.test_h_batch, self.test_r_batch, self.test_t_batch)</li>
          <li></li>
          <li>    norm_ent_embeddings = tf.nn.l2_normalize(self.ent_embeddings, axis=1)</li>
          <li>    score_head = self.distance(norm_ent_embeddings, tf.expand_dims(rel_vec, 1), tf.expand_dims(tail_vec, 1))</li>
          <li>    score_tail = self.distance(tf.expand_dims(head_vec, 1), tf.expand_dims(rel_vec, 1), norm_ent_embeddings)</li>
          <li></li>
          <li>    _, head_rank = tf.nn.top_k(score_head, k=self.data_stats.tot_entity)</li>
          <li>    _, tail_rank = tf.nn.top_k(score_tail, k=self.data_stats.tot_entity)</li>
          <li></li>
      </ol>
    </code></pre>



  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<p>On line 112 and 113, As I build and pass in my pytorch code:</p>
<pre><code class="lang-auto">    pos_r_theta = nn.Embedding(self.theta, pos_r)
    neg_r_theta = nn.Embedding(self.theta, neg_r)
</code></pre>
<p>It show me error (attached image). <strong>Q1:</strong> why this embedding not accepting the tensors as my parameters? <strong>Q2:</strong> what is the difference between tf.nn.embedding_lookup() in tf and nn.embedding() in pytorch? <strong>Q3:</strong> Does pytorch support any function similar to np.asarray (code line 103) (now want to use torch.from_numpy, proceed only in pytorch)?</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/e0bca540b0e2974056698490ec712276c17f835e.png" data-download-href="https://discuss.pytorch.org/uploads/default/e0bca540b0e2974056698490ec712276c17f835e" title="error"><img src="https://discuss.pytorch.org/uploads/default/original/2X/e/e0bca540b0e2974056698490ec712276c17f835e.png" alt="error" data-base62-sha1="w477IJhFX7vtqwMgyh3eSGOAK1E" width="690" height="103" data-dominant-color="0D0B0C"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">error</span><span class="informations">1111×167 7.75 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: spanev
cooked: <p>Hey <a class="mention" href="/u/muhibkhan">@muhibkhan</a>,</p>
<p><strong>A1</strong>: When you do this:</p>
<pre><code class="lang-auto">pos_r_theta = nn.Embedding(self.theta, pos_r)
</code></pre>
<p>you are creating an embedding laye where the two first args (self.theta, pos_r) should be integers indicating first the number of embeddings, then the embeddings dim.<br>
Here’s an example of how to use it:</p>
<pre><code class="lang-auto"># in __init__
num_embs = 3
emb_dim = 10
emb = nn.Embedding(num_embs, emb_dim) 
...
# later in forward
x = torch.LongTensor([2,1,0])
</code></pre>
<p>and the <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.Embedding" rel="nofollow noopener">documentation</a></p>
<p><strong>A2</strong>: From what I see from <code>tf.nn.embedding_lookup</code>, they are basically the same thing.</p>
<p>The difference being that the lookup table initialization is automatic in PyTorch (based on the arguments the <code>num_embeddings x embedding_dim</code> param will be initialized from a standard normal distribution), while you have to pass the already initialized table in TF (the <code>param</code> argument).</p>
<p><strong>A3</strong>:  If I understand correctly you want to create a <code>torch.Tensor</code> from a list, similarly to what is done on <a href="https://github.com/Sujit-O/pykg2vec/blob/702e0d4012195dd8b807ec9f412887a7f3c02c9e/pykg2vec/core/TransM.py#L103" rel="nofollow noopener">line 103</a>.</p>
<p>You can simply create the Tensor by passing your array <code>list</code> to the constructor:</p>
<pre><code class="lang-auto">list = [1,2,3]
tensor = torch.FloatTensor(list) 
#or
tensor = torch.LongTensor(list)
</code></pre>
<p>Hope that answers some of your questions.</p>

username: muhibkhan
cooked: <p>Thanks mate. In attached link, kindly see lines (58, 59, 103) that used by the lines 112 &amp; 113. He passed the tensor as parameters. When IO passed the tensor, it showed me error of - got (Tensor, Tensor)…?</p>