文本分类和词向量训练工具fastText的参数和用法

2023-05-16

fastText的参数和用法

fastText由Facebook开源,主要基于fasttext这篇文章的思路paper,主要用于两个任务:训练词向量和文本分类。

下载地址与document :fasttext官网

在这里插入图片描述

fasttext的 主要功能:

Training Supervised Classifier [supervised] Supervised Classifier Training for Text Classification. 训练分类器,就是文本分类,fasttext 的主营业务。

Training SkipGram Model [skipgram] Learning Word Representations/Word Vectors using skipgram technique. 训练skipgram的方式的词向量。

Quantization [quantize] Quantization is a process applied on a model so as to reduce the memory usage during prediction. 量化压缩,降低模型体积。

Predictions [predict] Predicting labels for a given text : Text Classification. 对于文本分类任务,用于预测类别。

Predictions with Probabilities [predict-prob] Predicting probabilities in addition to labels for a given text : Text Classification. 带有概率的预测类别。

Training of CBOW model [cbow] Learning Word Representations/Word Vectors using CBOW (Continuous Bag Of Words) technique. cbow方式训练词向量。

Print Word Vectors [print-word-vectors] Printing of Word Vectors for a trained model with each line representing a word vector. 打印一个单词的词向量。

Print Sentence Vectors [print-sentence-vectors] Printing of Sentence Vectors for a trained model with each line representing a vector for a paragraph. 打印文本向量,每个文本的向量长度是一样的,代表所有单词的综合特征。

Query Nearest Neighbors [nn] 找到某个单词的近邻。

Query for Analogies [analogies] 找到某个单词的类比词,比如 A - B + C。柏林 - 德国 + 法国 = 巴黎 这类的东西。

命令行的fasttext使用:

1 基于自己的语料训练word2vec

fasttext skipgram -input xxxcorpus -output xxxmodel

训练得到两个文件:xxxmodel.bin 和 xxxmodel.vec,分别是模型文件和词向量形式的模型文件

参数可选 skipgram 或者 cbow,分别对应SG和CBOW模型。

2 根据训练好的model查看某个词的neighbor

fasttext nn xxxmodel.bin

Query word? 后输入单词,即可获得其近邻单词。

3 其它的一些参数:

-minn 和 -maxn :subwords的长度范围,default是3和6
-epoch 和 -lr :轮数和学习率,default是5和0.05
-dim:词向量的维度,越大越🐮🍺,但是会占据更多内存,并降低计算速度。
-thread:运行的线程数,不解释。

python 模块的应用方式:

参数含义与功能基本相同,用法如下。

给一个栗子:

def train_word_vector(train_fname, test_fname, epoch, lr, save_model_fname, thr):
    """
    train text classification, and save model
    """
    dim = 500               # size of word vectors [100]
    ws = 5                # size of the context window [5]
    minCount = 500          # minimal number of word occurences [1]
    minCountLabel = 1     # minimal number of label occurences [1]
    minn = 1              # min length of char ngram [0]
    maxn = 2              # max length of char ngram [0]
    neg = 5               # number of negatives sampled [5]
    wordNgrams = 2        # max length of word ngram [1]
    loss = 'softmax'              # loss function {ns, hs, softmax, ova} [softmax]
    lrUpdateRate = 100      # change the rate of updates for the learning rate [100]
    t = 0.0001                 # sampling threshold [0.0001]
    label = '__label__'             # label prefix ['__label__']

    model = fasttext.train_supervised(train_fname, lr=lr, epoch=epoch, dim=dim, ws=ws, 
                                        minCount=minCount, minCountLabel=minCountLabel,
                                        minn=minn, maxn=maxn, neg=neg, 
                                        wordNgrams=wordNgrams, loss=loss,
                                        lrUpdateRate=lrUpdateRate,
                                        t=t, label=label, verbose=True)
    model.save_model(save_model_fname)

    return model

if __name__ == "__main__":
    """ param settings """
    model = train_word_vector(train_fname, test_fname,
                              epoch, lr, save_model_fname, thr)
    model.get_nearest_neighbors(some_word)
    model.predict('sentence') # 得到输出类别
    model.test(filename) # 输出三元组,(样本数量, acc, acc) 这里的acc是对二分类来说的

无监督学习词向量和有监督训练文本分类的 API如下:

train_unsupervised parameters

input # training file path (required)
model # unsupervised fasttext model {cbow, skipgram} [skipgram]
lr # learning rate [0.05]
dim # size of word vectors [100]
ws # size of the context window [5]
epoch # number of epochs [5]
minCount # minimal number of word occurences [5]
minn # min length of char ngram [3]
maxn # max length of char ngram [6]
neg # number of negatives sampled [5]
wordNgrams # max length of word ngram [1]
loss # loss function {ns, hs, softmax, ova} [ns]
bucket # number of buckets [2000000]
thread # number of threads [number of cpus]
lrUpdateRate # change the rate of updates for the learning rate [100]
t # sampling threshold [0.0001]
verbose # verbose [2]

train_supervised parameters

input # training file path (required)
lr # learning rate [0.1]
dim # size of word vectors [100]
ws # size of the context window [5]
epoch # number of epochs [5]
minCount # minimal number of word occurences [1]
minCountLabel # minimal number of label occurences [1]
minn # min length of char ngram [0]
maxn # max length of char ngram [0]
neg # number of negatives sampled [5]
wordNgrams # max length of word ngram [1]
loss # loss function {ns, hs, softmax, ova} [softmax]
bucket # number of buckets [2000000]
thread # number of threads [number of cpus]
lrUpdateRate # change the rate of updates for the learning rate [100]
t # sampling threshold [0.0001]
label # label prefix [’_label_’]
verbose # verbose [2]
pretrainedVectors # pretrained word vectors (.vec file) for supervised learning []

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

文本分类和词向量训练工具fastText的参数和用法 的相关文章

  • VSCode C++代码提示和补全

    网上都说安装完扩展C C 43 43 Extension就可以了 xff0c 但我这儿不行 xff0c 发现是因为需要自己另外提供头文件 xff0c 于是利用了mingw的头文件 1 解压i686 4 9 3 release posix d
  • vs 写c++一个类的头文件放在改工程下的一个子目录里,include出错

    工程名test 类名http 子目录 xff1a http 在主源文件中include这个http 的头文件时 xff0c include 34 http http h 34 编译出错 一定要改成绝对路径 include 34 home m
  • STM32 FreeRTOS堆、栈以及系统堆栈

    stm32以及freertos 堆栈解析 https blog csdn net sinat 36568888 article details 124320985 1 当freertos采用heap 4内存分配方案时 xff0c stm32
  • 在linux下如何编译c/c++代码(库)

    目录 在linux下如何编译c c 43 43 代码 库 1 运行环境 1 学知资料 2 Linux下静态库和动态库的区别 2 基本文件类型 2 c c 43 43 程序编译使用基本流程 2 gcc和g 43 43 区别 3 gcc基本使用
  • Ubuntu系统用火狐浏览器无法上网问题的解决方法

    刚做的Ubuntu系统用火狐浏览器无法上网问题的解决方法 首先打开终端 xff0c 输入 xff1a sudo apt get update 等待出现完成字样 其次再输入 xff1a sudo apt get install firefox
  • Raspberry Pi2/3引脚介绍

    引脚图 Raspberry Pi2 3引脚如下图所示 xff1a Raspberry Pi 3 的硬件接口通过开发板上的 40 排针 J8 公开 功能包括 xff1a 17x GPIO 引脚1x SPI 总线1x I2C 总线2x 5V 电
  • robot_pose_ekf 使用说明

    协方差参数的设置 主要确定mpu6050和odom编码器协方差参数的设置 参考 xff1a turtlebot node协方差的设置 mpu605参数的设置 参考 xff1a https github com Arkapravo turtl
  • 互斥锁、读写锁 、 自旋锁和RCU锁

    基础知识思考整理 http blog csdn net aganlengzi article details 50996227 互斥锁 mutex xff1a 在访问共享资源之前对进行加锁操作 xff0c 在访问完成之后进行解锁操作 加锁后
  • Dashgo D1概述

    概述 Dashgo D1是深圳EAI科技专门针对ROS开发的移动平台 xff0c 自主研发的核心结构保证了载重大 动力足 续航长和扩展性强的性能 xff0c 深受创客 科研 企业的欢迎 主要特性 xff1a 易于使用 由整机及其附件组成 x
  • 搭建Dashgo运行环境

    安装 设置用户的串口读取权限 span class hljs built in sudo span usermod span class hljs operator a span G dialout your user name your
  • 超声波避障

    运行如下脚本 roslaunch dashgo bringup bringup smoother ob span class hljs preprocessor launch span 在另一个终端运行 rostopic span clas
  • 虚拟机VirtualBox安装Ubuntu14.04

    本教程的运行环境 xff1a Windows 7 虚拟机 VirtualBox xff0c Ubuntu 14 04 1 准备 下载 VirtualBox5 0版可以使用后面提供的OVA镜像直接导入 xff0c 镜像是ROS的集合环境 xf
  • 树莓派实现自主导航

    使用 Flash Lidar F4 激光雷达进行自主导航 树莓派的IP假设为192 168 11 100 该教程基于地图已经建好并保存的情况下 teb amcl demo launch 的 args 参数要与前面 gmapping demo
  • 键盘控制移动

    PathGo 导航模块的默认固定 IP 是 192 168 31 200 xff0c 默认用户名为eaibot 默认密码为 eaibot 1 不带陀螺仪的底盘驱动 打开一个终端 xff0c 运行以下命令 xff0c 启动底盘驱动 带平滑加减
  • Dashgo-D1 不带陀螺仪的建图导航

    PathGo 导航模块的默认固定 IP 是 192 168 31 200 xff0c 默认用户名为 eaibot xff0c 默认密码为 eaibot D1与F4的坐标系已经校准正确的情况下 扫描建图 打开一个终端 xff0c ssh登录导
  • Dashgo-D1 带陀螺仪的建图导航

    PathGo 导航模块的默认固定 IP 是 192 168 31 200 xff0c 默认用户名为 eaibot 默认密码为 eaibot D1与F4的坐标系已经校准正确的情况下 扫描建图 打开一个终端 xff0c ssh登录导航模块并启动
  • Dashgo-D1 多点连续导航

    PathGo 导航模块的默认固定 IP 是 192 168 31 200 xff0c 默认用户名为 eaibot xff0c 默认密码为 eaibot 地图已经建好并引用的情况下 第一种方式 打开一个终端 xff0c ssh登录导航模块 x
  • private static final long serialVersionUID = 1L

    很显然这行代码的意思是将SerialVersionUID的值定义为一个常量 xff0c 那这是干什么的呢 xff1f 解决这个问题 xff0c 首先要了解包含SerialVersionUID的Serializable接口是什么 xff1f
  • 【善用工具】程序性能分析Gperftools初探(libwind+pprof+Kcachegrind)

    善用工具 http blog csdn net aganlengzi article details 62893533 本文主要试用的gperftool相关介绍可见 https github com gperftools gperftool
  • PowerShell文本文件、Json文件资源的读取,修改

    PowerShell文件资源的读写 文本类文件的读取 xff1a Path 61 需要读取的文件路径 不加 Raw为逐行读取 xff0c 添加则为整个文件资源的读取 data 61 Get Content Path Path data 61

随机推荐