Torchtext error: ValueError: invalid literal for int() with base 10: 'Sentiment'

username: jinudaniel
cooked: <p>I was trying to create a classifier model using the Amazon fine food reviews dataset that can be obtained from <a href="https://www.kaggle.com/snap/amazon-fine-food-reviews" rel="nofollow noopener">Kaggle</a>. But the training of the LSTM model is failing with the below error</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-40-f8a71793cefa&gt; in &lt;module&gt;
      1 training(model=model, epoch=2, eval_every=1000,
      2          loss_func=loss_function, optimizer=optimizer, train_iter=train_iter,
----&gt; 3         val_iter=val_iter)

&lt;ipython-input-39-fe1648a5a8c7&gt; in training(epoch, model, eval_every, loss_func, optimizer, train_iter, val_iter, early_stop, warmup_epoch)
     11     for e in range(epoch):
     12         train_iter.init_epoch()
---&gt; 13         for train_batch in iter(train_iter):
     14             step += 1
     15             model.train()

/opt/anaconda3/lib/python3.7/site-packages/torchtext/data/iterator.py in __iter__(self)
    155                     else:
    156                         minibatch.sort(key=self.sort_key, reverse=True)
--&gt; 157                 yield Batch(minibatch, self.dataset, self.device)
    158             if not self.repeat:
    159                 return

/opt/anaconda3/lib/python3.7/site-packages/torchtext/data/batch.py in __init__(self, data, dataset, device)
     32                 if field is not None:
     33                     batch = [getattr(x, name) for x in data]
---&gt; 34                     setattr(self, name, field.process(batch, device=device))
     35 
     36     @classmethod

/opt/anaconda3/lib/python3.7/site-packages/torchtext/data/field.py in process(self, batch, device)
    199         """
    200         padded = self.pad(batch)
--&gt; 201         tensor = self.numericalize(padded, device=device)
    202         return tensor
    203 

/opt/anaconda3/lib/python3.7/site-packages/torchtext/data/field.py in numericalize(self, arr, device)
    317             if not self.sequential:
    318                 arr = [numericalization_func(x) if isinstance(x, six.string_types)
--&gt; 319                        else x for x in arr]
    320             if self.postprocessing is not None:
    321                 arr = self.postprocessing(arr, None)

/opt/anaconda3/lib/python3.7/site-packages/torchtext/data/field.py in &lt;listcomp&gt;(.0)
    317             if not self.sequential:
    318                 arr = [numericalization_func(x) if isinstance(x, six.string_types)
--&gt; 319                        else x for x in arr]
    320             if self.postprocessing is not None:
    321                 arr = self.postprocessing(arr, None)

ValueError: invalid literal for int() with base 10: 'Sentiment'
</code></pre>
<p>The code can be found at GitHub<br>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/jinudaniel/amazon-fine-food-reviews/blob/master/amazon_reviews_lstm_pytorch.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/jinudaniel/amazon-fine-food-reviews/blob/master/amazon_reviews_lstm_pytorch.ipynb" target="_blank" rel="nofollow noopener">jinudaniel/amazon-fine-food-reviews/blob/master/amazon_reviews_lstm_pytorch.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Amazon Reviews using Pytorch and TorchText"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import random\n",
    "import torchtext\n",
</code></pre>

  This file has been truncated. <a href="https://github.com/jinudaniel/amazon-fine-food-reviews/blob/master/amazon_reviews_lstm_pytorch.ipynb" target="_blank" rel="nofollow noopener">show original</a>

  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>
<br>
Any help would be appreciated. Thanks.</p>

username: warrenfelsh
cooked: <p>The error message invalid literal for int() with base 10 would seem to indicate that you are passing a string that’s not an integer to the int() function . In other words it’s either empty, or has a character in it other than a digit.</p>
<p>You can solve this error by using Python isdigit() method to check whether the value is number or not. The returns True if all the characters are digits, otherwise False .</p>
<p><code>if val.isdigit():</code></p>
<p>The other way to overcome this issue is to wrap your code inside a Python try…except block to handle this error.</p>
<p><strong>Python2.x and Python3.x</strong></p>
<p>Sometimes the difference between Python2.x and Python3.x that leads to this ValueError: <a href="http://net-informations.com/python/err/int.htm" rel="nofollow noopener">invalid literal for int() with base 10</a> .</p>
<p>With Python2.x , int(str(3/2)) gives you “1”. With Python3.x , the same gives you (“1.5”): ValueError: invalid literal for int() with base 10: “1.5”.</p>