Defining LSTM Model in Ignite

username: renegadels
cooked: <p>Hello all,</p>
<p>I have a fully functional LSTM neural network in Keras. I found ignite as the closest alternative to Keras for PyTorch(which I have to use instead of Tensorflow due to some requirements), and need to implement exactly the same model in Ignite. I have my training data as a CSV file, and am attaching the code that needs to be converted below.</p>
<pre><code class="lang-auto">import keras 
import pandas as pd
import numpy as np
import csv
from unidecode import unidecode
from keras.preprocessing.text import Tokenizer
def _removeNonAscii(s): 
    return "".join(i for i in s if ord(i)&lt;128)

x_train = []
y_train = []
with open('/Users/anubhav/Desktop/dataset_/final.csv') as f:
    reader = csv.reader(f)
    for row in reader:
        x_train.append(_removeNonAscii(str(row[0])))
        y_train.append(str(row[1]))

tk = Tokenizer()
tk.fit_on_texts(x_train)
xtrain = tk.texts_to_sequences(x_train)

flattened = []
for b in xtrain:
    for t in b:
        flattened.append(t)
flattened
npFlat = np.asarray(flattened)

ytrain = []
for i in range(0, (len(y_train)-1)):
    try:
        ytrain.append(int(y_train[i]))
    except ValueError:
        print("Error", i)

vocabulary_size = len(tk.word_index)+1
print(vocabulary_size)

model = keras.Sequential()
model.add(keras.layers.Embedding(input_dim=vocabulary_size+1,
         output_dim=50))
model.add(keras.layers.LSTM(units=50,return_sequences=True))
model.add(keras.layers.LSTM(units=10))
model.add(keras.layers.Dropout(0.5))
model.add(keras.layers.Dense(8))
model.add(keras.layers.Dense(1, activation="sigmoid"))
model.compile(optimizer='adam', loss='mean_squared_error')

yTRain = np.asarray(ytrain)
trainingReshape = np.zeros(401,)
target = np.append(yTRain, trainingReshape)

model.fit(npFlat, target, epochs=29, batch_size=7, callbacks=[keras.callbacks.EarlyStopping(patience=5)])
</code></pre>
<p>How would I convert this Keras model to Pytorch Ignite? This is really urgent, and help would be much appreciated.</p>

username: vfdev-5
cooked: <p>Hey <a class="mention" href="/u/renegadels">@renegadels</a></p>
<blockquote>
<p>How would I convert this Keras model to Pytorch Ignite?</p>
</blockquote>
<p>Maybe this tutorial can help you :</p><aside class="onebox githubblob" data-onebox-src="https://github.com/pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb">
  <header class="source">

      <a href="https://github.com/pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb" target="_blank" rel="noopener nofollow ugc">github.com</a>
  </header>

  <article class="onebox-body">
    <h4><a href="https://github.com/pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb" target="_blank" rel="noopener nofollow ugc">pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb</a></h4>


      <pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "RfRKTxQO51bK"
   },
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HCS-d1T3znj2"
   },
   "source": [
    "# Convolutional Neural Networks for Sentence Classification using Ignite"
   ]
  },
</code></pre>



  This file has been truncated. <a href="https://github.com/pytorch/ignite/blob/master/examples/notebooks/TextCNN.ipynb" target="_blank" rel="noopener nofollow ugc">show original</a>

  </article>

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

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


username: renegadels
cooked: <p>Thanks a lot <a class="mention" href="/u/vfdev-5">@vfdev-5</a>. Will check it out and get back to you.</p>