加载预训练的单词嵌入
加载预训练的 fastText 单词嵌入。此函数需要 Text Analytics Toolbox™ Model for fastText English 16 Billion Token Word Embedding 支持包。如果未安装此支持包,则函数会提供下载链接。
emb = fastTextWordEmbedding;
加载数据
根据 factoryReports.csv 中的数据创建一个表格文本数据存储。仅读取 "Description" 和 "Category" 列中的数据。
filenameTrain = "factoryReports.csv";
textName = "Description";
labelName = "Category";
ttdsTrain = tabularTextDatastore(filenameTrain,'SelectedVariableNames',[textName labelName]);
预览数据存储。
ttdsTrain.ReadSize = 8;
preview(ttdsTrain)
ans=8×2 table
Description Category
_______________________________________________________________________ ______________________
{'Items are occasionally getting stuck in the scanner spools.' } {'Mechanical Failure'}
{'Loud rattling and banging sounds are coming from assembler pistons.'} {'Mechanical Failure'}
{'There are cuts to the power when starting the plant.' } {'Electronic Failure'}
{'Fried capacitors in the assembler.' } {'Electronic Failure'}
{'Mixer tripped the fuses.' } {'Electronic Failure'}
{'Burst pipe in the constructing agent is spraying coolant.' } {'Leak' }
{'A fuse is blown in the mixer.' } {'Electronic Failure'}
{'Things continue to tumble off of the belt.' } {'Mechanical Failure'}
创建一个自定义变换函数,该函数将从数据存储中读取的数据转换为包含预测变量和响应的表。在示例末尾列出的 transformTextData 函数接受从 tabularTextDatastore 对象读取的数据,并返回包含预测变量和响应的表。预测变量是由单词嵌入 emb 给出的 1×sequenceLength×C 单词向量数组,其中 C 是嵌入维度。这些响应是 classNames 中的类的分类标签。
使用在示例末尾列出的 read