505 views
1 1 vote

I have a tensorflow LSTM model for predicting the sentiment. I build the model with the maximum sequence length 150. (Maximum number of words) While making predictions, i have written the code as below:

batchSize = 32
maxSeqLength = 150

def getSentenceMatrix(sentence):
    arr = np.zeros([batchSize, maxSeqLength])
    sentenceMatrix = np.zeros([batchSize,maxSeqLength], dtype='int32')
    cleanedSentence = cleanSentences(sentence)
    cleanedSentence = ' '.join(cleanedSentence.split()[:150])
    split = cleanedSentence.split()
    for indexCounter,word in enumerate(split):
        try:
            sentenceMatrix[0,indexCounter] = wordsList.index(word)
        except ValueError:
            sentenceMatrix[0,indexCounter] = 399999 #Vector for unkown words
    return sentenceMatrix

input_text = "example data"
inputMatrix = getSentenceMatrix(input_text)



In the code i'm truncating my input text to 150 words and ignoring remaining data.Due to this my predictions are wrong.

cleanedSentence = ' '.join(cleanedSentence.split()[:150]) 


I know that if we have lesser length than sequence length we can pad with zero's. What we need to do if we have more length. Can you suggest me the best way to do this. Thanks in advance.

Please log in or register to answer this question.

Related questions

1 1 vote
1 1 answer
3.1k
3.1k views
mllearner000 asked Oct 23, 2018
3,069 views
Hi All,I am writing a simple program using Tensorflow and DNNClassifier. Training Data is 9 pixel with four spectral bands, i.e. 4*9=36 featurs. And each data-point will ...
0 0 votes
1 answers 1 answer
443
443 views
tofighi asked Dec 1, 2025
443 views
Consider a simplified Recurrent Neural Network (RNN) with a single input and a single output. The hidden state is updated using the recurrence:$$ h_t = \text{ReLU}(W_{ih}...
1 1 vote
0 0 answers
584
584 views
Neo asked Nov 12, 2018
584 views
Hello,I have a jpeg, where each image is around 1080 x 2048 in size and I have around 3000 of those images. I want to train a simple NN on this data. However, I am not su...
0 0 votes
0 0 answers
1.0k
1.0k views
Yassine asked Apr 10, 2022
1,005 views
Hello everyone newbie data scientist here.I'm working on a project to predict companies (probability of default) bankruptcy probability and to assign them a credit rating...
1 1 vote
1 1 answer
570
570 views
Gabriel777 asked Oct 27, 2018
570 views
Which of the following feature transformations would be a good choice to transform a categorical variable into a matrix binary feature?A. One-hot-encodingB. Principal Com...