Error: Expected object of scalar type Long but got scalar type Float for argument #2 'mat2'

Aya_Uchida: <pre><code class="lang-auto">class FC(nn.Module):
    def __init__(self, opt):
        super(FC, self).__init__()
        self.encoder = nn.Embedding(opt.VOCAB_SIZE, opt.EMBEDDING_DIM)
        self.gru = nn.Sequential(
            nn.GRU(input_size=100,hidden_size=opt.LINER_HID_SIZE),
            nn.ReLU(False),
        )
        self.fc_1 = nn.Sequential(
            nn.Linear(10000, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_1*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_1*10, opt.NUM_CLASS_1),
            nn.Dropout(0.5),
        )
        self.fc_2 = nn.Sequential(
            nn.Linear(10000, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_2*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_2*10, opt.NUM_CLASS_2),
            nn.Dropout(0.5),
        )
        self.fc_3 = nn.Sequential(
            nn.Linear(10000, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_3*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_3*10, opt.NUM_CLASS_3),
            nn.Dropout(0.5),
        )

    def forward(self, x):
        x = x.long()
        outputs = self.encoder(x)
        outputs = outputs.long()
        outputs = self.gru(outputs)
        outputs = outputs.view(outputs.size()[0], -1)
        output_1 = self.fc_1(outputs)
        output_2 = self.fc_2(outputs)
        output_3 = self.fc_3(outputs)
        return (output_1, output_2, output_3)
</code></pre>
<p>I consider that I have just transformed the ‘output’ to LongTensor but there is also an error 'expected object of scalar type Long but got scalar type Float for argument <span class="hashtag">#2</span> ‘mat2’ ', I am confused and could not solve the problem, please help me!</p>

Nikronic: <p>Hi, can you play add stacktrace error here? Which line is giving you this?</p>

Aya_Uchida: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/a/ad89cab6e9ce0db00377f78f6fb24ad87cdff0e7.png" data-download-href="https://discuss.pytorch.org/uploads/default/ad89cab6e9ce0db00377f78f6fb24ad87cdff0e7" title="UNHE%5D61%7BQX3F2RY5SBMDU%60X"><img src="https://discuss.pytorch.org/uploads/default/original/2X/a/ad89cab6e9ce0db00377f78f6fb24ad87cdff0e7.png" alt="UNHE%5D61%7BQX3F2RY5SBMDU%60X" data-base62-sha1="oLbOQmcGr0tK6NDR6FBkgChXW6j" width="690" height="282" data-dominant-color="131313"><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">UNHE%5D61%7BQX3F2RY5SBMDU%60X</span><span class="informations">1085×445 30.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><br>
It is the traceback. Thank you very much!</p>

Nikronic: <p>If I understand properly, the error is when you call <code>self.gru</code>.<br>
Based on the documentation <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.GRU" rel="nofollow noopener">https://pytorch.org/docs/stable/nn.html#torch.nn.GRU</a>, you have to pass <code>float</code> numbers to <code>GRU</code> object, so you have to remove <code>.long()</code> before passing <code>outputs</code> to <code>self.gru</code>.<br>
By the way, can you test your model by passing <code>hidden_intitial_state</code> like below example in the documentation:</p>
<pre><code class="lang-python">rnn = nn.GRU(10, 20, 2)
input = torch.randn(5, 3, 10)
h0 = torch.randn(2, 3, 20)
output, hn = rnn(input, h0)
</code></pre>
<p>In the end, I tested a simple model and for sure you have to pass <code>float</code> numbers to <code>GRU</code>.<br>
Please let me know the result.</p>

Aya_Uchida: <p>You advice helps a lot. I tested the example in my program and the example ran normally.Then I changed the input into the ‘outputs’ above. The error ‘expected float but long given’ appeared. I removed the ‘outputs = outputs.long()’ and the program ran normally.<br>
This is the code after modifying.</p>
<pre><code class="lang-auto">import sys
import os
sys.path.append(os.getcwd())
import torch
import torch.nn as nn
from config import Config


class FC(nn.Module):
    def __init__(self, opt):
        super(FC, self).__init__()
        self.encoder = nn.Embedding(opt.VOCAB_SIZE, opt.EMBEDDING_DIM)
        self.rnn = nn.GRU(100,128,2)
        self.relu = nn.ReLU(False)
        self.fc_1 = nn.Sequential(
            nn.Linear(12800, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_1*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_1*10, opt.NUM_CLASS_1),
            nn.Dropout(0.5),
        )
        self.fc_2 = nn.Sequential(
            nn.Linear(12800, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_2*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_2*10, opt.NUM_CLASS_2),
            nn.Dropout(0.5),
        )
        self.fc_3 = nn.Sequential(
            nn.Linear(12800, opt.LINER_HID_SIZE),
            #nn.BatchNorm1d(opt.LINER_HID_SIZE),
            nn.ReLU(False),
            nn.Linear(opt.LINER_HID_SIZE, opt.NUM_CLASS_3*10),
            nn.ReLU(False),
            nn.Linear(opt.NUM_CLASS_3*10, opt.NUM_CLASS_3),
            nn.Dropout(0.5),
        )

    def forward(self, x):
        x = x.long()
        outputs = self.encoder(x)
        h0 = torch.randn(2,100,128).cuda()
        outputs,ht = self.rnn(outputs, h0)
        outputs = self.relu(outputs)
        outputs = outputs.view(outputs.size()[0], -1)
        output_1 = self.fc_1(outputs)
        output_2 = self.fc_2(outputs)
        output_3 = self.fc_3(outputs)
        return (output_1, output_2, output_3)


</code></pre>
<p>Thanks very much again!</p>

Nikronic: <p>You’re welcome mate.<br>
But you should be careful about two things:</p>
<ol>
<li>If you want to use your code on GPU, you just need to create an instance of your model like this:</li>
</ol>
<pre><code class="lang-auto">model = FC()
model.cuda()
</code></pre>
<p>I mean, you do not need to call  <code>cuda</code>  for  <code>h0</code>  in your  <code>forward</code>  method.</p>
<ol start="2">
<li>You should not pass  <code>random</code>  values as  <code>h0</code>  to your  <code>GRU</code>  object. If you do not want to initialize it (as before), pass zeros like this:</li>
</ol>
<pre><code class="lang-auto">h0 = torch.zeros(2, 100, 128)
</code></pre>
<p>Good luck</p>

Aya_Uchida: <p>Oh! These are undoubtedly useful ideas. I’ve ignored the settings and I will correct my program.</p>