model.h
Go to the documentation of this file.
#ifndef FASTTEXT_MODEL_H
#define FASTTEXT_MODEL_H
#include <vector>
#include <random>
#include <utility>
#include <memory>
#include "args.h"
#include "matrix.h"
#include "vector.h"
#include "qmatrix.h"
#include "real.h"
#define SIGMOID_TABLE_SIZE 512
#define MAX_SIGMOID 8
#define LOG_TABLE_SIZE 512
namespace fasttext {
struct Node {
int32_t parent;
int32_t left;
int32_t right;
int64_t count;
bool binary;
};
class Model {
private:
std::shared_ptr<Matrix> wi_;
std::shared_ptr<Matrix> wo_;
std::shared_ptr<QMatrix> qwi_;
std::shared_ptr<QMatrix> qwo_;
std::shared_ptr<Args> args_;
Vector hidden_;
Vector output_;
Vector grad_;
int32_t hsz_;
int32_t osz_;
real loss_;
int64_t nexamples_;
real* t_sigmoid;
real* t_log;
// used for negative sampling:
std::vector<int32_t> negatives;
size_t negpos;
// used for hierarchical softmax:
std::vector< std::vector<int32_t> > paths;
std::vector< std::vector<bool> > codes;
std::vector<Node> tree;
static bool comparePairs(const std::pair<real, int32_t>&,
const std::pair<real, int32_t>&);
int32_t getNegative(int32_t target);
void initSigmoid();
void initLog();
static const int32_t NEGATIVE_TABLE_SIZE = 10000000;
public:
Model(std::shared_ptr<Matrix>, std::shared_ptr<Matrix>,
std::shared_ptr<Args>, int32_t);
~Model();
real binaryLogistic(int32_t, bool, real);
real negativeSampling(int32_t, real);
real hierarchicalSoftmax(int32_t, real);
real softmax(int32_t, real);
void predict(const std::vector<int32_t>&, int32_t,
std::vector<std::pair<real, int32_t>>&,
Vector&, Vector&) const;
void predict(const std::vector<int32_t>&, int32_t,
std::vector<std::pair<real, int32_t>>&);
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 update(const std::vector<int32_t>&, int32_t, real);
void computeHidden(const std::vector<int32_t>&, Vector&) const;
void computeOutputSoftmax(Vector&, Vector&) const;
void computeOutputSoftmax();
void setTargetCounts(const std::vector<int64_t>&);
void initTableNegatives(const std::vector<int64_t>&);
void buildTree(const std::vector<int64_t>&);
real getLoss() const;
real sigmoid(real) const;
real log(real) const;
std::minstd_rand rng;
bool quant_;
void setQuantizePointer(std::shared_ptr<QMatrix>, std::shared_ptr<QMatrix>, bool);
};
}
#endif
fasttext::Model::t_sigmoid
real * t_sigmoid
Definition: model.h:52
fasttext::Model::loss_
real loss_
Definition: model.h:50
fasttext::Node::parent
int32_t parent
Definition: model.h:31
fasttext::Model::qwo_
std::shared_ptr< QMatrix > qwo_
Definition: model.h:43
fasttext::Node
Definition: model.h:30
fasttext::Model::rng
std::minstd_rand rng
Definition: model.h:103
fasttext::Node::right
int32_t right
Definition: model.h:33
fasttext::Model::tree
std::vector< Node > tree
Definition: model.h:60
fasttext
Definition: args.cc:17
fasttext::Vector
Definition: vector.h:23
fasttext::Node::count
int64_t count
Definition: model.h:34
fasttext::loss_name::softmax
fasttext::Model::t_log
real * t_log
Definition: model.h:53
predict
void predict(int argc, char **argv)
Definition: main.cc:138
fasttext::Model
Definition: model.h:38
real.h
fasttext::Model::codes
std::vector< std::vector< bool > > codes
Definition: model.h:59
matrix.h
fasttext::Model::hsz_
int32_t hsz_
Definition: model.h:48
fasttext::Model::quant_
bool quant_
Definition: model.h:104
fasttext::Model::grad_
Vector grad_
Definition: model.h:47
fasttext::Model::qwi_
std::shared_ptr< QMatrix > qwi_
Definition: model.h:42
fasttext::Model::negpos
size_t negpos
Definition: model.h:56
fasttext::Model::paths
std::vector< std::vector< int32_t > > paths
Definition: model.h:58
vector.h
fasttext::Node::binary
bool binary
Definition: model.h:35
fasttext::Model::wo_
std::shared_ptr< Matrix > wo_
Definition: model.h:41
fasttext::Model::wi_
std::shared_ptr< Matrix > wi_
Definition: model.h:40
fasttext::Model::output_
Vector output_
Definition: model.h:46
args.h
fasttext::real
float real
Definition: real.h:15
fasttext::Model::nexamples_
int64_t nexamples_
Definition: model.h:51
fasttext::Node::left
int32_t left
Definition: model.h:32
fasttext::Model::args_
std::shared_ptr< Args > args_
Definition: model.h:44
fasttext::Model::negatives
std::vector< int32_t > negatives
Definition: model.h:55
fasttext::Model::hidden_
Vector hidden_
Definition: model.h:45
fasttext::Model::osz_
int32_t osz_
Definition: model.h:49
qmatrix.h