### Install fastText Python module Source: https://github.com/facebookresearch/fasttext/blob/main/docs/support.md Commands to install the fastText library as a Python module. Users can install via pip or the setup.py script. ```bash $ git clone https://github.com/facebookresearch/fastText.git $ cd fastText $ sudo pip install . $ # or : $ sudo python setup.py install ``` -------------------------------- ### Build fastText command line tool Source: https://github.com/facebookresearch/fasttext/blob/main/docs/support.md Instructions to clone the repository and compile the fastText binary using make. This requires a C++11 compatible compiler and make utility. ```bash $ git clone https://github.com/facebookresearch/fastText.git $ cd fastText $ make ``` -------------------------------- ### Verify fastText Python installation Source: https://github.com/facebookresearch/fasttext/blob/main/docs/support.md A simple test to verify that the fastText Python module is correctly installed and importable. ```python import fasttext ``` -------------------------------- ### Install fastText from GitHub Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.rst Installs the latest development version of fastText from its GitHub repository. This involves cloning the repository and then installing it using pip or setup.py. ```bash git clone https://github.com/facebookresearch/fastText.git cd FastText sudo pip install . # or : sudo python setup.py install ``` -------------------------------- ### Install fastText from GitHub repository Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.md Installs the development version of fastText by cloning the GitHub repository and installing it locally. This method is useful for accessing the latest features or contributing to the project. ```bash $ git clone https://github.com/facebookresearch/fastText.git $ cd fastText $ sudo pip install . $ # or : $ sudo python setup.py install ``` -------------------------------- ### Install fastText using pip Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.md Installs the latest release of the fastText library using pip. This is the simplest way to get started with fastText. ```bash $ pip install fasttext ``` -------------------------------- ### Install fastText Python Bindings Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Command to install the fastText library and its Python bindings from the source repository. ```bash pip install . ``` -------------------------------- ### Build and Setup fastText WebAssembly Source: https://github.com/facebookresearch/fasttext/blob/main/docs/webassembly-module.md Commands to initialize the Emscripten environment, clone the repository, and compile the source code into WebAssembly binaries. ```bash source /path/to/emsdk/emsdk_env.sh git clone git@github.com:facebookresearch/fastText.git cd fastText make wasm ``` -------------------------------- ### Install fastText using pip Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.rst Installs the latest release of the fastText library using pip. This is the recommended method for most users. ```bash pip install fasttext ``` -------------------------------- ### Install fastText via CLI Source: https://context7.com/facebookresearch/fasttext/llms.txt Commands to install the fastText library using pip or by building from source to access the latest features and command-line tools. ```bash pip install fasttext git clone https://github.com/facebookresearch/fastText.git cd fastText pip install . make ``` -------------------------------- ### Serve WebAssembly Application Source: https://github.com/facebookresearch/fasttext/blob/main/docs/webassembly-module.md Command to start a local development server to correctly load WebAssembly resources in a browser environment. ```bash cd webassembly-test python -m SimpleHTTPServer ``` -------------------------------- ### Install fastText via CLI Source: https://github.com/facebookresearch/fasttext/blob/main/docs/python-module.md Instructions for installing the fastText library using pip or building from the source repository. ```bash pip install fasttext ``` ```bash git clone https://github.com/facebookresearch/fastText.git cd fastText sudo pip install . # or : sudo python setup.py install ``` -------------------------------- ### FastText Command Line Interface for Model Training and Usage Source: https://context7.com/facebookresearch/fasttext/llms.txt Provides examples of using the FastText command-line interface for training word vectors, text classifiers, and performing various model operations like prediction and quantization. ```bash # Train word vectors (skipgram) ./fasttext skipgram -input data.txt -output model # Creates model.bin (full model) and model.vec (word vectors text file) # Train word vectors (cbow) ./fasttext cbow -input data.txt -output model # Train with custom parameters ./fasttext skipgram -input data.txt -output model \ -dim 300 -epoch 10 -lr 0.05 -minCount 5 \ -minn 3 -maxn 6 -thread 4 # Print word vectors echo "king queen" | ./fasttext print-word-vectors model.bin # Print sentence vectors echo "This is a sentence" | ./fasttext print-sentence-vectors model.bin # Interactive nearest neighbors ./fasttext nn model.bin # Type words to find neighbors, Ctrl+C to exit # Word analogies (A - B + C = ?) ./fasttext analogies model.bin # Type: berlin germany france # Output: paris 0.896 # Train text classifier ./fasttext supervised -input train.txt -output model \ -lr 1.0 -epoch 25 -wordNgrams 2 # Test classifier ./fasttext test model.bin test.txt # Output: N, P@1, R@1 # Test with k predictions ./fasttext test model.bin test.txt 5 # Predict labels ./fasttext predict model.bin test.txt ./fasttext predict model.bin test.txt 5 # top-5 # Predict with probabilities ./fasttext predict-prob model.bin test.txt 3 # Interactive prediction ./fasttext predict model.bin - # Type text, get predictions # Quantize model ./fasttext quantize -output model # Creates model.ftz (compressed) # Autotune ./fasttext supervised -input train.txt -output model \ -autotune-validation valid.txt \ -autotune-duration 600 \ -autotune-modelsize 2M ``` -------------------------------- ### Optimize Precision at Recall (Command Line) Source: https://github.com/facebookresearch/fasttext/blob/main/docs/autotune.md This command line example illustrates how to configure autotune to optimize for the best precision at a specified recall level (e.g., 30%). This is useful for scenarios where maintaining a certain level of accuracy is critical. ```sh ./fasttext supervised [...] -autotune-metric precisionAtRecall:30 ``` -------------------------------- ### Load FastText Model and Get Basic Information (JavaScript) Source: https://github.com/facebookresearch/fasttext/blob/main/webassembly/doc/examples/misc.html Initializes a FastText model, loads a model file from a URL, and then logs whether the model is quantized and its dimensionality. This is a foundational step before performing other model operations. ```javascript import {FastText, addOnPostRun} from "./fasttext.js"; addOnPostRun(() => { let ft = new FastText(); const url = "lid.176.ftz"; ft.loadModel(url).then(model => { /* isQuant */ console.log(model.isQuant()); /* getDimension */ console.log(model.getDimension()); }); }); ``` -------------------------------- ### Build fastText using CMake Source: https://github.com/facebookresearch/fasttext/blob/main/README.md Instructions for building the fastText library using 'cmake'. This method requires cloning the master branch and involves creating a build directory, configuring with cmake, and then compiling and installing. It generates the fasttext binary along with shared, static, and PIC libraries. ```bash git clone https://github.com/facebookresearch/fastText.git cd fastText mkdir build && cd build && cmake .. make && make install ``` -------------------------------- ### Manage Pre-trained Word Vectors with fastText Source: https://context7.com/facebookresearch/fasttext/llms.txt Demonstrates how to download, load, and manipulate pre-trained word vectors. It includes examples for finding nearest neighbors, reducing vector dimensions, and manually parsing text-based vector files. ```python import fasttext import fasttext.util # Download and load pre-trained vectors fasttext.util.download_model('en', if_exists='ignore') model = fasttext.load_model('cc.en.300.bin') # Get word vector vector = model.get_word_vector('hello') print(vector.shape) # (300,) # Find similar words neighbors = model.get_nearest_neighbors('computer') # Reduce dimensions (for memory/speed) fasttext.util.reduce_model(model, 100) # Reduce to 100 dims # Manual loading of .vec file def load_vectors(fname): vectors = {} with open(fname, 'r', encoding='utf-8') as f: n, d = map(int, f.readline().split()) for line in f: parts = line.rstrip().split(' ') vectors[parts[0]] = list(map(float, parts[1:])) return vectors ``` -------------------------------- ### Optimize Recall at Precision (Command Line) Source: https://github.com/facebookresearch/fasttext/blob/main/docs/autotune.md This command line example shows how to set autotune to optimize for the best recall at a given precision level (e.g., 30%). This is beneficial when maximizing the number of true positives is a priority, given a certain accuracy threshold. ```sh ./fasttext supervised [...] -autotune-metric recallAtPrecision:30 ``` -------------------------------- ### Train Supervised Text Classifier with fastText Source: https://context7.com/facebookresearch/fasttext/llms.txt Shows how to train a supervised text classification model using labeled data. Includes examples of single and multi-label classification, prediction, and model persistence. ```python import fasttext # Train a text classifier model = fasttext.train_supervised( input='train.txt', lr=0.1, epoch=25, wordNgrams=2, dim=100, loss='softmax', bucket=2000000, minCount=1, label='__label__', thread=4 ) # Predict labels labels, probs = model.predict("This is a great movie!") # Predict top-k labels labels, probs = model.predict("This is a great movie!", k=3) # Save model model.save_model('classifier.bin') ``` -------------------------------- ### Train fastText with Combined Epochs and Learning Rate Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Combines increased epochs and an adjusted learning rate for potentially superior model training. This example uses -epoch 25 and -lr 1.0, demonstrating the combined effect on performance. ```bash ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 -epoch 25 Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 76394 lr: 0.000000 loss: 4.350277 eta: 0h0m ./fasttext test model_cooking.bin cooking.valid N 3000 P@1 0.585 R@1 0.255 Number of examples: 3000 ``` ```python model = fasttext.train_supervised(input="cooking.train", lr=1.0, epoch=25) Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 76394 lr: 0.000000 loss: 4.350277 eta: 0h0m model.test("cooking.valid") (3000L, 0.585, 0.255) ``` -------------------------------- ### Accessing labels and training arguments from supervised model Source: https://github.com/facebookresearch/fasttext/blob/main/website/blog/2019-06-25-blog-post.md Illustrates how to interact with the model object returned by `fasttext.train_supervised` to get a list of labels, access word vectors, and retrieve training arguments like epoch, loss, and wordNgrams. ```python import fasttext model = fasttext.train_supervised("train.txt") print(model.words) # list of words in dictionary print(model.labels) # list of labels # Accessing training arguments: print(model.epoch) print(model.loss) print(model.wordNgrams) ``` -------------------------------- ### Build fastText using Make Source: https://github.com/facebookresearch/fasttext/blob/main/README.md Instructions for building the fastText library using the 'make' command. This is the preferred method and produces the main 'fasttext' binary. It requires downloading the source code, unzipping it, navigating to the directory, and running 'make'. Customization of compiler macros is possible within the Makefile. ```bash wget https://github.com/facebookresearch/fastText/archive/v0.9.2.zip unzip v0.9.2.zip cd fastText-0.9.2 make ``` -------------------------------- ### Save and Train methods for ProductQuantizer Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1ProductQuantizer.html Methods to persist the ProductQuantizer state to an output stream and to perform the training process on input data. ```cpp void fasttext::ProductQuantizer::save(std::ostream &out); void fasttext::ProductQuantizer::train(int, const real*); ``` -------------------------------- ### Get Word ID (C++) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Dictionary.html Gets the integer ID for a given word. This method is similar to 'find' but might have different behavior for unknown words or specific dictionary states. ```cpp int32_t fasttext::Dictionary::getId( const std::string & _w_ ) const ``` -------------------------------- ### fastText CLI Entry Point and Usage Helpers Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/main_8cc.html The main.cc file acts as the entry point for the fastText command-line tool. It includes core logic for model operations and helper functions to display usage information for each specific command. ```cpp #include #include "fasttext.h" #include "args.h" void printUsage(); void printQuantizeUsage(); void printTestUsage(); void printPredictUsage(); void printPrintWordVectorsUsage(); int main(int argc, char **argv) { // Entry point logic for fastText commands return 0; } void train(int argc, char **argv) { // Training implementation } void predict(int argc, char **argv) { // Prediction implementation } ``` -------------------------------- ### Train fastText with Adjusted Learning Rate Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Modifies the learning rate (-lr) to control how much the model updates after processing each example. This example sets the learning rate to 1.0 for improved training speed and performance. ```bash ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 81469 lr: 0.000000 loss: 6.405640 eta: 0h0m ./fasttext test model_cooking.bin cooking.valid N 3000 P@1 0.563 R@1 0.245 Number of examples: 3000 ``` ```python model = fasttext.train_supervised(input="cooking.train", lr=1.0) Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 81469 lr: 0.000000 loss: 6.405640 eta: 0h0m model.test("cooking.valid") (3000L, 0.563, 0.245) ``` -------------------------------- ### Access fastText Command-Line Help Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Displays the high-level documentation and list of available subcommands for the fastText binary. ```bash ./fasttext ``` -------------------------------- ### Get Word and Sentence Vectors with FastText.js (JavaScript) Source: https://github.com/facebookresearch/fasttext/blob/main/webassembly/doc/examples/misc.html After loading a FastText model, this snippet demonstrates how to obtain the vector representation for a single word and for entire sentences. It shows how to get the vector for 'Hello' and two different sentences. ```javascript import {FastText, addOnPostRun} from "./fasttext.js"; addOnPostRun(() => { let ft = new FastText(); const url = "lid.176.ftz"; ft.loadModel(url).then(model => { /* getWordVector */ let v = model.getWordVector("Hello"); console.log(v); /* getSentenceVector */ let v1 = model.getSentenceVector("Hello"); console.log(v1); let v2 = model.getSentenceVector("Hello this is a sentence"); console.log(v2); }); }); ``` -------------------------------- ### GET /getVector Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1FastText-members.html Retrieves the vector representation for a specific word. ```APIDOC ## GET /getVector ### Description Retrieves the dense vector representation of a given word from the loaded model. ### Method GET ### Endpoint /getVector ### Parameters #### Query Parameters - **word** (string) - Required - The word to retrieve the vector for. ### Response #### Success Response (200) - **vector** (array) - The numerical vector representation. #### Response Example { "word": "apple", "vector": [0.12, -0.45, 0.88, ...] } ``` -------------------------------- ### Download and Prepare Dataset Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Downloads the Stackexchange cooking dataset and splits it into training and validation sets using standard shell commands. ```bash wget https://dl.fbaipublicfiles.com/fasttext/data/cooking.stackexchange.tar.gz && tar xvzf cooking.stackexchange.tar.gz head -n 12404 cooking.stackexchange.txt > cooking.train tail -n 3000 cooking.stackexchange.txt > cooking.valid ``` -------------------------------- ### Get Word ID, Subwords, and Input Vector with FastText.js (JavaScript) Source: https://github.com/facebookresearch/fasttext/blob/main/webassembly/doc/examples/misc.html Demonstrates retrieving the unique integer ID for a word, extracting subword information for a given word, and getting the input vector for a specific word ID. This provides deeper insights into the model's internal representation. ```javascript const printVector = function(predictions, limit) { limit = limit || Infinity; for (let i=0; i { let ft = new FastText(); const url = "lid.176.ftz"; ft.loadModel(url).then(model => { /* getWordId */ console.log(model.getWordId("Hello")); /* getSubwords */ let subWordInformation = model.getSubwords("désinstitutionnalisation"); printVector(subWordInformation[0]); /* getInputVector */ console.log(model.getInputVector(832)); }); }); ``` -------------------------------- ### Download and Prepare Training Data for fastText Source: https://github.com/facebookresearch/fasttext/blob/main/website/blog/2017-10-02-blog-post.md This snippet demonstrates how to download, decompress, and format training data from Tatoeba for use with fastText. It involves using wget, bunzip2, tar, and awk commands to prepare a 'train.txt' and 'valid.txt' file. ```bash >> wget http://downloads.tatoeba.org/exports/sentences.tar.bz2 >> bunzip2 sentences.tar.bz2 >> tar xvf sentences.tar >> awk -F"\t" '{print"__label__"$2" "$3}' < sentences.csv | shuf > all.txt >> head -n 10000 all.txt > valid.txt >> tail -n +10001 all.txt > train.txt ``` -------------------------------- ### GET /wordVectors Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/fasttext_8h_source.html Retrieves or prints word vectors from the current model. ```APIDOC ## GET /wordVectors ### Description Retrieves the word vectors associated with the current model state. ### Method GET ### Endpoint /wordVectors ### Parameters None ### Response #### Success Response (200) - **vectors** (object) - The computed word vector matrix. ``` -------------------------------- ### POST /train Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/main_8cc.html Initializes the training process for a fastText model based on the provided command-line arguments. ```APIDOC ## POST /train ### Description Initiates the model training process using the specified configuration parameters passed via command-line arguments. ### Method POST ### Endpoint /train ### Parameters #### Request Body - **argc** (int) - Required - The number of arguments provided. - **argv** (char**) - Required - An array of character strings representing the training configuration. ### Request Example { "argc": 3, "argv": ["fasttext", "supervised", "-input data.txt"] } ### Response #### Success Response (200) - **status** (string) - Returns success upon completion of training process. ``` -------------------------------- ### GET /tokenize Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.md Tokenizes a given string of text based on fastText's internal rules. ```APIDOC ## GET /tokenize ### Description Splits a string into a list of tokens based on fastText's ASCII-based tokenization logic. ### Method GET ### Parameters #### Query Parameters - **text** (string) - Required - The input string to tokenize ### Response #### Success Response (200) - **tokens** (list) - List of string tokens ### Response Example { "tokens": ["hello", "world"] } ``` -------------------------------- ### Build WebAssembly Binaries for fastText Source: https://github.com/facebookresearch/fasttext/blob/main/webassembly/README.md Builds the WebAssembly binaries for fastText after navigating into the cloned repository directory. This command utilizes the Makefile to compile the C++ source code into WebAssembly modules. ```bash cd fastText make wasm ``` -------------------------------- ### Migrating from fasttext.cbow to fasttext.train_unsupervised Source: https://github.com/facebookresearch/fasttext/blob/main/website/blog/2019-06-25-blog-post.md Provides an example of how to update code that used the older `fasttext.cbow` function to the new `fasttext.train_unsupervised` function, including model saving. ```python # Old way: # fasttext.cbow("train.txt", "model_file", lr=0.05, dim=100, ws=5, epoch=5) # New way: model = fasttext.train_unsupervised("train.txt", model='cbow', lr=0.05, dim=100, ws=5, epoch=5) model.save_model("model_file.bin") ``` -------------------------------- ### fastText Model Initialization and Utility Methods Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/model_8h_source.html Covers methods for initializing various components of the model and utility functions. This includes initializing sigmoid and log tables, setting target counts, initializing negative sampling tables, building the prediction tree, and quantizing the model. ```cpp void initSigmoid(); void initLog(); void setTargetCounts(const std::vector&); void initTableNegatives(const std::vector&); void buildTree(const std::vector&); void setQuantizePointer(std::shared_ptr, std::shared_ptr, bool); ``` -------------------------------- ### Get Word Counts (C++) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Dictionary.html Retrieves the counts of words or ngrams of a specified type. This is a private helper function used for internal calculations related to word frequencies. ```cpp std::vector< int64_t > fasttext::Dictionary::getCounts( [entry_type](namespacefasttext.html#a532eedeee97e8d66a96b519d165f4eb7) _type_ ) const ``` -------------------------------- ### POST /load_save Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1ProductQuantizer.html Handles persistence of the ProductQuantizer model state. ```APIDOC ## POST /load_save ### Description Loads or saves the model configuration and centroids to/from an input/output stream. ### Method POST ### Endpoint /load_save ### Parameters #### Request Body - **action** (string) - Required - "load" or "save" - **stream** (binary) - Required - The data stream ### Request Example { "action": "save" } ### Response #### Success Response (200) - **result** (string) - "operation_complete" #### Response Example { "result": "operation_complete" } ``` -------------------------------- ### Get Line from Input Stream (C++) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Dictionary.html Reads a line from an input stream and parses it into words, word hashes, and labels. This function handles tokenization and prepares the input for further processing. ```cpp int32_t fasttext::Dictionary::getLine( std::istream & _in_, std::vector< int32_t > & _words_, std::vector< int32_t > & _word_hashes_, std::vector< int32_t > & _labels_, std::minstd_rand & _rng_ ) const ``` ```cpp int32_t fasttext::Dictionary::getLine( std::istream & _in_, std::vector< int32_t > & _words_, std::vector< int32_t > & _labels_, ``` -------------------------------- ### Get Label String from ID (C++) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Dictionary.html Retrieves the string representation of a label given its integer ID. This is used for converting internal label representations back to human-readable strings. ```cpp std::string fasttext::Dictionary::getLabel( int32_t _lid_ ) const ``` -------------------------------- ### Initialize Documentation UI Components Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/functions_g.html Initializes the Doxygen documentation interface, including the search box, navigation tree, and menu system. These functions are typically triggered on document ready to ensure the UI is interactive. ```javascript $(document).ready(initResizable); $(function() { initMenu('',true,false,'search.php','Search'); $(document).ready(function() { init_search(); }); }); $(document).ready(function(){ initNavTree('functions_g.html',''); }); ``` -------------------------------- ### Quantize fastText model to reduce size Source: https://github.com/facebookresearch/fasttext/blob/main/docs/faqs.md Reduces the size of a trained fastText model using the quantization feature. This command takes an existing model and outputs a compressed version. ```bash ./fasttext quantize -output model ``` -------------------------------- ### Configure fastText Build with CMake Source: https://github.com/facebookresearch/fasttext/blob/main/CMakeLists.txt This script defines the project requirements, source files, and build targets for fastText. It includes settings for C++17, optimization flags, and library installation paths. ```cmake cmake_minimum_required(VERSION 2.8.9) project(fasttext) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_FLAGS " -pthread -std=c++17 -funroll-loops -O3 -march=native") add_library(fasttext-shared SHARED ${SOURCE_FILES} ${HEADER_FILES}) add_library(fasttext-static STATIC ${SOURCE_FILES} ${HEADER_FILES}) add_executable(fasttext-bin src/main.cc) target_link_libraries(fasttext-bin pthread fasttext-static) ``` -------------------------------- ### List fastText command line arguments Source: https://github.com/facebookresearch/fasttext/blob/main/docs/options.md Invokes the fastText supervised mode without arguments to display all available configuration options, their descriptions, and default values. This is useful for verifying the current environment's parameter defaults. ```bash $ ./fasttext supervised ``` -------------------------------- ### FastText Configuration Arguments Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/args_8h.html This section details the arguments and configuration options available for the fastText library, as defined in the args.h file. ```APIDOC ## fasttext::Args Class ### Description Represents the arguments and configuration options for the fastText library. ### Method N/A (Class definition) ### Endpoint N/A (Class definition) ### Parameters This class encapsulates various parameters related to model training and prediction. Key parameters include: #### Model Type - **model_name** (enum: cbow, sg, sup) - Specifies the type of model to use (e.g., continuous bag-of-words, skip-gram, supervised). #### Loss Function - **loss_name** (enum: hs, ns, softmax) - Specifies the loss function to use (e.g., hierarchical softmax, negative sampling, softmax). ### Request Example N/A (Class definition) ### Response N/A (Class definition) ``` -------------------------------- ### Initialize Doxygen Documentation UI Components Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/namespacemembers.html These JavaScript snippets initialize the Doxygen-generated documentation interface, including the search box, navigation tree, and menu system. They rely on the jQuery library to execute functions once the DOM is fully loaded. ```javascript $(document).ready(initResizable); $(function() { initMenu('',true,false,'search.php','Search'); $(document).ready(function() { init_search(); }); }); $(document).ready(function(){ initNavTree('namespacemembers.html',''); }); ``` -------------------------------- ### Train fastText with Word N-grams Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Improves model performance by incorporating word n-grams, particularly useful for sequence-aware tasks like sentiment analysis. This example uses wordNgrams=2 to include bigrams. ```bash ./fasttext supervised -input cooking.train -output model_cooking -lr 1.0 -epoch 25 -wordNgrams 2 Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 75366 lr: 0.000000 loss: 3.226064 eta: 0h0m ./fasttext test model_cooking.bin cooking.valid N 3000 P@1 0.599 R@1 0.261 Number of examples: 3000 ``` ```python model = fasttext.train_supervised(input="cooking.train", lr=1.0, epoch=25, wordNgrams=2) Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 75366 lr: 0.000000 loss: 3.226064 eta: 0h0m model.test("cooking.valid") (3000L, 0.599, 0.261) ``` -------------------------------- ### Get Number of Elements in fastText QMatrix Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/qmatrix_8h_source.html The `getN` method returns the number of elements (rows) in the `QMatrix`. This is a constant method, meaning it does not modify the state of the `QMatrix` object. It returns a `int64_t` representing the count. ```cpp int64_t getN() const; **Definition:** qmatrix.cc:87 ``` -------------------------------- ### Train fastText with Increased Epochs Source: https://github.com/facebookresearch/fasttext/blob/main/docs/supervised-tutorial.md Increases the number of training epochs using the -epoch option for potentially better model accuracy. This example shows how to set epochs to 25 for both command-line and Python training. ```bash ./fasttext supervised -input cooking.train -output model_cooking -epoch 25 Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 77633 lr: 0.000000 loss: 7.147976 eta: 0h0m ``` ```python import fasttext model = fasttext.train_supervised(input="cooking.train", epoch=25) Read 0M words Number of words: 9012 Number of labels: 734 Progress: 100.0% words/sec/thread: 77633 lr: 0.000000 loss: 7.147976 eta: 0h0m ``` -------------------------------- ### Display Help and Save Configuration Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Args.html Methods for printing help documentation to the console and serializing the current argument configuration to an output stream. ```C++ void printHelp(); void printQuantizationHelp(); void printTrainingHelp(); void save(std::ostream & out); ``` -------------------------------- ### ProductQuantizer Class Members and Methods Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/productquantizer_8h_source.html A collection of core members and methods for the ProductQuantizer class. These include configuration parameters like nbits_ and ksub_, as well as functional methods for training and computing quantization codes. ```cpp class ProductQuantizer { public: ProductQuantizer(); void train(int, const real *); void compute_code(const real *, uint8_t *) const; void compute_codes(const real *, uint8_t *, int32_t) const; void Estep(const real *, const real *, uint8_t *, int32_t, int32_t) const; void load(std::istream &); private: const int32_t nbits_; const int32_t ksub_; const int32_t max_points_; const int32_t max_points_per_cluster_; int32_t dim_; int32_t lastdsub_; std::vector centroids_; }; ``` -------------------------------- ### Initialize fastText in HTML Source: https://github.com/facebookresearch/fasttext/blob/main/docs/webassembly-module.md A minimal HTML implementation showing how to import the fastText wrapper and initialize the module using the addOnPostRun hook. ```html ``` -------------------------------- ### C++: fastText Utility Functions (size, seek) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/utils_8h_source.html Defines utility functions for fastText, specifically 'size' to get the size of an ifstream and 'seek' to reposition the stream. These functions are essential for file processing within the fastText library. ```cpp #ifndef FASTTEXT_UTILS_H #define FASTTEXT_UTILS_H #include namespace fasttext { namespace utils { int64_t size(std::ifstream&); void seek(std::ifstream&, int64_t); } } #endif ``` ```cpp int64_t size(std::ifstream &ifs) { // Implementation to get file size } void seek(std::ifstream &ifs, int64_t pos) { // Implementation to seek in file stream } ``` -------------------------------- ### Clone fastText Repository Source: https://github.com/facebookresearch/fasttext/blob/main/webassembly/README.md Clones the fastText GitHub repository to your local machine. This command downloads the source code necessary for building the WebAssembly binaries. ```bash git clone git@github.com:facebookresearch/fastText.git ``` -------------------------------- ### Accessing classification model information and testing Source: https://github.com/facebookresearch/fasttext/blob/main/python/README.md Retrieves the list of words and labels from a trained text classification model and evaluates its performance on a test set using the `test` function. It prints the number of examples, precision at 1, and recall at 1. ```python def print_results(N, p, r): print("N\t" + str(N)) print("P@{} {:.3f}".format(1, p)) print("R@{} {:.3f}".format(1, r)) print(model.words) print(model.labels) print_results(*model.test('test.txt')) ``` -------------------------------- ### fastText QMatrix Quantization and I/O Functions Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1QMatrix.html Provides documentation for functions related to quantizing matrices and handling input/output operations. This includes quantizing a matrix, quantizing norms, loading from a stream, and saving to a stream. These are key for efficient storage and retrieval of quantized data. ```cpp void [quantize](classfasttext_1_1QMatrix.html#ab9ae1914dc1b72e305880a8c22626afc) (const [Matrix](classfasttext_1_1Matrix.html) &) void [quantizeNorm](classfasttext_1_1QMatrix.html#a0e4d84be1c6cd0cbfc4568f905961017) (const [Vector](classfasttext_1_1Vector.html) &) void [load](classfasttext_1_1QMatrix.html#a03c039b81b5aaed30d95149de9379998) (std::istream &) void [save](classfasttext_1_1QMatrix.html#a00267b43ee5eefc92948c654fb9fc9f1) (std::ostream &) ``` -------------------------------- ### Configure fastText autotune parameters Source: https://github.com/facebookresearch/fasttext/blob/main/docs/options.md Lists the specific arguments required to activate hyperparameter optimization. Requires a validation file provided via the -autotune-validation flag to enable the autotuning process. ```text -autotune-validation validation file to be used for evaluation -autotune-metric metric objective {f1, f1:labelname} [f1] -autotune-predictions number of predictions used for evaluation [1] -autotune-duration maximum duration in seconds [300] -autotune-modelsize constraint model file size [] (empty = do not quantize) ``` -------------------------------- ### FastText Configuration Parameters Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/args_8h_source.html This section outlines the various parameters used to configure the FastText model for training and testing. ```APIDOC ## FastText Configuration Parameters ### Description This section details the configurable parameters for the FastText library, covering aspects like data input/output, model architecture, training hyperparameters, and verbosity. ### Method N/A (Configuration parameters, not an API endpoint) ### Endpoint N/A ### Parameters #### Input/Output Parameters - **input** (string) - Required - Path to the input training file. - **test** (string) - Required - Path to the input test file. - **output** (string) - Required - Path to the output directory for saving the model and predictions. #### Training Hyperparameters - **lr** (double) - Optional - Learning rate for model training. Default is 0.1. - **lrUpdateRate** (int) - Optional - Learning rate update rate. Default is 100. - **dim** (int) - Optional - Dimension of the word vectors. Default is 100. - **ws** (int) - Optional - Size of the context window. Default is 5. - **epoch** (int) - Optional - Number of training epochs. Default is 5. - **minCount** (int) - Optional - Minimum number of word occurrences to consider. Default is 5. - **minCountLabel** (int) - Optional - Minimum number of label occurrences to consider. Default is 1. - **neg** (int) - Optional - Number of negative samples. Default is 5. - **wordNgrams** (int) - Optional - Size of word ngrams. Default is 1. - **loss** (loss_name) - Optional - Loss function to use. Options: `hs`, `ns`, `softmax`. Default is `softmax`. - **model** (model_name) - Optional - Model name. Options: `cbow`, `sg`, `sup`. Default is `sg`. - **bucket** (int) - Optional - Size of the bucket for hashing ngrams. Default is 2000000. - **minn** (int) - Optional - Minimum length of character ngrams. Default is 3. - **maxn** (int) - Optional - Maximum length of character ngrams. Default is 6. #### Other Parameters - **thread** (int) - Optional - Number of threads to use for training. Default is 12. - **t** (double) - Optional - Sampling threshold for highly frequent words. Default is 1.0. - **label** (string) - Optional - Prefix for labels. Default is `__label__`. - **verbose** (int) - Optional - Verbosity level. Default is 2. ### Request Example ```json { "input": "train.txt", "test": "test.txt", "output": "model", "dim": 150, "ws": 7, "epoch": 10, "loss": "ns", "model": "cbow" } ``` ### Response #### Success Response (N/A) This section is not applicable as these are configuration parameters and not an API endpoint response. #### Response Example (N/A) N/A ``` -------------------------------- ### Set Skipgram Model Thread Count Source: https://github.com/facebookresearch/fasttext/blob/main/docs/unsupervised-tutorials.md This example demonstrates how to specify the number of threads used during skipgram model training. fastText is multi-threaded, and setting the thread count can optimize performance based on available CPU cores. The default is 12 threads. ```bash $ ./fasttext skipgram -input data/fil9 -output result/fil9 -thread 4 ``` ```python import fasttext model = fasttext.train_unsupervised('data/fil9', thread=4) ``` -------------------------------- ### POST /test Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/main_8cc.html Evaluates a pre-trained fastText model against a test dataset using provided command-line arguments. ```APIDOC ## POST /test ### Description Evaluates the performance of a trained model on a specific test set. ### Method POST ### Endpoint /test ### Parameters #### Request Body - **argc** (int) - Required - The number of arguments provided. - **argv** (char**) - Required - An array of character strings representing the test configuration. ### Request Example { "argc": 3, "argv": ["fasttext", "test", "model.bin"] } ### Response #### Success Response (200) - **metrics** (object) - Precision and recall metrics for the evaluated model. ``` -------------------------------- ### fastText Model Initialization and Training Methods Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Model-members.html This snippet outlines key methods for initializing and training a fastText model. It includes functions for building the model tree, computing output probabilities, and handling negative sampling. These methods are crucial for the core functionality of the fastText library. ```cpp private: void binaryLogistic(int32_t, bool, real); void buildTree(const std::vector< int64_t > &); void computeHidden(const std::vector< int32_t > &, Vector &) const; void computeOutputSoftmax(Vector &, Vector &) const; void computeOutputSoftmax(); void dfs(int32_t, int32_t, real, std::vector< std::pair< real, int32_t >> &, Vector &) const; void findKBest(int32_t, std::vector< std::pair< real, int32_t >> &, Vector &, Vector &) const; void hierarchicalSoftmax(int32_t, real); void initLog(); void initSigmoid(); void initTableNegatives(const std::vector< int64_t > &); Model(std::shared_ptr< Matrix >, std::shared_ptr< Matrix >, std::shared_ptr< Args >, int32_t); void negativeSampling(int32_t, real); ``` -------------------------------- ### Load and Predict Language with FastText via HuggingFace Source: https://github.com/facebookresearch/fasttext/blob/main/docs/language-identification.md This Python snippet demonstrates how to download a FastText language identification model from the HuggingFace Hub and use it to predict the language of a given text. It shows how to load the model and perform predictions, including getting the top-k predictions. ```python import fasttext from huggingface_hub import hf_hub_download model_path = hf_hub_download(repo_id="facebook/fasttext-language-identification", filename="model.bin") model = fasttext.load_model(model_path) print(model.predict("Hello, world!")) print(model.predict("Hello, world!", k=5)) ``` -------------------------------- ### fasttext::Args Constructor and Member Functions (C++) Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1Args.html This snippet covers the constructor and key member functions of the fasttext::Args class. It includes methods for parsing command-line arguments, printing various help messages, and loading/saving configurations from/to streams. These functions are essential for configuring fastText models. ```cpp class Args { public: Args(); void parseArgs(int argc, char **argv); void printHelp(); void printBasicHelp(); void printDictionaryHelp(); void printTrainingHelp(); void printQuantizationHelp(); void save(std::ostream &); void load(std::istream &); // Public Attributes std::string input; std::string test; std::string output; // ... other attributes }; fasttext::Args::Args() { // Constructor implementation } void fasttext::Args::parseArgs(int argc, char **argv) { // Implementation to parse command-line arguments } void fasttext::Args::printHelp() { // Implementation to print general help } void fasttext::Args::printBasicHelp() { // Implementation to print basic help } void fasttext::Args::printDictionaryHelp() { // Implementation to print dictionary-specific help } void fasttext::Args::save(std::ostream &out) { // Implementation to save arguments to an output stream } void fasttext::Args::load(std::istream &in) { // Implementation to load arguments from an input stream } ``` -------------------------------- ### Load FastText Word Vectors in Python Source: https://github.com/facebookresearch/fasttext/blob/main/docs/english-vectors.md This Python function loads pre-trained word vectors from a file. It parses the header to get the vocabulary size and vector dimension, then reads each line to store words and their corresponding float vectors in a dictionary. It handles potential encoding errors. ```python import io def load_vectors(fname): fin = io.open(fname, 'r', encoding='utf-8', newline='\n', errors='ignore') n, d = map(int, fin.readline().split()) data = {} for line in fin: tokens = line.rstrip().split(' ') data[tokens[0]] = map(float, tokens[1:]) return data ``` -------------------------------- ### ProductQuantizer internal configuration and state Source: https://github.com/facebookresearch/fasttext/blob/main/website/static/docs/en/html/classfasttext_1_1ProductQuantizer.html Private member variables defining the quantization parameters, including bit depth, cluster counts, and random number generation state. ```cpp std::vector centroids_; int32_t dim_; int32_t dsub_; const real eps_ = 1e-7; const int32_t ksub_ = 1 << nbits_; int32_t lastdsub_; const int32_t max_points_ = max_points_per_cluster_ * ksub_; const int32_t max_points_per_cluster_ = 256; const int32_t nbits_ = 8; const int32_t niter_ = 25; int32_t nsubq_; std::minstd_rand rng; const int32_t seed_ = 1234; ``` -------------------------------- ### Download fastText Models (Command Line & Python) Source: https://github.com/facebookresearch/fasttext/blob/main/docs/crawl-vectors.md Download pre-trained fastText word vector models for a specified language. This can be done directly from the command line using a download script or programmatically within a Python script. Ensure the fastText Python package is installed before use. ```bash ./download_model.py en # English Downloading https://dl.fbaipublicfiles.com/fasttext/vectors-crawl/cc.en.300.bin.gz (19.78%) [=========> ] ``` ```python import fasttext.util fasttext.util.download_model('en', if_exists='ignore') # English ft = fasttext.load_model('cc.en.300.bin') ``` -------------------------------- ### Load and Predict with FastText Model Source: https://github.com/facebookresearch/fasttext/blob/main/docs/webassembly-module.md Demonstrates how to initialize the FastText WebAssembly module, load a model file, and perform text predictions. It includes a helper function to iterate over prediction results returned as a vector. ```javascript import {FastText, addOnPostRun} from "./fasttext.js"; const printVector = function(predictions) { for (let i=0; i { let ft = new FastText(); const url = "lid.176.ftz"; ft.loadModel(url).then(model => { console.log("Model loaded."); let text = "Bonjour à tous. Ceci est du français"; printVector(model.predict(text, 5, 0.0)); }); }); ``` -------------------------------- ### Configure Skipgram Model Parameters (Subword Size and Dimension) Source: https://github.com/facebookresearch/fasttext/blob/main/docs/unsupervised-tutorials.md This example shows how to train a skipgram model with custom subword sizes (minn, maxn) and vector dimensions (dim). The skipgram model predicts a target word from a nearby word. Adjusting these parameters can impact model performance and training efficiency. ```bash $ ./fasttext skipgram -input data/fil9 -output result/fil9 -minn 2 -maxn 5 -dim 300 ``` ```python import fasttext model = fasttext.train_unsupervised('data/fil9', minn=2, maxn=5, dim=300) ``` -------------------------------- ### Test Model Performance (CLI & Python) Source: https://github.com/facebookresearch/fasttext/blob/main/docs/autotune.md Evaluates the performance of a trained FastText model on a given validation file. The command-line uses './fasttext test', and the Python version uses the 'model.test()' method, returning metrics like N (number of examples), P@1 (precision at 1), and R@1 (recall at 1). ```sh >> ./fasttext test model_cooking.bin cooking.valid ``` ```python >>> model.test("cooking.valid") (3000L, 0.666, 0.288) ```