#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 65 of file weightmatrix.h.
◆ WeightMatrix()
tesseract::WeightMatrix::WeightMatrix |
( |
| ) |
|
|
inline |
Definition at line 67 of file weightmatrix.h.
67 : int_mode_(
false), use_adam_(
false) {}
◆ AddDeltas()
void tesseract::WeightMatrix::AddDeltas |
( |
const WeightMatrix & |
other | ) |
|
◆ ConvertToInt()
void tesseract::WeightMatrix::ConvertToInt |
( |
| ) |
|
Definition at line 100 of file weightmatrix.cpp.
103 int dim2 = wi_.
dim2();
104 for (
int t = 0; t < wi_.
dim1(); ++t) {
105 double* f_line = wf_[t];
106 int8_t* i_line = wi_[t];
107 double max_abs = 0.0;
108 for (
int f = 0; f < dim2; ++f) {
109 double abs_val = fabs(f_line[f]);
110 if (abs_val > max_abs) max_abs = abs_val;
112 double scale = max_abs / INT8_MAX;
114 if (scale == 0.0) scale = 1.0;
115 for (
int f = 0; f < dim2; ++f) {
122 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
void ResizeNoInit(int size1, int size2, int pad=0)
void Resize(int size1, int size2, const T &empty)
static IntSimdMatrix * GetFastestMultiplier()
int IntCastRounded(double x)
void init_to_size(int size, T t)
◆ CountAlternators()
void tesseract::WeightMatrix::CountAlternators |
( |
const WeightMatrix & |
other, |
|
|
double * |
same, |
|
|
double * |
changed |
|
) |
| const |
Definition at line 315 of file weightmatrix.cpp.
317 int num_outputs = updates_.
dim1();
318 int num_inputs = updates_.
dim2();
321 for (
int i = 0; i < num_outputs; ++i) {
322 const double* this_i = updates_[i];
323 const double* other_i = other.updates_[i];
324 for (
int j = 0; j < num_inputs; ++j) {
325 double product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 346 of file weightmatrix.cpp.
349 for (
int i = 0; i < wi_.
dim1(); ++i) {
350 for (
int j = 0; j < wi_.
dim2(); ++j) {
351 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
355 for (
int i = 0; i < wf_.
dim1(); ++i) {
356 for (
int j = 0; j < wf_.
dim2(); ++j) {
357 HistogramWeight(wf_[i][j], &histogram);
const int kHistogramBuckets
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 165 of file weightmatrix.cpp.
167 if (fp->FRead(&mode,
sizeof(mode), 1) != 1)
return false;
175 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
181 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 189 of file weightmatrix.cpp.
196 for (
int i = 0; i < old_scales.
size(); ++i) scales_[i] = old_scales[i];
bool DeSerialize(bool swap, FILE *fp)
void resize_no_init(int size)
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 367 of file weightmatrix.cpp.
380 for (
int k = 0; k < n; ++k) total += u[k] * v[k];
static bool IsAVXAvailable()
static bool IsSSEAvailable()
double DotProductSSE(const double *u, const double *v, int n)
double DotProductAVX(const double *u, const double *v, int n)
◆ FloatToDouble()
Definition at line 387 of file weightmatrix.cpp.
389 int dim1 = wf.
dim1();
390 int dim2 = wf.
dim2();
392 for (
int i = 0; i < dim1; ++i) {
393 const float* wfi = wf[i];
394 double* wdi = (*wd)[i];
395 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 127 of file weightmatrix.cpp.
128 int no = int_mode_ ? wi_.
dim1() : wf_.
dim1();
129 int ni = int_mode_ ? wi_.
dim2() : wf_.
dim2();
131 updates_.
Resize(no, ni, 0.0);
133 if (use_adam_) dw_sq_sum_.
Resize(no, ni, 0.0);
void Resize(int size1, int size2, const T &empty)
void Transpose(const GENERIC_2D_ARRAY< double > &input)
◆ InitWeightsFloat()
int tesseract::WeightMatrix::InitWeightsFloat |
( |
int |
no, |
|
|
int |
ni, |
|
|
bool |
use_adam, |
|
|
float |
weight_range, |
|
|
TRand * |
randomizer |
|
) |
| |
Definition at line 51 of file weightmatrix.cpp.
55 if (randomizer !=
nullptr) {
56 for (
int i = 0; i < no; ++i) {
57 for (
int j = 0; j < ni; ++j) {
58 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 216 of file weightmatrix.cpp.
218 MatrixDotVectorInternal(wf_,
true,
false, u, v);
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const int8_t * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 221 of file weightmatrix.cpp.
224 multiplier_->MatrixDotVector(wi_, scales_, u, v);
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const double * |
v, |
|
|
double * |
inout |
|
) |
| |
Definition at line 229 of file weightmatrix.cpp.
233 const double* u = wf_[0];
234 for (
int i = 0; i < n; ++i) {
235 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 72 of file weightmatrix.cpp.
74 int old_no = wf_.
dim1();
75 int new_no = code_map.size();
77 std::vector<double> means(ni, 0.0);
78 for (
int c = 0; c < old_no; ++c) {
79 const double* weights = wf_[c];
80 for (
int i = 0; i < ni; ++i) means[i] += weights[i];
82 for (
double& mean : means) mean /= old_no;
85 for (
int dest = 0; dest < new_no; ++dest) {
86 int src = code_map[dest];
87 const double* src_data = src >= 0 ? old_wf[src] : means.data();
88 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 92 of file weightmatrix.h.
93 if (multiplier_ ==
nullptr)
return size;
94 return multiplier_->RoundInputs(size);
◆ Serialize()
bool tesseract::WeightMatrix::Serialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| const |
Definition at line 146 of file weightmatrix.cpp.
151 if (fp->FWrite(&mode,
sizeof(mode), 1) != 1)
return false;
154 if (!scales_.
Serialize(fp))
return false;
157 if (training && !updates_.
Serialize(fp))
return false;
158 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 253 of file weightmatrix.cpp.
257 int num_outputs = dw_.
dim1();
260 int num_inputs = dw_.
dim2() - 1;
261 int num_samples = u.dim2();
265 #pragma omp parallel for num_threads(4) if (in_parallel) 267 for (
int i = 0; i < num_outputs; ++i) {
268 double* dwi = dw_[i];
269 const double* ui = u[i];
270 for (
int j = 0; j < num_inputs; ++j) {
275 for (
int k = 0; k < num_samples; ++k) total += ui[k];
276 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 283 of file weightmatrix.cpp.
287 learning_rate *= sqrt(1.0 - pow(adam_beta, num_samples));
288 learning_rate /= 1.0 - pow(momentum, num_samples);
290 if (use_adam_ && num_samples > 0 && momentum > 0.0) {
292 dw_ *= learning_rate * (1.0 - momentum);
293 updates_ *= momentum;
297 dw_ *= learning_rate;
299 if (momentum > 0.0) wf_ += updates_;
300 if (momentum >= 0.0) updates_ *= momentum;
void Transpose(const GENERIC_2D_ARRAY< double > &input)
const int kAdamCorrectionIterations
const double kAdamEpsilon
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, T epsilon)
void SumSquares(const GENERIC_2D_ARRAY< T > &src, T decay_factor)
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 243 of file weightmatrix.cpp.
245 MatrixDotVectorInternal(wf_t_,
false,
true, u, v);
The documentation for this class was generated from the following files: