tensorflow中tokenizer的相关知识

it2026-08-08  2

参考

tokenizer = Tokenizer(num_words=max_words) # 只考虑最常见的前max_words个词 tokenizer.fit_on_texts(texts) # 使用一系列文档来生成token词典,texts为list类,每个元素为一个文档 sequences = tokenizer.texts_to_sequences(texts) # 将多个文档转换为word下标的向量形式,shape为[len(texts)len(text)] -- (文档数,每条文档的长度) word_index = tokenizer.word_index # word_index 一个dict,保存所有word对应的编号id,从1开始 print('Found %s unique tokens.' % len(word_index)) data = pad_sequences(sequences, maxlen=maxlen) # 返回的是个2维张量,长度为maxlen,只关注前maxlen个单词 labels = np.asarray(labels) print('shape of data: ', data.shape) print('shape of labels: ', labels.shape) # 该语料的测试集和训练集的样本数都是25000个。
最新回复(0)