IndexError: list index out of range, NLP BERT Tensorflow

Viewed 71

So I used Bert model trained it and saved it as hdf5 file, but when I try to predict , it shows this error :

IndexError: list index out of range

here is the code

import os.path
import numpy as np
import tensorflow as tf
import ktrain
from ktrain import text

"""## Part 1: Data Preprocessing

### Loading the IMDB dataset
"""

dataset = tf.keras.utils.get_file(fname="aclImdb_v1.tar.gz",
                              
origin="http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz",
                                  extract=True)
IMDB_DATADIR = os.path.join(os.path.dirname(dataset), 'aclImdb')

print(os.path.dirname(dataset))
print(IMDB_DATADIR)

"""### Creating the training and test sets"""

(x_train, y_train), (x_test, y_test), preproc = text.texts_from_folder(datadir=IMDB_DATADIR,
                                                                   classes=['pos','neg'],
                                                                   maxlen=500,
                                                                   train_test_names=['train','test'],
                                                                   preprocess_mode='bert')

"""## Part 2: Building the BERT model"""

model = text.text_classifier(name='bert',
                         train_data=(x_train, y_train),
                         preproc=preproc)

"""## Part 3: Training the BERT model"""

learner = ktrain.get_learner(model=model,
                            train_data=(x_train, y_train),
                            val_data=(x_test, y_test),
                            batch_size=6)

learner.fit_onecycle(lr=2e-5,
                    epochs=1)

tf.keras.models.save_model(model, 'NLP_model.hdf5')

from keras_bert import get_custom_objects

model = tf.keras.models.load_model('NLP_model.hdf5', custom_objects=get_custom_objects())

model.predict('This movie is not the scariest of all time, but it is a great example of a campy 
eighties horror flick -- low budget, no stars, lots of inventive death scenes, and enough nudity to 
keep the teenagers in their seats. The premise is interesting and fun and the three evil kids play 
their parts well. A nice starting point for "Just Say" Julie Brown exposing her talents early in her 
career. This film wont be seen by many, but for fans of 80s horror its a must.ense love would be more 
believable.n.')

i'm trying to predict one of the sentences on the test set.

picture of the full code error

I would appreciate the help, ty

EDIT :

Model Accuracy

1 Answers

As shown in the ktrain tutorials and example notebooks like this one, you need to use the Predictor instance to make predictions on raw text inputs:

# create a Predictor instance
predictor = ktrain.get_predictor(learner.model, preproc)

# make prediction
output = predictor.predict('I loved this movie!')
print(output)

# save Predictor to disk
predictor.save('/tmp/mypredictor')

# reload Predictor from disk
reloaded_predictor = ktrain.load_predictor('/tmp/mypredictor')

# make another prediction
output = reloaded_predictor.predict('I loved this movie!')
print(output)
Related