为什么Keras pad_sequences在同一代码中不能两次工作?

时间:2019-05-02 14:51:04

标签: python keras

我正在尝试使用Keras预处理某些文本,我需要多次使用keras.preprocessing.sequence.pad_sequences()函数,但是似乎不允许这样做。有没有办法解决此错误并多次使用该功能?

import numpy
from keras.preprocessing.sequence import pad_sequences

example_sequences = [[1,2,3,4],[1,5,4],[2,3]]
MAX_SEQUENCE_LENGTH = 3

pad_sequences = pad_sequences(example_sequences, maxlen=MAX_SEQUENCE_LENGTH)
print(pad_sequences)

sequences2 = [[1,2,3,4],[1,5,4],[2,3]]
MAX_SEQUENCE_LENGTH2 = 3

pad_sequences2 = pad_sequences(sequences2, maxlen=MAX_SEQUENCE_LENGTH2)

输出:

Using TensorFlow backend.
[[2 3 4]
 [1 5 4]
 [0 2 3]]
Traceback (most recent call last):
  File "C:\Users\test.py", line 47, in <module>
    pad_sequences2 = pad_sequences(sequences2, maxlen=MAX_SEQUENCE_LENGTH2)
TypeError: 'numpy.ndarray' object is not callable

如您所见,第一次调用该函数会生成一个矩阵,但是第二次调用该函数,我会得到一个错误,即numpy.ndarray无法调用。请注意,pad_sequences函数的输入是相同的,并且没有输入是numpy数组。那么,为什么不能从该函数获得相同的输出?如何解决该问题,以便可以多次使用pad_sequences函数?

1 个答案:

答案 0 :(得分:0)

您正在将第一个pad_sequences的输出分配给名为pad_sequences的变量:

pad_sequences = pad_sequences(example_sequences, maxlen=MAX_SEQUENCE_LENGTH)
print(pad_sequences)  # "pad_sequences" is now a Numpy array!

因此,当您到达pad_sequences的第二个“调用”时,它实际上是一个Numpy数组:

pad_sequences2 = pad_sequences(sequences2, maxlen=MAX_SEQUENCE_LENGTH2)

只需重命名输出变量,例如

padded_sequences = pad_sequences(example_sequences, maxlen=MAX_SEQUENCE_LENGTH)
print(padded_sequences)