How I can create a prediction on Pytorch LSTM?

Sam_Shamsan: <p>I am building a multi label classifier for text using LSTM. I am using this snippet of code to train the model. How I can create a prediction on my test data to send it for submission:</p>
<h1></h1>
<p>import torch.nn.functional as F<br>
def train(model, train_loader, val_loader, epochs, optimizer, loss_fn, device):<br>
train_loss = 0<br>
val_loss = 0<br>
for epoch in range(1, epochs + 1):<br>
model.train()<br>
for batch in train_loader:<br>
batch_X, batch_y = batch</p>
<pre><code>        batch_X = batch_X.to(device)
        batch_y = batch_y.to(device)
        # batch_X = F.sigmoid(batch_X)
        batch_X= F.relu(batch_X)
        model.zero_grad()
        optimizer.zero_grad()
        
        #batch_len = batch_X.t()[0,:]
        #print(batch_len)
        
        # TODO: Complete this train method to train the model provided.
        output = model.forward(batch_X)
        # print(output)
        
        #_, batch_y = batch_y.max(dim=1)            
        batch_y = torch.autograd.Variable(batch_y)
        # batch_y = batch_y.reshape(-1,1)
     
        # output = output.view(output.size(0), batch_y.size(1))
        
        loss = loss_fn(output, batch_y)
        loss.backward()
        optimizer.step()
        
        train_loss += loss.item()
        #print(loss.item())
        #break
    model.eval()
    for batch in val_loader:
        batch_X, batch_y = batch
        
        batch_X = batch_X.to(device)
        batch_y = batch_y.to(device)
        
        # model.zero_grad()
        #optimizer.zero_grad()
        # TODO: Complete this train method to train the model provided.
        output = model.forward(batch_X)
        
        #_, batch_y = batch_y.max(dim=1)            
        batch_y = torch.autograd.Variable(batch_y)
        # batch_y = batch_y.reshape(-1,1)
        #print(output)
        
        loss = loss_fn(output, batch_y)            
        val_loss += loss.item()
        #break
    
    total_train_loss = train_loss / len(train_loader)
    total_val_loss = val_loss / len(val_loader)
    
    print("Epoch: {}, BCE Train Loss: {} Valid Loss {}".format(epoch, total_train_loss, total_val_loss))
    val_loss = 0
    train_loss = 0
    #break

return model
</code></pre>
<p>model = LSTMClassifier(embedding_dim, hidden_dim, vocab_size).to(device)<br>
optimizer = optim.Adam(model.parameters(), lr=learning_rate)<br>
loss_fn = torch.nn.BCELoss()</p>
<p>model_tn = train(model, train_sample_dl, var_sample_dl, num_epochs, optimizer, loss_fn, device)</p>
<h1></h1>

Sam_Shamsan: <p>And this is how am reading the testing data: <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c96a349022d455b1ba1ea7c77fb78b836c78282a.png" data-download-href="https://discuss.pytorch.org/uploads/default/c96a349022d455b1ba1ea7c77fb78b836c78282a" title="Screen Shot 2020-11-19 at 9.49.54 AM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c96a349022d455b1ba1ea7c77fb78b836c78282a_2_690x249.png" alt="Screen Shot 2020-11-19 at 9.49.54 AM" data-base62-sha1="sJNvXgB6mMPPXzRlCCnMmu54ccy" width="690" height="249" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c96a349022d455b1ba1ea7c77fb78b836c78282a_2_690x249.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c96a349022d455b1ba1ea7c77fb78b836c78282a_2_1035x373.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c96a349022d455b1ba1ea7c77fb78b836c78282a_2_1380x498.png 2x" data-dominant-color="F3F3F3"><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">Screen Shot 2020-11-19 at 9.49.54 AM</span><span class="informations">2254×816 94 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>

Abhilash_Srivastava: <aside class="quote no-group" data-username="Sam_Shamsan" data-post="1" data-topic="103417">
<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/sam_shamsan/48/31160_2.png" class="avatar"> Sam_Shamsan:</div>
<blockquote>
<p><code>      batch_X= F.relu(batch_X)</code></p>
</blockquote>
</aside>
<p>You shouldn’t b using relu here.</p>
<aside class="quote no-group" data-username="Sam_Shamsan" data-post="1" data-topic="103417">
<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/sam_shamsan/48/31160_2.png" class="avatar"> Sam_Shamsan:</div>
<blockquote>
<p><code>        model.zero_grad()</code></p>
</blockquote>
</aside>
<p>this shouldn’t be required.</p>
<aside class="quote no-group" data-username="Sam_Shamsan" data-post="1" data-topic="103417">
<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/sam_shamsan/48/31160_2.png" class="avatar"> Sam_Shamsan:</div>
<blockquote>
<p><code>        output = model.forward(batch_X)</code></p>
</blockquote>
</aside>
<p>Instead use <code>output = model(batch_X)</code></p>
<aside class="quote no-group" data-username="Sam_Shamsan" data-post="1" data-topic="103417">
<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/sam_shamsan/48/31160_2.png" class="avatar"> Sam_Shamsan:</div>
<blockquote>
<p><code>        batch_y = torch.autograd.Variable(batch_y)</code></p>
</blockquote>
</aside>
<p>This may not be required.</p>
<aside class="quote no-group" data-username="Sam_Shamsan" data-post="1" data-topic="103417">
<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/sam_shamsan/48/31160_2.png" class="avatar"> Sam_Shamsan:</div>
<blockquote>
<p>loss_fn = torch.nn.BCELoss()</p>
</blockquote>
</aside>
<p>You might need <code>torch.nn.BCEWithLogitsLoss()</code> depending on what the output of your <code>model</code> is like.</p>

Sam_Shamsan: <p>Thank you so much, I reconstruct the code as you recommended. now I have this issue. any suggestions.</p>
<p>ValueError: y contains previously unseen labels: [0.5        0.50008935]</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/e/8e9c16c9fc173030811631d91111bcae64d5c827.png" data-download-href="https://discuss.pytorch.org/uploads/default/8e9c16c9fc173030811631d91111bcae64d5c827" title="Screen Shot 2020-11-19 at 3.31.35 PM"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e9c16c9fc173030811631d91111bcae64d5c827_2_690x435.png" alt="Screen Shot 2020-11-19 at 3.31.35 PM" data-base62-sha1="klAdDCMfpkgKpP4hek21hPFXQWP" width="690" height="435" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e9c16c9fc173030811631d91111bcae64d5c827_2_690x435.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e9c16c9fc173030811631d91111bcae64d5c827_2_1035x652.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/e/8e9c16c9fc173030811631d91111bcae64d5c827_2_1380x870.png 2x" data-dominant-color="F2F1F3"><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">Screen Shot 2020-11-19 at 3.31.35 PM</span><span class="informations">2432×1536 547 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>
<p>need to have the return as the label indices like this<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/f/0/f0b419215c02fa2f325d285ce331c7d2a6d6090a.png" alt="Screen Shot 2020-11-19 at 3.35.10 PM" data-base62-sha1="ylmrZmxaUDgEw6apwT046zgUaHo" width="658" height="146"></p>

Abhilash_Srivastava: <p>This is not related to your model or Pytorch, this is sklearn error.<br>
Are you referring to some codebase?</p>