#include <weightmatrix.h>
|
| WeightMatrix () |
|
int | InitWeightsFloat (int no, int ni, bool ada_grad, float weight_range, TRand *randomizer) |
|
void | ConvertToInt () |
|
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, 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 63 of file weightmatrix.h.
◆ WeightMatrix()
tesseract::WeightMatrix::WeightMatrix |
( |
| ) |
|
|
inline |
Definition at line 65 of file weightmatrix.h.
65 : int_mode_(
false), use_ada_grad_(
false) {}
◆ AddDeltas()
void tesseract::WeightMatrix::AddDeltas |
( |
const WeightMatrix & |
other | ) |
|
◆ ConvertToInt()
void tesseract::WeightMatrix::ConvertToInt |
( |
| ) |
|
Definition at line 62 of file weightmatrix.cpp.
65 int dim2 = wi_.
dim2();
66 for (
int t = 0; t < wi_.
dim1(); ++t) {
67 double* f_line = wf_[t];
68 inT8* i_line = wi_[t];
70 for (
int f = 0; f < dim2; ++f) {
71 double abs_val = fabs(f_line[f]);
72 if (abs_val > max_abs) max_abs = abs_val;
76 if (scale == 0.0) scale = 1.0;
77 for (
int f = 0; f < dim2; ++f) {
void init_to_size(int size, T t)
void Resize(int size1, int size2, const T &empty)
int IntCastRounded(double x)
void ResizeNoInit(int size1, int size2)
◆ CountAlternators()
void tesseract::WeightMatrix::CountAlternators |
( |
const WeightMatrix & |
other, |
|
|
double * |
same, |
|
|
double * |
changed |
|
) |
| const |
Definition at line 276 of file weightmatrix.cpp.
278 int num_outputs = updates_.
dim1();
279 int num_inputs = updates_.
dim2();
282 for (
int i = 0; i < num_outputs; ++i) {
283 const double* this_i = updates_[i];
284 const double* other_i = other.updates_[i];
285 for (
int j = 0; j < num_inputs; ++j) {
286 double product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 307 of file weightmatrix.cpp.
310 for (
int i = 0; i < wi_.
dim1(); ++i) {
311 for (
int j = 0; j < wi_.
dim2(); ++j) {
312 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
316 for (
int i = 0; i < wf_.
dim1(); ++i) {
317 for (
int j = 0; j < wf_.
dim2(); ++j) {
318 HistogramWeight(wf_[i][j], &histogram);
const int kHistogramBuckets
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 125 of file weightmatrix.cpp.
127 if (fp->FRead(&mode,
sizeof(mode), 1) != 1)
return false;
139 if (use_ada_grad_ && !dw_sq_sum_.
DeSerialize(fp))
return false;
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 147 of file weightmatrix.cpp.
154 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 328 of file weightmatrix.cpp.
341 for (
int k = 0; k < n; ++k) total +=
u[k] *
v[k];
double DotProductSSE(const double *u, const double *v, int n)
static bool IsAVXAvailable()
double DotProductAVX(const double *u, const double *v, int n)
static bool IsSSEAvailable()
◆ FloatToDouble()
Definition at line 348 of file weightmatrix.cpp.
350 int dim1 = wf.
dim1();
351 int dim2 = wf.
dim2();
353 for (
int i = 0; i < dim1; ++i) {
354 const float* wfi = wf[i];
355 double* wdi = (*wd)[i];
356 for (
int j = 0; j < dim2; ++j) wdi[j] = static_cast<double>(wfi[j]);
void ResizeNoInit(int size1, int size2)
◆ 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 87 of file weightmatrix.cpp.
88 int no = int_mode_ ? wi_.
dim1() : wf_.
dim1();
89 int ni = int_mode_ ? wi_.
dim2() : wf_.
dim2();
91 updates_.
Resize(no, ni, 0.0);
93 if (use_ada_grad_) 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 |
ada_grad, |
|
|
float |
weight_range, |
|
|
TRand * |
randomizer |
|
) |
| |
Definition at line 39 of file weightmatrix.cpp.
43 if (randomizer != NULL) {
44 for (
int i = 0; i < no; ++i) {
45 for (
int j = 0; j < ni; ++j) {
46 wf_[i][j] = randomizer->SignedRand(weight_range);
50 use_ada_grad_ = ada_grad;
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 |
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const inT8 * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 179 of file weightmatrix.cpp.
181 int num_out = wi_.
dim1();
182 int num_in = wi_.
dim2() - 1;
183 for (
int i = 0; i < num_out; ++i) {
184 const inT8* Wi = wi_[i];
189 for (
int j = 0; j < num_in; ++j) total += Wi[j] *
u[j];
192 v[i] = (
static_cast<double>(total) /
MAX_INT8 + Wi[num_in]) * scales_[i];
int32_t IntDotProductSSE(const int8_t *u, const int8_t *v, int n)
static bool IsSSEAvailable()
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const double * |
v, |
|
|
double * |
inout |
|
) |
| |
Definition at line 198 of file weightmatrix.cpp.
202 const double*
u = wf_[0];
203 for (
int i = 0; i < n; ++i) {
204 inout[i] += u[i] *
v[i];
◆ NumOutputs()
int tesseract::WeightMatrix::NumOutputs |
( |
| ) |
const |
|
inline |
◆ Serialize()
bool tesseract::WeightMatrix::Serialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| const |
Definition at line 106 of file weightmatrix.cpp.
111 if (fp->FWrite(&mode,
sizeof(mode), 1) != 1)
return false;
114 if (!scales_.
Serialize(fp))
return false;
117 if (training && !updates_.
Serialize(fp))
return false;
118 if (training && use_ada_grad_ && !dw_sq_sum_.
Serialize(fp))
return false;
bool Serialize(FILE *fp) const
bool Serialize(FILE *fp) const
◆ SumOuterTransposed()
Definition at line 222 of file weightmatrix.cpp.
226 int num_outputs = dw_.
dim1();
229 int num_inputs = dw_.
dim2() - 1;
230 int num_samples =
u.dim2();
234 #pragma omp parallel for num_threads(4) if (in_parallel) 236 for (
int i = 0; i < num_outputs; ++i) {
237 double* dwi = dw_[i];
238 const double* ui =
u[i];
239 for (
int j = 0; j < num_inputs; ++j) {
244 for (
int k = 0; k < num_samples; ++k) total += ui[k];
245 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, |
|
|
int |
num_samples |
|
) |
| |
Definition at line 252 of file weightmatrix.cpp.
255 if (use_ada_grad_ && num_samples > 0) {
259 dw_ *= learning_rate;
261 if (momentum > 0.0) wf_ += updates_;
262 if (momentum >= 0.0) updates_ *= momentum;
void AdaGradScaling(const GENERIC_2D_ARRAY< T > &sqsum, int num_samples)
void SumSquares(const GENERIC_2D_ARRAY< T > &src)
void Transpose(const GENERIC_2D_ARRAY< double > &input)
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
The documentation for this class was generated from the following files: