#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 *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 94 of file weightmatrix.cpp.
97 int dim2 = wi_.
dim2();
98 for (
int t = 0; t < wi_.
dim1(); ++t) {
99 double* f_line = wf_[t];
100 inT8* i_line = wi_[t];
101 double max_abs = 0.0;
102 for (
int f = 0; f < dim2; ++f) {
103 double abs_val = fabs(f_line[f]);
104 if (abs_val > max_abs) max_abs = abs_val;
108 if (scale == 0.0) scale = 1.0;
109 for (
int f = 0; f < dim2; ++f) {
116 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
void ResizeNoInit(int size1, int size2, int pad=0)
void Resize(int size1, int size2, const T &empty)
int IntCastRounded(double x)
void init_to_size(int size, T t)
static IntSimdMatrix * GetFastestMultiplier()
◆ CountAlternators()
void tesseract::WeightMatrix::CountAlternators |
( |
const WeightMatrix & |
other, |
|
|
double * |
same, |
|
|
double * |
changed |
|
) |
| const |
Definition at line 309 of file weightmatrix.cpp.
311 int num_outputs = updates_.
dim1();
312 int num_inputs = updates_.
dim2();
315 for (
int i = 0; i < num_outputs; ++i) {
316 const double* this_i = updates_[i];
317 const double* other_i = other.updates_[i];
318 for (
int j = 0; j < num_inputs; ++j) {
319 double product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 340 of file weightmatrix.cpp.
343 for (
int i = 0; i < wi_.
dim1(); ++i) {
344 for (
int j = 0; j < wi_.
dim2(); ++j) {
345 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
349 for (
int i = 0; i < wf_.
dim1(); ++i) {
350 for (
int j = 0; j < wf_.
dim2(); ++j) {
351 HistogramWeight(wf_[i][j], &histogram);
const int kHistogramBuckets
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 159 of file weightmatrix.cpp.
161 if (fp->FRead(&mode,
sizeof(mode), 1) != 1)
return false;
169 if (multiplier_ !=
nullptr) multiplier_->Init(wi_);
175 if (use_adam_ && !dw_sq_sum_.
DeSerialize(fp))
return false;
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
static IntSimdMatrix * GetFastestMultiplier()
bool DeSerializeOld(bool training, TFile *fp)
◆ DeSerializeOld()
bool tesseract::WeightMatrix::DeSerializeOld |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 183 of file weightmatrix.cpp.
190 for (
int i = 0; i < old_scales.
size(); ++i) scales_[i] = old_scales[i];
void resize_no_init(int size)
static void FloatToDouble(const GENERIC_2D_ARRAY< float > &wf, GENERIC_2D_ARRAY< double > *wd)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerialize(bool swap, FILE *fp)
◆ DotProduct()
double tesseract::WeightMatrix::DotProduct |
( |
const double * |
u, |
|
|
const double * |
v, |
|
|
int |
n |
|
) |
| |
|
static |
Definition at line 361 of file weightmatrix.cpp.
374 for (
int k = 0; k < n; ++k) total += u[k] * v[k];
static bool IsSSEAvailable()
double DotProductSSE(const double *u, const double *v, int n)
double DotProductAVX(const double *u, const double *v, int n)
static bool IsAVXAvailable()
◆ FloatToDouble()
Definition at line 381 of file weightmatrix.cpp.
383 int dim1 = wf.
dim1();
384 int dim2 = wf.
dim2();
386 for (
int i = 0; i < dim1; ++i) {
387 const float* wfi = wf[i];
388 double* wdi = (*wd)[i];
389 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 121 of file weightmatrix.cpp.
122 int no = int_mode_ ? wi_.
dim1() : wf_.
dim1();
123 int ni = int_mode_ ? wi_.
dim2() : wf_.
dim2();
125 updates_.
Resize(no, ni, 0.0);
127 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 45 of file weightmatrix.cpp.
49 if (randomizer != NULL) {
50 for (
int i = 0; i < no; ++i) {
51 for (
int j = 0; j < ni; ++j) {
52 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 210 of file weightmatrix.cpp.
212 MatrixDotVectorInternal(wf_,
true,
false, u, v);
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const inT8 * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 215 of file weightmatrix.cpp.
218 multiplier_->MatrixDotVector(wi_, scales_, u, v);
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const double * |
v, |
|
|
double * |
inout |
|
) |
| |
Definition at line 223 of file weightmatrix.cpp.
227 const double* u = wf_[0];
228 for (
int i = 0; i < n; ++i) {
229 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 66 of file weightmatrix.cpp.
68 int old_no = wf_.
dim1();
69 int new_no = code_map.size();
71 std::vector<double> means(ni, 0.0);
72 for (
int c = 0; c < old_no; ++c) {
73 const double* weights = wf_[c];
74 for (
int i = 0; i < ni; ++i) means[i] += weights[i];
76 for (
double& mean : means) mean /= old_no;
79 for (
int dest = 0; dest < new_no; ++dest) {
80 int src = code_map[dest];
81 const double* src_data = src >= 0 ? old_wf[src] : means.data();
82 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 140 of file weightmatrix.cpp.
145 if (fp->FWrite(&mode,
sizeof(mode), 1) != 1)
return false;
148 if (!scales_.
Serialize(fp))
return false;
151 if (training && !updates_.
Serialize(fp))
return false;
152 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 247 of file weightmatrix.cpp.
251 int num_outputs = dw_.
dim1();
254 int num_inputs = dw_.
dim2() - 1;
255 int num_samples = u.dim2();
259 #pragma omp parallel for num_threads(4) if (in_parallel) 261 for (
int i = 0; i < num_outputs; ++i) {
262 double* dwi = dw_[i];
263 const double* ui = u[i];
264 for (
int j = 0; j < num_inputs; ++j) {
269 for (
int k = 0; k < num_samples; ++k) total += ui[k];
270 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 277 of file weightmatrix.cpp.
281 learning_rate *= sqrt(1.0 - pow(adam_beta, num_samples));
282 learning_rate /= 1.0 - pow(momentum, num_samples);
284 if (use_adam_ && num_samples > 0 && momentum > 0.0) {
286 dw_ *= learning_rate * (1.0 - momentum);
287 updates_ *= momentum;
291 dw_ *= learning_rate;
293 if (momentum > 0.0) wf_ += updates_;
294 if (momentum >= 0.0) updates_ *= momentum;
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)
void Transpose(const GENERIC_2D_ARRAY< double > &input)
const double kAdamEpsilon
const int kAdamCorrectionIterations
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 237 of file weightmatrix.cpp.
239 MatrixDotVectorInternal(wf_t_,
false,
true, u, v);
The documentation for this class was generated from the following files: