Search code examples
pythonpandaskerasnlpartificial-intelligence

How to solve this attribute error in python?


def encode_sequences(tokenizer, length , lines):
  seq = tokenizer.text_to_sequences(lines)
  seq = pad_sequences(seq, maxlen = length, padding = 'posts')
  return seq

from sklearn.model_selection import train_test_split
train , test = train_test_split(deu_eng , test_size = 0.2, random_state = 12)

trainX = encode_sequences(deu_tokenizer, deu_length, train[:,1])
trainY = encode_sequences(eng_tokenizer, eng_length, train[:,0])

testX = encode_sequences(deu_tokenizer, deu_length, test[:,1])
testY = encode_sequences(eng_tokenizer, eng_length, test[:,0])
error :-
AttributeError                            Traceback (most recent call last)
<ipython-input-29-0cb789025947> in <module>()
      7 train , test = train_test_split(deu_eng , test_size = 0.2, random_state = 12)
      8 
----> 9 trainX = encode_sequences(deu_tokenizer, deu_length, train[:,1])
     10 trainY = encode_sequences(eng_tokenizer, eng_length, train[:,0])
     11 

<ipython-input-29-0cb789025947> in encode_sequences(tokenizer, length, lines)
      1 #model
      2 def encode_sequences(tokenizer, length , lines):
----> 3   seq = tokenizer.text_to_sequences(lines)
      4   seq = pad_sequences(seq, maxlen = length, padding = 'posts')
      5   return seq

AttributeError: 'Tokenizer' object has no attribute 'text_to_sequences'

Solution

  • I believe you have mispelled the texts_to_sequences attribute. The error states that it cannot find the text_to_sequences method in the line:

    seq = tokenizer.text_to_sequences(lines)
    

    From the documentation, you can see that this method should be spelled:

    seq = tokenizer.texts_to_sequences(lines)
    

    This should resolve the error.