#include <weightmatrix.h>
|
| WeightMatrix () |
|
int | InitWeightsFloat (int no, int ni, bool use_adam, float weight_range, TRand *randomizer) |
|
int | RemapOutputs (const std::vector< int > &code_map) |
|
void | ConvertToInt () |
|
int | RoundInputs (int size) const |
|
bool | is_int_mode () const |
|
int | NumOutputs () const |
|
const double * | GetWeights (int index) const |
|
double | GetDW (int i, int j) const |
|
void | InitBackward () |
|
bool | Serialize (bool training, TFile *fp) const |
|
bool | DeSerialize (bool training, TFile *fp) |
|
bool | DeSerializeOld (bool training, TFile *fp) |
|
void | MatrixDotVector (const double *u, double *v) const |
|
void | MatrixDotVector (const int8_t *u, double *v) const |
|
void | MultiplyAccumulate (const double *v, double *inout) |
|
void | VectorDotMatrix (const double *u, double *v) const |
|
void | SumOuterTransposed (const TransposedArray &u, const TransposedArray &v, bool parallel) |
|
void | Update (double learning_rate, double momentum, double adam_beta, int num_samples) |
|
void | AddDeltas (const WeightMatrix &other) |
|
void | CountAlternators (const WeightMatrix &other, double *same, double *changed) const |
|
void | Debug2D (const char *msg) |
|
Definition at line 66 of file weightmatrix.h.
◆ WeightMatrix()
tesseract::WeightMatrix::WeightMatrix |
( |
| ) |
|
|
inline |
Definition at line 68 of file weightmatrix.h.
68 : int_mode_(
false), use_adam_(
false) {}
◆ AddDeltas()
void tesseract::WeightMatrix::AddDeltas |
( |
const WeightMatrix & |
other | ) |
|
◆ ConvertToInt()
void tesseract::WeightMatrix::ConvertToInt |
( |
| ) |
|
Definition at line 105 of file weightmatrix.cpp.
108 int dim2 = wi_.
dim2();
109 for (
int t = 0; t < wi_.
dim1(); ++t) {
110 double* f_line = wf_[t];
111 int8_t* i_line = wi_[t];
112 double max_abs = 0.0;
113 for (
int f = 0; f < dim2; ++f) {
114 double abs_val = fabs(f_line[f]);
115 if (abs_val > max_abs) max_abs = abs_val;
117 double scale = max_abs / INT8_MAX;
119 if (scale == 0.0) scale = 1.0;
120 for (
int f = 0; f < dim2; ++f) {
127 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
static IntSimdMatrix * GetFastestMultiplier()
void ResizeNoInit(int size1, int size2, int pad=0)
void init_to_size(int size, const T &t)
int IntCastRounded(double x)
void Resize(int size1, int size2, const T &empty)
◆ CountAlternators()
void tesseract::WeightMatrix::CountAlternators |
( |
const WeightMatrix & |
other, |
|
|
double * |
same, |
|
|
double * |
changed |
|
) |
| const |
Definition at line 320 of file weightmatrix.cpp.
322 int num_outputs = updates_.
dim1();
323 int num_inputs = updates_.
dim2();
326 for (
int i = 0; i < num_outputs; ++i) {
327 const double* this_i = updates_[i];
328 const double* other_i = other.updates_[i];
329 for (
int j = 0; j < num_inputs; ++j) {
330 double product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 351 of file weightmatrix.cpp.
354 for (
int i = 0; i < wi_.
dim1(); ++i) {
355 for (
int j = 0; j < wi_.
dim2(); ++j) {
356 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
360 for (
int i = 0; i < wf_.
dim1(); ++i) {
361 for (
int j = 0; j < wf_.
dim2(); ++j) {
362 HistogramWeight(wf_[i][j], &histogram);
DLLSYM void tprintf(const char *format,...)
const int kHistogramBuckets
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 170 of file weightmatrix.cpp.
172 if (!fp->DeSerialize(&mode))
return false;
180 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
186 if (use_adam_ && !dw_sq_sum_.
DeSerialize(fp))
return false;
static IntSimdMatrix * GetFastestMultiplier()
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerializeOld(bool training, TFile *fp)
◆ DeSerializeOld()
bool tesseract::WeightMatrix::DeSerializeOld |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 194 of file weightmatrix.cpp.
201 for (
int i = 0; i < old_scales.
size(); ++i) scales_[i] = old_scales[i];
void resize_no_init(int size)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
static void FloatToDouble(const GENERIC_2D_ARRAY< float > &wf, GENERIC_2D_ARRAY< double > *wd)
◆ DotProduct()
double tesseract::WeightMatrix::DotProduct |
( |
const double * |
u, |
|
|
const double * |
v, |
|
|
int |
n |
|
) |
| |
|
static |
Definition at line 372 of file weightmatrix.cpp.
385 for (
int k = 0; k < n; ++k) total += u[k] * v[k];
double DotProductAVX(const double *u, const double *v, int n)
double DotProductSSE(const double *u, const double *v, int n)
static bool IsSSEAvailable()
static bool IsAVXAvailable()
◆ FloatToDouble()
Definition at line 392 of file weightmatrix.cpp.
394 int dim1 = wf.
dim1();
395 int dim2 = wf.
dim2();
397 for (
int i = 0; i < dim1; ++i) {
398 const float* wfi = wf[i];
399 double* wdi = (*wd)[i];
400 for (
int j = 0; j < dim2; ++j) wdi[j] = static_cast<double>(wfi[j]);
void ResizeNoInit(int size1, int size2, int pad=0)
◆ GetDW()
double tesseract::WeightMatrix::GetDW |
( |
int |
i, |
|
|
int |
j |
|
) |
| const |
|
inline |
◆ GetWeights()
const double* tesseract::WeightMatrix::GetWeights |
( |
int |
index | ) |
const |
|
inline |
◆ InitBackward()
void tesseract::WeightMatrix::InitBackward |
( |
| ) |
|
Definition at line 132 of file weightmatrix.cpp.
133 int no = int_mode_ ? wi_.
dim1() : wf_.
dim1();
134 int ni = int_mode_ ? wi_.
dim2() : wf_.
dim2();
136 updates_.
Resize(no, ni, 0.0);
138 if (use_adam_) dw_sq_sum_.
Resize(no, ni, 0.0);
void Transpose(const GENERIC_2D_ARRAY< double > &input)
void Resize(int size1, int size2, const T &empty)
◆ InitWeightsFloat()
int tesseract::WeightMatrix::InitWeightsFloat |
( |
int |
no, |
|
|
int |
ni, |
|
|
bool |
use_adam, |
|
|
float |
weight_range, |
|
|
TRand * |
randomizer |
|
) |
| |
Definition at line 56 of file weightmatrix.cpp.
60 if (randomizer !=
nullptr) {
61 for (
int i = 0; i < no; ++i) {
62 for (
int j = 0; j < ni; ++j) {
63 wf_[i][j] = randomizer->SignedRand(weight_range);
void Resize(int size1, int size2, const T &empty)
◆ is_int_mode()
bool tesseract::WeightMatrix::is_int_mode |
( |
| ) |
const |
|
inline |
◆ MatrixDotVector() [1/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 221 of file weightmatrix.cpp.
223 MatrixDotVectorInternal(wf_,
true,
false, u, v);
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const int8_t * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 226 of file weightmatrix.cpp.
229 multiplier_->MatrixDotVector(wi_, scales_, u, v);
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const double * |
v, |
|
|
double * |
inout |
|
) |
| |
Definition at line 234 of file weightmatrix.cpp.
238 const double* u = wf_[0];
239 for (
int i = 0; i < n; ++i) {
240 inout[i] += u[i] * v[i];
◆ NumOutputs()
int tesseract::WeightMatrix::NumOutputs |
( |
| ) |
const |
|
inline |
◆ RemapOutputs()
int tesseract::WeightMatrix::RemapOutputs |
( |
const std::vector< int > & |
code_map | ) |
|
Definition at line 77 of file weightmatrix.cpp.
79 int old_no = wf_.
dim1();
80 int new_no = code_map.size();
82 std::vector<double> means(ni, 0.0);
83 for (
int c = 0; c < old_no; ++c) {
84 const double* weights = wf_[c];
85 for (
int i = 0; i < ni; ++i) means[i] += weights[i];
87 for (
double& mean : means) mean /= old_no;
90 for (
int dest = 0; dest < new_no; ++dest) {
91 int src = code_map[dest];
92 const double* src_data = src >= 0 ? old_wf[src] : means.data();
93 memcpy(wf_[dest], src_data, ni *
sizeof(*src_data));
void ResizeNoInit(int size1, int size2, int pad=0)
◆ RoundInputs()
int tesseract::WeightMatrix::RoundInputs |
( |
int |
size | ) |
const |
|
inline |
Definition at line 93 of file weightmatrix.h.
94 if (multiplier_ ==
nullptr)
return size;
95 return multiplier_->RoundInputs(size);
◆ Serialize()
bool tesseract::WeightMatrix::Serialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| const |
Definition at line 151 of file weightmatrix.cpp.
156 if (!fp->Serialize(&mode))
return false;
159 if (!scales_.
Serialize(fp))
return false;
162 if (training && !updates_.
Serialize(fp))
return false;
163 if (training && use_adam_ && !dw_sq_sum_.
Serialize(fp))
return false;
bool Serialize(FILE *fp) const
bool Serialize(FILE *fp) const
◆ SumOuterTransposed()
Definition at line 258 of file weightmatrix.cpp.
262 int num_outputs = dw_.
dim1();
265 int num_inputs = dw_.
dim2() - 1;
266 int num_samples = u.dim2();
270 #pragma omp parallel for num_threads(4) if (in_parallel) 272 for (
int i = 0; i < num_outputs; ++i) {
273 double* dwi = dw_[i];
274 const double* ui = u[i];
275 for (
int j = 0; j < num_inputs; ++j) {
280 for (
int k = 0; k < num_samples; ++k) total += ui[k];
281 dwi[num_inputs] = total;
static double DotProduct(const double *u, const double *v, int n)
◆ Update()
void tesseract::WeightMatrix::Update |
( |
double |
learning_rate, |
|
|
double |
momentum, |
|
|
double |
adam_beta, |
|
|
int |
num_samples |
|
) |
| |
Definition at line 288 of file weightmatrix.cpp.
292 learning_rate *= sqrt(1.0 - pow(adam_beta, num_samples));
293 learning_rate /= 1.0 - pow(momentum, num_samples);
295 if (use_adam_ && num_samples > 0 && momentum > 0.0) {
297 dw_ *= learning_rate * (1.0 - momentum);
298 updates_ *= momentum;
302 dw_ *= learning_rate;
304 if (momentum > 0.0) wf_ += updates_;
305 if (momentum >= 0.0) updates_ *= momentum;
void SumSquares(const GENERIC_2D_ARRAY< T > &src, const T &decay_factor)
const int kAdamCorrectionIterations
void Transpose(const GENERIC_2D_ARRAY< double > &input)
const double kAdamEpsilon
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, const T &epsilon)
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 248 of file weightmatrix.cpp.
250 MatrixDotVectorInternal(wf_t_,
false,
true, u, v);
The documentation for this class was generated from the following files: