为什么会出现 "TypeError: 'numpy.ndarray' object is not callable",如何解决这个问题?

1 人不认可

我目前正在从tensorflow系列学习ML。在这种情况下,是关于文本分类的。正如你在代码中所看到的,我已经训练好了模型,并将其保存为一个文件来测试它。

加载已保存的文件

modelFile = keras.models.load_model('model_text_classification.h5')

Function to encode:

def review_encode(string):
  '''look up the mapping of all the words and return to us an encoded list'''
  encoded = [1] # start with 1 as a starting tag as the system with word_index['<START>'] = 1
  for word in string:
    if word in word_index:
      encoded.append(word_index[word.lower()])
    else:
      encoded.append(2) # as the END tag
  return encoded

Pre-processing:

  • the file is a large string, but I need to convert this into an encoded list of numbers
  • the size of the text is only at max 256 words, because that's how I was using when I trained the data
  • with open('lion_king.txt', encoding = 'utf-8') as f:
      for line in f.readlines():
        nline = line.replace(',', '').replace('.', '').replace('(', '').replace(')', '').replace('\"', '').replace(':', '')
        nline = nline.split(' ')
        # encode and trim the data down to 256 words
        encode = review_encode(nline)
        encode = keras.preprocessing.sequence.pad_sequences([encode], value = word_index['<PAD>'], padding = 'post', maxlen = 256) # [encode], because is expecting a list of lists
        # using the model to make a prediction
        predict = model.predict_classes(encode)
        print(line)
        print(encode)
        print(predict(encode[0])) #HERE IS ERROR
    

    Expecting output: 打印预测结果为96%的正面。
    例子。 [0.9655667]

    完整的追踪记录。

    TypeError                                 Traceback (most recent call last)
    <ipython-input-58-790c338a89ce> in <module>()
         13     print(line)
         14     print(encode)
    ---> 15     print(predict(encode[0]))
    TypeError: 'numpy.ndarray' object is not callable