Skip to content

Commit afe3af7

Browse files
committed
update tutorial
1 parent 24694a8 commit afe3af7

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

docs/source/tutorials/文本分类.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ PS: 基于词进行文本分类
459459
# apply函数将对内部的instance依次执行word_seg操作,并把其返回值放入到raw_words这个field
460460
ds.apply(word_seg, new_field_name='raw_words')
461461
# 除了apply函数,fastNLP还支持apply_field, apply_more(可同时创建多个field)等操作
462+
# 同时我们增加一个seq_len的field
463+
ds.add_seq_len('raw_words')
462464
463465
vocab = Vocabulary()
464466
@@ -500,11 +502,14 @@ PS: 基于词进行文本分类
500502
# | 0 | 15.4寸笔记本的键盘... | ['15.4', '寸', '笔... | [71, 72, 73, 74, ... |
501503
# +--------+-----------------------+-----------------------+----------------------+
502504
505+
# 由于之后需要使用之前定义的BiLSTMMaxPoolCls模型,所以需要将words这个field修改为chars
506+
data_bundle.rename_field('words', 'chars')
507+
503508
我们可以打印一下vocab看一下当前的词表内容
504509

505510
.. code-block:: python
506511
507-
print(data_bundle.get_vocab('words'))
512+
print(data_bundle.get_vocab('chars'))
508513
# Vocabulary([选择, 珠江, 花园, 的, 原因]...)
509514
510515
(3) 选择预训练词向量
@@ -520,7 +525,7 @@ PS: 基于词进行文本分类
520525
521526
from fastNLP.embeddings import StaticEmbedding
522527
523-
word2vec_embed = StaticEmbedding(data_bundle.get_vocab('words'), model_dir_or_name='/path/to/Tencent_AILab_ChineseEmbedding.txt')
528+
word2vec_embed = StaticEmbedding(data_bundle.get_vocab('chars'), model_dir_or_name='/path/to/Tencent_AILab_ChineseEmbedding.txt')
524529
525530
再之后的模型定义与训练过程与上面是一致的,这里就不再赘述了。
526531

0 commit comments

Comments
 (0)