FastText 和 Faiss 的初探了解

概览

大模型目前已经是如火如荼的程度,各个大厂都有推出面向大众的基础大模型,同时诸多行业也有在训练专有大模型,而大模型的发展由来却是经过多年从文本检索生成、深度学习、自然语言处理,在Transformer架构出来后,才有了爆发性的发展,今天我们来探索使用下文本解析、词向量方向的事情!


语言模型的演进之路

基于概率的词预测---》 基于向量---》神经网络--》编码解码器架构---》注意力机制---》Transformer

介绍

  • FastText:由Facebook开源,用于高效学习词语表示和句子分类的组件库。
  • Faiss:由Facebook开源的一个向量数据库,支持开发人员快速搜索彼此相似的多媒体文档的嵌入。它解决了针对基于哈希的搜索进行优化的传统查询搜索引擎的局限性,并提供了更具可扩展性的相似性搜索功能。

FastText

Install

bash 复制代码
git clone https://github.com/facebookresearch/fastText.git
$ cd fastText
$ make

这将为所有类以及主二进制文件生成目标文件fasttext.

文本分类

文本分类的目标是将文档(例如电子邮件、帖子、短信、产品评论等)分配到一个或多个类别。这些类别可以是评论分数、垃圾邮件与非垃圾邮件,或文档的输入语言。如今,构建此类分类器的主要方法是机器学习,即从示例中学习分类规则。为了构建此类分类器,我们需要标记数据,它由文档及其相应的类别(或标签或标签)组成。

1、准备待训练的数据集

https://dl.fbaipublicfiles.com/fasttext/data/cooking.stackexchange.tar.gz

bash 复制代码
__label__sauce __label__cheese How much does potato starch affect a cheese sauce recipe?
__label__food-safety __label__acidity Dangerous pathogens capable of growing in acidic environments
__label__cast-iron __label__stove How do I cover up the white spots on my cast iron stove?
__label__restaurant Michelin Three Star Restaurant; but if the chef is not there
__label__knife-skills __label__dicing Without knife skills, how can I quickly and accurately dice vegetables?
__label__storage-method __label__equipment __label__bread What's the purpose of a bread box?
__label__baking __label__food-safety __label__substitutions __label__peanuts how to seperate peanut oil from roasted peanuts at home?
__label__chocolate American equivalent for British chocolate terms
__label__baking __label__oven __label__convection Fan bake vs bake
__label__sauce __label__storage-lifetime __label__acidity __label__mayonnaise Regulation and balancing of readymade packed mayonnaise and other sauces
__label__tea What kind of tea do you boil for 45minutes?
__label__baking __label__baking-powder __label__baking-soda __label__leavening How long can batter sit before chemical leaveners lose their power?
__label__food-safety __label__soup Can I RE-freeze chicken soup after it has thawed?
__label__sous-vide __label__vacuum Ziploc vacuumed bags expand in sous vide
__label__baking __label__substitutions __label__syrup What can I use instead of corn syrup?
__label__vegan __label__almonds __label__almond-milk Does soaking almonds have the same effect as blanching and removing the skins when making almond milk?
__label__baking __label__cake __label__soda Cake sinks in the middle when baking. Only happens when I make a Coca-Cola Cake
__label__baking Which plastic wrap is okay for oven use?
__label__tea Can I dissolve sugar first before steeping tea?
__label__food-safety __label__salmon Is it safe to eat food that was heated in plastic wrap to the point the plastic wrap flamed?
__label__flavor __label__spices __label__chemistry Flavor and Chemical Composition of Thyme
__label__equipment What can I use as a manual hard cheese slicer?
__label__flour __label__milling Are stone or metal grinding wheels better for flour?
__label__beans Do fava beans need to cook longer than other kinds of beans?
__label__baking __label__bread __label__kneading Kneading Bread After Rising
__label__beef __label__roast __label__gravy __label__roast-beef Extraordinary Beef Gravy?
__label__baking __label__bread __label__crust How to heat up already baked french bread in oven to get a crispy crust
__label__chocolate Is there a difference in appearance between semi and unsweetened chocolate?
__label__food-science __label__marinade __label__brining If salt dehydrates the meat, then why would brining make it more juicy as a whole?
__label__cookies __label__texture __label__american-cuisine How long after baking do American chewy cookies get their normal texture?
__label__fruit __label__alcohol __label__liqueur Is cooking with fruit liqueur comparable to cooking with fruit juice?
__label__bread __label__cheese __label__jelly __label__brie Suggestions for Brie + Bread + Preserves
__label__soup __label__texture __label__standards What is the correct consistency of a cream soup?
__label__food-science __label__tea Making tea - milk first or tea first
__label__food-safety __label__salt Sea Salt and Mercury
__label__cinnamon Cinnamon Thickening
__label__sauce __label__flavor __label__syrup Basic carrier sauce/syrup for different sweet flavors?
__label__roasting __label__eggplant What is the 'cleanest' way to roast eggplants indoor?
__label__rice Cooking and storing rice for a whole week
__label__dehydrating dehydrating puree food
__label__soup __label__canning __label__food-processing In industrially produced soup, how does each can contain equal parts of all ingredients?
__label__flavor __label__microwave __label__popcorn How Is Microwave Popcorn Flavoured?
__label__culinary-uses __label__vegetables __label__eggplant What can I do with under-ripe eggplant?
__label__water __label__cocktails __label__whiskey Whiskey and Water
__label__meat __label__ham Wet Cooked Ham Slices
__label__onions __label__deep-frying __label__restaurant-mimicry Beer Battered Onion Rings -- what makes them look shiny?
__label__wok Determining a wok's material
__label__food-safety __label__oil Is cloudy-looking used peanut oil safe?
__label__oil __label__cleaning __label__coconut __label__olive-oil __label__maintenance To finely spray a thin layer of warm liquid Coconut Oil?
__label__candy __label__fudge Why Do We 'Simmer' Fudge Instead of 'Boiling' it?

2、训练数据

bash 复制代码
./fasttext supervised -input cooking.train -output model_cooking

3、查看训练结果

  • model_cooking.bin: 训练好的分类器文件
  • model_cooking.vec: 这个里面放的每个单词及其向量

4、简单测试下

说明:

第一个问题问什么烤盘适合做香蕉面包,关联词是 baking 有关联。

第一个问题问为什么不把刀放进洗碗机,关联词是 食物安全 无关联。

5、验证一下训练的结果

上面显示了默认1和设置5时的召回率。

6、一些提高准确率和召回率的方法

  • 数据量:增加训练学习的样本数据量
  • 预处理:标点符号处理、大小写统一减少词汇量
  • 更多周期和更大的学习率:增加学习周期,多学习几次。
  • 单词 n-gram:通过使用二元词组而不是一元词组来提高模型的性能。

重新进行训练与验证,准确率提高到了13%。

Word2Vec

通过对一系列的文本进行训练,就能得到每个词的多维向量,比如小明和小刚总是和'男性'一起出现,那小明和小刚的向量维度中就可能有几个维度是和性别有关。

举个例子,就像用RGB数字来描述颜色,这个世界上每个词都可以用向量来表达它,向量维度越多越准确。

  • skipgram: 通过邻近单词来学习预测目标单词。
  • cbow: 根据上下文来预测目标单词。

1、使用skipgram模式生成词向量

2、查看生成结果

3、词搜索

Faiss

通过并行搜索(GPU)、

其他

1、什么是二元词组?

首先"unigram" 是指单个不可分割的单元或标记,通常用作模型的输入。例如,unigram 可以是单词或字母,具体取决于模型。在 fastText 中,我们在单词级别工作,因此 unigram 就是单词。

比如下面这句话,

我爱吃家乡富平县的大红苹果

"unigram'拆分如下

'我'、'爱'、'吃'、'家乡'、'富平县'、'的'、'大'、'红'、'苹果'

二元词组拆分如下

'我爱'、'爱吃'、'吃家乡'、'家乡富平县'、'富平县的'、'的大'、'大红'、'红苹果'

计算每个二元词组在一个大训练集中的出现概览,就能用概览去预测新的文本生成序列。

条目 概览
我爱 30%
我恨 40%
我想 30%

2、分层 softmax

建立一个二叉树,其叶子与标签相对应。每个中间节点都有一个经过训练的二元决策激活(例如 S 形),并预测我们应该向左还是向右。然后,输出单元的概率由从根到输出单元叶子的路径上中间节点的概率的乘积给出。

在 fastText 中,使用哈夫曼树,这样对于更频繁的输出,查找时间更快,因此输出的平均查找时间是最佳的。

相关推荐
何遇mirror16 天前
【Faiss】构建高效搜索系统 - Faiss向量数据库的搭建
faiss
程序者王大川18 天前
【大数据】深入解析向量数据库Faiss:搭建与使用指南
数据库·学习·ai编程·kylin·faiss
何遇mirror20 天前
构建高效搜索系统 - Faiss向量数据库的快速入门
faiss
微学AI20 天前
人工智能任务6-基于FAISS数据库的应用:向量数据库的搭建与中文文本相似度搜索
数据库·人工智能·faiss
大白菜程序猿23 天前
向量数据库Faiss的详细介绍和搭建使用教程
faiss
王卫东1 个月前
ES 模糊查询 wildcard 的替代方案探索
elasticsearch·faiss·es·ngram·wildcard·模糊搜索
cesske1 个月前
springboot springmvc spring区别
faiss
王卫东1 个月前
深入理解Faiss:高效向量检索的利器
faiss·向量数据库
风好衣轻1 个月前
【向量数据库】Ubuntu编译安装FAISS
linux·ubuntu·faiss