FastText(optionsopt)

new FastText(optionsopt)

Exports an instance of FastText class
Parameters:
Name Type Attributes Default Description
options object <optional>
{} Library configuration
Properties
Name Type Attributes Default Description
model string <optional>
'training-model' The model path which will be used for this instance
logs boolean <optional>
false Whether to show library logs

Methods

cbow(trainData, optionsopt) → {boolean}


Function to train a CBOW model
Parameters:
Name Type Attributes Default Description
trainData string URL or filename
options string <optional>
{} Options for training model
Properties
Name Type Attributes Default Description
dim number <optional>
100 Size of word vector
ws number <optional>
5 Size of context window
neg number <optional>
5 Number of negatives sampled
maxn number <optional>
5 Maximum wordGram length
minn number <optional>
3 Minimum wordGram length
model string <optional>
'training-model-skipgram' The model name which will be used to export the saved model
Returns:
boolean - Returns true after finishing the training
Example

Using fastText.skipgram() to train models

 const FastText = require('fasttext-node');
 const fastText = new FastText();  // Pass any configurations in the constructor
 const result = await fastText.cbow(
   'https://raw.githubusercontent.com/jazzyarchitects/fasttext-node/master/train.txt',
   {
     dim: 50,
     model: 'my-training-model-cbow',
   }
 );

predict(string, optionsopt) → {array}


Predicts the probability label according to a given model
Parameters:
Name Type Attributes Default Description
string string String to predict the labels of
options object <optional>
{} Options for training model
Properties
Name Type Attributes Default Description
labelCount number <optional>
3 Number of labels to be returned by the predicting function
model string <optional>
'training-model' The model name which will be used to export the saved model. This option will override the model options passed in the constructor.
Returns:
array - An array of input and it's respective label and probability
Examples

Using fastText.predict() to predict labels of a string from the given model

 const FastText = require('fasttext-node');
 const fastText = new FastText();  // Pass any configurations in the constructor
 const result = await fastext.predict(
  [
    'Custard Pudding tasting like raw eggs',
    'Is Himalayan pink salt the same as the pink salt used for curing?',
  ],
  {
    model: 'my-training-model',
  }
 );

// OR

 const result = await fastext.predict(`
    Custard Pudding tasting like raw eggs
    Is Himalayan pink salt the same as the pink salt used for curing?
  `,
  {
    model: 'my-training-model',
  }
 );

Result of fastText.predict()

[
   {
     input: 'Custard Pudding tasting like raw eggs',
     predictions: {
       eggs: 0.595703,
       'egg-whites': 0.00390627,
       frying: 0.00390627
     }
   },
   {
     input: 'Is Himalayan pink salt the same as the pink salt used for curing?',
     predictions: {
       salt: 0.166016,
       flavor: 0.0136719,
       language: 0.0117188
   }
]

skipgram(trainData, optionsopt) → {boolean}


Function to train a skipgram model

Difference between CBOW and SKIPGRAM model can be found at: Skipgram v/s Cbow
Parameters:
Name Type Attributes Default Description
trainData string URL or filename
options string <optional>
{} Options for training model
Properties
Name Type Attributes Default Description
dim number <optional>
100 Size of word vector
ws number <optional>
5 Size of context window
neg number <optional>
5 Number of negatives sampled
maxn number <optional>
5 Maximum wordGram length
minn number <optional>
3 Minimum wordGram length
model string <optional>
'training-model-unsupervised' The model name which will be used to export the saved model
Returns:
boolean - Returns true after finishing the training
Example

Using fastText.skipgram() to train models

 const FastText = require('fasttext-node');
 const fastText = new FastText();  // Pass any configurations in the constructor
 const result = await fastText.skipgram(
   'https://raw.githubusercontent.com/jazzyarchitects/fasttext-node/master/train.txt',
   {
     dim: 50,
     model: 'my-training-model-skipgram',
   }
 );

test(testData, optionsopt) → {object}


Function to test the trained model against a set of test data.
  • If the testData param is a url the fetch the file from the url
  • If the testData param is a file name then read that filename
  • Else show an error as you cannot test from small amount of data
Parameters:
Name Type Attributes Default Description
testData string URL or filename
options string <optional>
{} Options for training model
Properties
Name Type Attributes Default Description
labelCount number <optional>
3 Number of labels to be returned by the predicting function
model string <optional>
'training-model' The model name which will be used to export the saved model
Returns:
object - Returns the accuracy of the model according to the given test data set. To know what precision and recall mean, visit https://en.wikipedia.org/wiki/Precision_and_recall
Examples

Using fastText.test() to test the accuracy models

 const FastText = require('fasttext-node');
 const fastText = new FastText();  // Pass any configurations in the constructor
 const result = await fastText.test(
   'https://raw.githubusercontent.com/jazzyarchitects/fasttext-node/master/train.txt',
   {
     labelCount: 3,
     model: 'my-training-model',
   }
 );

Result of fastText.test()

{
   samples: 12404,
   precision: 0.203,
   recall: 0.44
}

train(trainData, optionsopt) → {boolean}

Function to train a model from the given training data. This function uses supervised learning for training the model.
  • If the trainData param is a url the fetch the file from the url
  • If the trainData param is a file name then read that filename
  • Else show an error as you cannot train from small amount of data
Parameters:
Name Type Attributes Default Description
trainData string URL or filename
options object <optional>
{} Options for training model
Properties
Name Type Attributes Default Description
epoch number <optional>
25 Epoch for the training
lr number <optional>
0.1 Learning rate for training model
lrUpdateRate number <optional>
100 The rate at which learning rate is to be updated while training
dim number <optional>
5 Size of word vector
ws number <optional>
5 Size of context window
neg number <optional>
5 Number of negatives sampled
wordNgrams number <optional>
2 Max length of word ngram
loss 'ns' | 'hs' | 'softmax' <optional>
'ns' Loss function. Should be one of 'ns' 'hs' or 'softmax'
thread number <optional>
12 Number of threads
model string <optional>
'training-model' The model name which will be used to export the saved model. This option will override the model options passed in the constructor.
Returns:
boolean - Returns true after finishing the training
Example

Using fastText.train() to train models

 const FastText = require('fasttext-node');
 const fastText = new FastText();  // Pass any configurations in the constructor
 const result = await fastText.train(
   'https://raw.githubusercontent.com/jazzyarchitects/fasttext-node/master/train.txt',
   {
     epoch: 50,
     model: 'my-training-model',
   }
 );