【发布时间】:2021-05-04 13:35:05
【问题描述】:
我刚刚使用 Keras Tokenizer 准备了文本数据
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
VOCAB_SIZE= 10000
tokenizer = Tokenizer(num_words = VOCAB_SIZE)
tokenizer.fit_on_texts(X_train)
X_train_seq = tokenizer.texts_to_sequences(X_train)
X_test_seq = tokenizer.texts_to_sequences(X_test)
知道所有应该成为相同长度的向量以适应神经网络。我应该如何使用 Keras 的 pad_sequences 函数来做到这一点?这会是(不确定maxlen):
X_train_seq _padded = pad_sequences(X_train_seq, maxlen = VOCAB_SIZE)
X_test_seq _padded = pad_sequences(X_test_seq, maxlen = VOCAB_SIZE)
【问题讨论】:
-
相同的序列长度不是强制性的,而是批量优化的好习惯。详情请查看this QnA。希望对您有所帮助。
-
@M.Innat 从中得到,我用错了,应该将其设置为训练数据中最长条目的长度或更短。
-
不需要设置最大的序列长度,而是合理的大小。事实上,它是一种超参数。
标签: python tensorflow keras