trainWordEmbedding — Train a word embedding from a text file or tokenized documents.
trainWordEmbedding(filename) and trainWordEmbedding(documents) return a wordEmbedding trained from local text. File input is UTF-8 text with one document per line and whitespace-separated words; tokenizedDocument input uses its stored tokens.
Syntax
emb = trainWordEmbedding(filename)
emb = trainWordEmbedding(documents)
emb = trainWordEmbedding(___, Name, Value)Inputs
source- Type
Any- Required
- Yes
- Description
- UTF-8 text filename or tokenizedDocument object.
NameValue- Type
Any- Required
- Variadic
- Description
- Name-value options controlling local deterministic embedding training.
Returns
emb- Type
Any- Description
- Word embedding compatibility object.
Errors
RunMat:trainWordEmbedding:InvalidInput- When
- Inputs do not match a supported trainWordEmbedding form.
- Message
trainWordEmbedding received invalid input
Supported forms
emb = trainWordEmbedding(filename)
emb = trainWordEmbedding(documents)
emb = trainWordEmbedding(___, Name, Value)
emb is a wordEmbedding object.
Dimension, NumEpochs, MinCount, NumNegativeSamples, and UpdateRate must be positive integers.
Window must be a nonnegative integer.
Model must be "skipgram" or "cbow".
LossFunction must be "ns", "hs", or "softmax"; NumNegativeSamples is only valid with "ns".
NGramRange must be a two-element nonnegative integer vector [min max] with min <= max; [0 0] disables subword features.
Behavior
filename may be a string scalar, character vector, scalar string array, or 1-by-1 cell containing a text filename.
documents must be a tokenizedDocument object.
Dimension, Window, Model, DiscardFactor, LossFunction, NumNegativeSamples, NumEpochs, MinCount, NGramRange, InitialLearnRate, UpdateRate, and Verbose are parsed and validated using the documented MATLAB option names.
MinCount filters vocabulary words before model construction. Vocabulary order is descending frequency with first-seen order as the tie-breaker.
The returned object exposes Dimension and Vocabulary and stores dense vectors in the same implementation-detail property used by word2vec and vec2word.
The current trainer is deterministic and uses lexical/subword hashing with windowed co-occurrence features. It supports repeatable embedding lookup workflows but does not implement the fastText optimizer.
RunMat mode accepts native integer option values. Counts, sizes, ranges, and Verbose are decoded exactly; integer-valued learning-rate controls must be exactly representable as double.
Very large dense embedding outputs are rejected before allocation when they exceed the configured model-size limit.
Execution and residency
trainWordEmbedding tokenizes host file or document data and builds host model metadata; it does not use provider kernels.
trainWordEmbedding performs host text processing and model construction and has no provider kernel.
Examples
Train From File
emb = trainWordEmbedding("training.txt", "Dimension", 50, "MinCount", 1)Expected output:
`emb` is a `wordEmbedding` object.Train From Tokenized Documents
documents = tokenizedDocument(["alpha beta"; "beta gamma"]);
emb = trainWordEmbedding(documents, "Window", 2, "MinCount", 1)Expected output:
`emb.Vocabulary` contains the retained document tokens.Using trainWordEmbedding with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how trainWordEmbedding changes the result.
Run a small trainWordEmbedding example, explain the result, then change one input and compare the output.
FAQ
Does RunMat implement MATLAB's exact fastText training algorithm?⌄
No. The current deterministic lexical/subword and co-occurrence trainer produces a usable wordEmbedding, but its vectors are not fastText optimizer results.
Can the trained model be used with word2vec and vec2word?⌄
Yes. The output is the wordEmbedding object consumed by the existing embedding lookup builtins.
Does trainWordEmbedding execute on the GPU?⌄
No. It reads host text/model data and builds host vectors.
Related Strings functions
Text Analytics
addDependencyDetails · addEntityDetails · addLemmaDetails · addPartOfSpeechDetails · addSentenceDetails · addTypeDetails · bagOfNgrams · bagOfWords · cosineSimilarity · doc2sequence · encode · extractFileText · extractHTMLText · fastTextWordEmbedding · findElement · getAttribute · htmlTree · ind2word · isVocabularyWord · normalizeWords · readWordEmbedding · removeLongWords · removeShortWords · removeStopWords · removeWords · stopWords · tokenDetails · tokenizedDocument · vaderSentimentScores · vec2word · word2ind · word2vec · wordEncoding · writeWordEmbedding
Transform
append · deblank · erase · eraseBetween · erasePunctuation · eraseURLs · extractAfter · extractBefore · extractBetween · insertAfter · insertBefore · join · lower · pad · replace · replaceBetween · reverse · split · splitlines · strcat · strip · strjoin · strjust · strrep · strsplit · strtrim · upper
Core
blanks · char · compose · convertCharsToStrings · convertContainedStringsToChars · convertStringsToChars · genvarname · int2str · isletter · isspace · isStringScalar · isstrprop · mat2str · native2unicode · newline · num2str · sprintf · sscanf · str2double · str2num · strcmp · strcmpi · string · string.empty · strings · strlength · strncmp · strncmpi · strtok · unicode2native
Search
contains · endsWith · matches · startsWith · strfind
Pattern
digitsPattern · lettersPattern · pattern · regexpPattern · textBoundary · wildcardPattern
Source you can read
Every RunMat function's source is public. Read exactly how trainWordEmbedding is executed, line by line, in Rust.
- View the source for trainWordEmbedding in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.