Main Content

doc2sequence

Convert documents to sequences for deep learning

Description

example

sequences= doc2sequence(enc,documents)returns a cell array of the numeric indices of the words indocumentsgiven by the word encodingenc。Each element ofsequencesis a vector of the indices of the words in the corresponding document.

example

sequences= doc2sequence(emb,documents)returns a cell array of the embedding vectors of the words indocumentsgiven by the word embeddingemb。Each element ofsequencesis a matrix of the embedding vectors of the words in the corresponding document.

example

sequences= doc2sequence(___,Name,Value)specifies additional options using one or more name-value pair arguments.

Examples

collapse all

Load the factory reports data and create atokenizedDocumentarray.

文件名="factoryReports.csv"; data = readtable(filename,'TextType','string'); textData = data.Description; documents = tokenizedDocument(textData);

Create a word encoding.

enc = wordEncoding(documents);

Convert the documents to sequences of word indices.

sequences = doc2sequence(enc,documents);

View the sizes of the first 10 sequences. Each sequence is a 1-by-Svector, whereSis the number of word indices in the sequence. Because the sequences are padded,Sis constant.

sequences(1:10)
ans=10×1 cell array{1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double} {1x17 double}

Convert an array of tokenized documents to sequences of word vectors using a pretrained word embedding.

Load a pretrained word embedding using thefastTextWordEmbeddingfunction. This function requires Text Analytics Toolbox™ Modelfor fastText English 16 Billion Token Word Embeddingsupport package. If this support package is not installed, then the function provides a download link.

emb = fastTextWordEmbedding;

Load the factory reports data and create atokenizedDocumentarray.

文件名="factoryReports.csv"; data = readtable(filename,'TextType','string'); textData = data.Description; documents = tokenizedDocument(textData);

Convert the documents to sequences of word vectors usingdoc2sequence。Thedoc2sequencefunction, by default, left-pads the sequences to have the same length. When converting large collections of documents using a high-dimensional word embedding, padding can require large amounts of memory. To prevent the function from padding the data, set the'PaddingDirection'option to'none'。Alternatively, you can control the amount of padding using the'Length'option.

sequences = doc2sequence(emb,documents,'PaddingDirection','none');

View the sizes of the first 10 sequences. Each sequence isD-by-Smatrix, whereDis the embedding dimension, andSis the number of word vectors in the sequence.

sequences(1:10)
ans=10×1 cell array{300×10 single} {300×11 single} {300×11 single} {300×6 single} {300×5 single} {300×10 single} {300×8 single} {300×9 single} {300×7 single} {300×13 single}

Convert a collection of documents to sequences of word vectors using a pretrained word embedding, and pad or truncate the sequences to a specified length.

Load a pretrained word embedding usingfastTextWordEmbedding。This function requires Text Analytics Toolbox™ Modelfor fastText English 16 Billion Token Word Embeddingsupport package. If this support package is not installed, then the function provides a download link.

emb = fastTextWordEmbedding;

Load the factory reports data and create atokenizedDocumentarray.

文件名="factoryReports.csv"; data = readtable(filename,'TextType','string'); textData = data.Description; documents = tokenizedDocument(textData);

Convert the documents to sequences of word vectors. Specify to left-pad or truncate the sequences to have length 100.

sequences = doc2sequence(emb,documents,'Length',100);

View the sizes of the first 10 sequences. Each sequence isD-by-Smatrix, whereDis the embedding dimension, andSis the number of word vectors in the sequence (the sequence length). Because the sequence length is specified,Sis constant.

sequences(1:10)
ans=10×1 cell array{300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single} {300×100 single}

Input Arguments

collapse all

输入字嵌入, specified as awordEmbeddingobject.

Input word encoding, specified as awordEncodingobject.

Input documents, specified as atokenizedDocumentarray.

Name-Value Pair Arguments

Specify optional comma-separated pairs ofName,Valuearguments.Nameis the argument name andValueis the corresponding value.Namemust appear inside quotes. You can specify several name and value pair arguments in any order asName1,Value1,...,NameN,ValueN

Example:'Length','shortest'truncates the sequences to have the same length as the shortest sequence.

Unknown word behavior, specified as the comma-separated pair consisting of'UnknownWord'and one of the following:

  • 'discard'– If a word is not in the input map, then discard it.

  • 'nan'– If a word is not in the input map, then return aNaNvalue.

Tip

If you are creating sequences for training a deep learning network with a word embedding, use'discard'。Do not use sequences withNaNvalues, because doing so can propagate errors through the network.

Padding direction, specified as the comma-separated pair consisting of'PaddingDirection'and one of the following:

  • 'left'– Pad sequences on the left.

  • 'right'– Pad sequences on the right.

  • 'none'– Do not pad sequences.

Tip

When converting large collections of data using a high-dimensional word embedding, padding can require large amounts of memory. To prevent the function from adding too much padding, set the'PaddingDirection'option to'none'or set'Length'to a smaller value.

Padding value, specified as the comma-separated pair consisting of'PaddingValue'and a numeric scalar. Do not pad sequences withNaN, because doing so can propagate errors through the network.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64

Sequence length, specified as the comma-separated pair consisting of'Length'and one of the following:

  • 'longest'– Pad sequences to have the same length as the longest sequence.

  • 'shortest'– Truncate sequences to have the same length as the shortest sequence.

  • Positive integer – Pad or truncate sequences to have the specified length. The function truncates the sequences on the right.

Data Types:single|double|int8|int16|int32|int64|uint8|uint16|uint32|uint64|char|string

Output Arguments

collapse all

Output sequences, returned as a cell array.

For word embedding input, theith element ofsequencesis a matrix of the word vectors corresponding to theith input document.

For word encoding input, theith element ofsequencesis a vector of the word encoding indices corresponding to theith input document.

Tips

  • When converting large collections of data using a high-dimensional word embedding, padding can require large amounts of memory. To prevent the function from adding too much padding, set the'PaddingDirection'option to'none'or set'Length'to a smaller value.

Introduced in R2018b