'ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() in for loop

So I'm trying to make my first A.I project a Language Translation program using Natural Language Processing and there seems to be an error and I don't know how to solve it.

preds = model.predict(testX.reshape((testX.shape[0],testX.shape[1])))

These predictions are sequences of integers. We need to convert these integers to their corresponding words. So I defined a function to do this:

def get_word(n, tokenizer):
      for word, index in tokenizer.word_index.items():
          if index == n:
              return word
      return None

Then this code will Convert predictions into text.

#convert predictions into text (english)
preds_text = []
for i in preds:
       temp = []
       for j in range(len(i)):
            t = get_word(i[j], eng_tokenizer)
            if j > 0:
                if (t == get_word(i[j-1], eng_tokenizer)) or (t == None):
                     temp.append('')
                else:
                     temp.append(t)
            else:
                   if(t == None):
                          temp.append('')
                   else:
                          temp.append(t) 

       preds_text.append(' '.join(temp))

Here is the error,

ValueError                                Traceback (most recent call last)
d:\ai\colab (1).ipynb Cell 22' in <cell line: 2>()
      3 temp = []
      4 for j in range(len(i)):
----> 5      t = get_word(i[j], eng_tokenizer)
      6      if j > 0:
      7          if (t == get_word(i[j-1], eng_tokenizer)) or (t == None):

And here is the source code where you can find everything you need. Source Code



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source