fastrtext (version 0.3.3)

build_supervised: Build a supervised fasttext model

Description

Trains a supervised model, following the method layed out in Bag of Tricks for Efficient Text Classification using the fasttext implementation.

See FastText text classification tutorial for more information on training supervised models using fasttext.

Usage

build_supervised(documents, targets, model_path, lr = 0.05, dim = 100,
  ws = 5, epoch = 5, minCount = 5, minCountLabel = 0, neg = 5,
  wordNgrams = 1, loss = c("ns", "hs", "softmax", "ova", "one-vs-all"),
  bucket = 2e+06, minn = 3, maxn = 6, thread = 12,
  lrUpdateRate = 100, t = 1e-04, label = "__label__", verbose = 2,
  pretrainedVectors = NULL)

Arguments

documents

character vector of documents used for training

targets

vector of targets/catagory of each document. Must have same length as documents and be coercable to character

model_path

Name of output file without file extension.

lr

learning rate

dim

size of word vectors

ws

size of the context window

epoch

number of epochs

minCount

minimal number of word occurences

minCountLabel

minimal number of label occurences

neg

number of negatives sampled

wordNgrams

max length of word ngram

loss

= c('softmax', 'ns', 'hs', 'ova'), loss function ns, hs, softmax, one Vs all. one Vs all loss is usefull for multi class when you need to apply a threshold for each class score.

bucket

number of buckets

minn

min length of char ngram

maxn

max length of char ngram

thread

number of threads

lrUpdateRate

change the rate of updates for the learning rate

t

sampling threshold

label

text string, labels prefix. Default is "label"

verbose

verbosity level

pretrainedVectors

path to pretrained word vectors for supervised learning. Leave empty for no pretrained vectors.

Value

path to new model file as a character

Examples

Run this code
# NOT RUN {
library(fastrtext)
model_file <- build_supervised(documents = train_sentences[["text"]],
                               targets =train_sentences[["class.text"]],
                               model_path = 'my_model',
                               dim = 20, lr = 1, epoch = 20, wordNgrams = 2)

model <- load_model(model_file)

predictions <- predict(model, test_sentences[["text"]])
mean(sapply(predictions, names) == test_sentences[["class.text"]])
# ~0.8
# }

Run the code above in your browser using DataLab