AttributeError: 'tuple' object has no attribute 'repeat'

username: Hisrar
cooked: <p>I am doing an assignment on Neural Machine Translation and training an LSTM specifically . During Decoding stage I got this Error " AttributeError: ‘tuple’ object has no attribute ‘repeat’ ". I am unable to understand what the error (that I am getting) is, and how I can solve it?</p>
<p>Traceback (most recent call last):<br>
File “train.py”, line 618, in <br>
early_stopping=early_stopping)<br>
File “train.py”, line 186, in train_epochs<br>
decode(model, dataset, epoch)<br>
File “/home/hisrar/NMT/kg_model.py”, line 357, in beam_decode<br>
dec_hidden.repeat(1, batch_size * beam_size, 1)<br>
AttributeError: ‘tuple’ object has no attribute ‘repeat’</p>
<p>Part of the program where I get the Error is</p>
<pre><code>    batch_size, beam_size = self.config.batch_size, self.config.beam_size

    # [1, batch_size x beam_size]

    dec_input = torch.ones(1, batch_size * beam_size,
                           dtype=torch.long,
                           device=self.device) * SOS_ID

   #[num_layers, batch_size * beam_size, hidden_size]

    #dec_hidden = dec_hidden.repeat(1, beam_size,1)
  
    if enc_outputs is not None:
        enc_outputs = enc_outputs.repeat(1, beam_size, 1)
        enc_length = enc_length.repeat(beam_size)
</code></pre>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b844f4eb23a1c6fff6b8bf5d4a9c6c85a7b8dae5.png" data-download-href="https://discuss.pytorch.org/uploads/default/b844f4eb23a1c6fff6b8bf5d4a9c6c85a7b8dae5" title="screenshot"><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/8/b844f4eb23a1c6fff6b8bf5d4a9c6c85a7b8dae5.png" alt="screenshot" data-base62-sha1="qi7ANWJnrNqrYxUDtW7s8J7C017" width="690" height="497" data-dominant-color="3B1730"><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">screenshot</span><span class="informations">955×689 48.8 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: AlphaBetaGamma96
cooked: <aside class="quote no-group" data-username="Hisrar" data-post="1" data-topic="141166">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/hisrar/48/30308_2.png" class="avatar"> Hisrar:</div>
<blockquote>
<p>dec_hidden</p>
</blockquote>
</aside>
<p>the attribute <code>repeat</code> is available to <code>Tensor</code> but the variable <code>dec_hidden</code> is of type Tuple. Make sure to convert your tuple to a <code>Tensor</code> and then it’ll work. If it’s a <code>Tuple</code> of 1 object do,</p>
<pre><code class="lang-auto">dec_hidden[0].repeat(1, beam_size, 1)
</code></pre>

username: Hisrar
cooked: <pre><code class="lang-auto">dec_hidden[0].repeat(1, beam_size, 1)
</code></pre>
<p>changing dec_hidden to dec_hidden[0],  I got a new error.</p>
<p>File “/usr/local/lib/python3.5/dist-packages/torch/nn/modules/rnn.py”, line 187, in check_hidden_size<br>
raise RuntimeError(msg.format(expected_hidden_size, tuple(hx.size())))<br>
RuntimeError: Expected hidden[0] size (2, 64, 500), got (64, 500)</p>

username: AlphaBetaGamma96
cooked: <p>can you print what <code>dec_hidden</code> is?</p>

username: Hisrar
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/4/b49b63094300288dc9c3592de5929bf083018c16.png" data-download-href="https://discuss.pytorch.org/uploads/default/b49b63094300288dc9c3592de5929bf083018c16" title="dec_hidden"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b49b63094300288dc9c3592de5929bf083018c16_2_690x227.png" alt="dec_hidden" data-base62-sha1="pLIRhThyfjQH2ncjoU9giaX8jtk" width="690" height="227" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b49b63094300288dc9c3592de5929bf083018c16_2_690x227.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/4/b49b63094300288dc9c3592de5929bf083018c16_2_1035x340.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/4/b49b63094300288dc9c3592de5929bf083018c16.png 2x" data-dominant-color="3E1A32"><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">dec_hidden</span><span class="informations">1234×406 57.9 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: AlphaBetaGamma96
cooked: <p>Ok so it seems that that object is a Tuple of 2 tensors. Do you want to repeat both Tensors or just one? If that have the same shape you can use <code>torch.cat</code> then that’ll convert your tuple to a Tensor that can then use <code>.repeat</code></p>

username: Hisrar
cooked: <p>Yes I want to repeat both Tensors.</p>

username: AlphaBetaGamma96
cooked: <aside class="quote no-group" data-username="AlphaBetaGamma96" data-post="2" data-topic="141166">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/a/3da27b/48.png" class="avatar"> AlphaBetaGamma96:</div>
<blockquote>
<p><code>dec_hidden</code></p>
</blockquote>
</aside>
<p>Could try something like this?</p>
<p>dec_hidden_repeeat = [x.repeat(1, beam_size, 1) for x in dec_hidden]</p>

username: Hisrar
cooked: <p>Tried the Idea with little variation, and it works well . <a class="mention" href="/u/alphabetagamma96">@AlphaBetaGamma96</a>  Thank you so much for your Help</p>