How to do inference on new sample

username: Harel_Poms
cooked: <p>Hello, I’m working on a clone of the following project (document classification) in a virtual machine :</p><aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/uvipen/Very-deep-cnn-pytorch">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/uvipen/Very-deep-cnn-pytorch" target="_blank" rel="noopener nofollow ugc">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/9c48b1716de3269417c1f739d63abc7941c825b4f396b828ef0acbef75b174c8/uvipen/Very-deep-cnn-pytorch" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/uvipen/Very-deep-cnn-pytorch" target="_blank" rel="noopener nofollow ugc">GitHub - uvipen/Very-deep-cnn-pytorch: Very deep CNN for text classification</a></h3>

  <p>Very deep CNN for text classification . Contribute to uvipen/Very-deep-cnn-pytorch development by creating an account on GitHub.</p>


  </article>

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

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

<p>I trained the model and created both model files as well as checkpoint files using the following collab notebook (I only use DBpedia and YahooAnswers):</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://colab.research.google.com/drive/17xsJ1oRgQl-m5Re_B6I2-cGxNqAByyYA?usp=sharing">
  <header class="source">
      <img src="https://ssl.gstatic.com/colaboratory-static/common/ac0e6bae38aa2b5a52a4f6e10b43c43d/img/favicon.ico" class="site-icon" width="128" height="128">

      <a href="https://colab.research.google.com/drive/17xsJ1oRgQl-m5Re_B6I2-cGxNqAByyYA?usp=sharing" target="_blank" rel="noopener nofollow ugc">colab.research.google.com</a>
  </header>

  <article class="onebox-body">
    <img width="260" height="260" src="https://colab.research.google.com/img/colab_favicon_256px.png" class="thumbnail onebox-avatar">

<h3><a href="https://colab.research.google.com/drive/17xsJ1oRgQl-m5Re_B6I2-cGxNqAByyYA?usp=sharing" target="_blank" rel="noopener nofollow ugc">Google Colaboratory</a></h3>



  </article>

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

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

<p>I then took the model/checkpoint files which I generated from this collab for use in the project, loaded the model from the checkpoint (for example, with DBPedia):</p>
<p>dbpedia_ckp_path = “./checkpoint/dbpedia_current_checkpoint.pt”<br>
dbpedia_PATH = “./best_model/dbpedia_best_model.pt”</p>
<p>dbpedia_model = VDCNN(n_classes=14, num_embedding=len(“”“abcdefghijklmnopqrstuvwxyz0123456789,;.!?:'"/\|_@#$%^&amp;*~`±=&lt;&gt;()<span class="chcklst-box fa fa-square-o fa-fw"></span>{}”“”) + 1, embedding_dim=16,depth=9, n_fc_neurons=2048, shortcut=False)</p>
<p>dbpedia_model.load_state_dict(torch.load(dbpedia_PATH, map_location=torch.device(‘cpu’))[‘state_dict’])</p>
<p>dbpedia_model.eval()</p>
<p>optimizer = torch.optim.Adam(dbpedia_model.parameters(), lr=0.001)<br>
loaded_dbpedia_model, optimizer, start_epoch, valid_loss_min = load_ckp(dbpedia_ckp_path, dbpedia_model, optimizer)</p>
<p>print("model = ", loaded_dbpedia_model)<br>
print("optimizer = ", optimizer)<br>
print("start_epoch = ", start_epoch)<br>
print("valid_loss_min = ", valid_loss_min)<br>
print(“valid_loss_min = {:.6f}”.format(valid_loss_min))</p>
<p>loaded_dbpedia_model = loaded_dbpedia_model.to(“cpu”)</p>
<p>Now I want to inference on a new sample text and make a prediction, for example:</p>
<p>ex_text_str = “Brekke Church (Norwegian: Brekke kyrkje) is a parish church in Gulen Municipality in Sogn og Fjordane county, Norway. It is located in the village of Brekke. The church is part of the Brekke parish in the Nordhordland deanery in the Diocese of BjÃ¸rgvin. The white, wooden church, which has 390 seats, was consecrated on 19 November 1862 by the local Dean Thomas Erichsen. The architect Christian Henrik Grosch made the designs for the church, which is the third church on the site.”</p>
<p>I’ve tried following this article in order to classify it (using its predict method):<br>
link 3 in the picture</p>
<p>and using this chunk of code:</p>
<p>dbpedia_train_iter = DBpedia(split=‘train’)<br>
dbpedia_tokens = yield_tokens(dbpedia_train_iter)<br>
vocab = build_vocab_from_iterator(dbpedia_tokens, specials=[“”])<br>
vocab.set_default_index(vocab[“”])<br>
text_pipeline = lambda x: vocab(tokenizer(x))</p>
<p>But on one vm I get the following error : RuntimeError: Internal error: headers don’t contain content-disposition.</p>
<p>On another vm, where the error does not happen, I get another error:<br>
index out of range in self (in torch.embedding(…))</p>
<p>So I tried following another article to classify the new sample for DBpedia:<br>
link 4 in the picture<br>
but it seems to be outdated and so I didnt make any progress either.</p>
<p>In short, I’d like to write code to inference a new text sample, and thus print its classification for the user, but all my attempts so far didnt amount to anything. If succesful, the result for the above sample should be<br>
This text belongs to NaturalPlace class</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/3/833b140e18b436afe679133103729b6660d8c03a.png" data-download-href="https://discuss.pytorch.org/uploads/default/833b140e18b436afe679133103729b6660d8c03a" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/3/833b140e18b436afe679133103729b6660d8c03a.png" alt="image" data-base62-sha1="iIV7EiZfc3ZSeFRdeHYArYq5fKy" width="690" height="68" data-dominant-color="D7D7DC"><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">image</span><span class="informations">709×70 4.49 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>Any help would be appreciated.</p>
<p>~Regards Harel.</p>