原文链接
这个例子用来展示如何使用一个卷积神经网络来对文本数据进行分类。想使用卷积神经网络对文本数据进行分类,必须先把文本数据转换成图像的矩阵形式。填充或截断观察值,使其具有恒定长度,并且使用一个字嵌入将文档转换为长度为
的子向量序列。然后就可以使用
的图像来表示字了。
创建一个tabularTextDatastore对象,方便于将文本数据从CSV文件格式转换为图像格式。使用自带的转换函数 transform从 tabularTextDatastore对象中读取数据并转换为图像。transformTextData这个函数获取从数据存储中读取的数据和预先训练过的嵌入单词,并将每个观察结果转换为单词向量数组。
这个实例中,训练了一个带有各种各样宽度的一维卷积滤波器,每个滤波器的宽度都依赖于单词的长度。
加载预训练的单词嵌入
加载预训练的单词嵌入,这个功能需要文本分析工具箱。
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'}
创建一个自定义的transform函数从数据文本库读取数据并且转换到一个带有预测结果的表格中。transformTextData函数,从 tabularTextDatastore对象中读取数据并且返回预测值。这个预测值是大小的字组成的向量,其中的
是嵌入的维度。
未完待续,这部分还用不到,如果需要的话再补上。
