Defining LSTM Model in Ignite

Hello all,

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.

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)<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)])

How would I convert this Keras model to Pytorch Ignite? This is really urgent, and help would be much appreciated.

Hey @renegadels

How would I convert this Keras model to Pytorch Ignite?

Maybe this tutorial can help you :

1 Like

Thanks a lot @vfdev-5. Will check it out and get back to you.

1 Like