为什么会发生“TypeError: 'numpy.ndarray' object is not callable”以及如何解决?

Y4RD13

我目前正在从 tensorflow 系列学习机器学习。在这种情况下是关于文本分类的。正如您在代码中看到的,我已经训练了模型并将其保存为文件以进行测试。

加载保存的文件

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

编码功能:

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

预处理:

  1. 该文件是一个大字符串,但我需要将其转换为编码的数字列表
  2. 文本的大小最多只有 256 个字,因为这是我在训练数据时使用的方式
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

预期输出:将预测打印为 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
安吉特班萨尔

您已经使用predict = model.predict_classes(encode)which 覆盖了 predict 的任何函数定义并用数组替换。

所以 Predict 是一个不可调用的数组。如果您只是为了查看预测的类,encode[0]可以使用:print(predict[0])

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

'TypeError: 'numpy.ndarray' object is not callable' 是什么意思?

计算热指数时如何解决 Metpy ('numpy.ndarray' object has no attribute 'to') 错误?

如何解决 unhashable 类型:numpy ndarray

我如何解决这个 TypeError: 'int' object is not callable?

Python:如何解决“TypeError:'str' object is not callable”

TypeError:无法转换numpy.object_类型的np.ndarray

为什么我用numpy memmap收到OverflowError和WindowsError以及如何解决?

`numpy.piecewise` 丢弃函数的虚部。为什么,以及如何解决?

为什么pylint为numpy.ndarray.shape返回`unsubscriptable-object`?

如何扩展numpy.ndarray

TypeError:无法散列的类型:'numpy.ndarray'-如何在执行query()时解决此错误

为什么numpy.ndarray.T比numpy.transpose(numpy.ndarray)快得多?

如何解决不可散列类型的问题:“ numpy.ndarray”

OpenMDAO:如何解决“ numpy.ndarray”对象没有属性“ log”错误

调试器舍入浮点数...为什么会发生这种情况,以及如何解决呢?

为什么“ numpy.ndarray.view”会忽略先前对“ numpy.ndarray.newbyteorder”的调用?

如何使用列的值在ndarray上合并2 numpy ndarray?

numpy ndarray形状有什么作用?

Pytorch无法转换numpy.object类型的np.ndarray

如何从numpy ndarray删除特定值

“from numpy import ndarray”是如何工作的?

如何在 numpy ndarray 中保留值

如何过滤numpy ndarray中的列

如何在numpy ndArray中插入值?

如何增加'numpy.ndarray'矩阵

如何向下转换numpy.ndarray

如何获取numpy.ndarray的索引

如何堆叠多个图像numpy ndarray

如何减少numpy.ndarray的形状?