#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 | ) |
|
Definition at line 337 of file weightmatrix.cpp.
338 assert(dw_.
dim1() == other.dw_.dim1());
339 assert(dw_.
dim2() == other.dw_.dim2());
◆ ConvertToInt()
void tesseract::WeightMatrix::ConvertToInt |
( |
| ) |
|
Definition at line 125 of file weightmatrix.cpp.
128 int dim2 = wi_.
dim2();
129 for (
int t = 0; t < wi_.
dim1(); ++t) {
130 double* f_line = wf_[t];
131 int8_t* i_line = wi_[t];
132 double max_abs = 0.0;
133 for (
int f = 0; f < dim2; ++f) {
134 double abs_val = fabs(f_line[f]);
135 if (abs_val > max_abs) max_abs = abs_val;
137 double scale = max_abs / INT8_MAX;
139 if (scale == 0.0) scale = 1.0;
140 for (
int f = 0; f < dim2; ++f) {
◆ CountAlternators()
void tesseract::WeightMatrix::CountAlternators |
( |
const WeightMatrix & |
other, |
|
|
double * |
same, |
|
|
double * |
changed |
|
) |
| const |
Definition at line 346 of file weightmatrix.cpp.
348 int num_outputs = updates_.
dim1();
349 int num_inputs = updates_.
dim2();
350 assert(num_outputs == other.updates_.dim1());
351 assert(num_inputs == other.updates_.dim2());
352 for (
int i = 0; i < num_outputs; ++i) {
353 const double* this_i = updates_[i];
354 const double* other_i = other.updates_[i];
355 for (
int j = 0; j < num_inputs; ++j) {
356 double product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 377 of file weightmatrix.cpp.
380 for (
int i = 0; i < wi_.
dim1(); ++i) {
381 for (
int j = 0; j < wi_.
dim2(); ++j) {
382 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
386 for (
int i = 0; i < wf_.
dim1(); ++i) {
387 for (
int j = 0; j < wf_.
dim2(); ++j) {
388 HistogramWeight(wf_[i][j], &histogram);
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 191 of file weightmatrix.cpp.
193 if (!fp->DeSerialize(&mode))
return false;
208 if (use_adam_ && !dw_sq_sum_.
DeSerialize(fp))
return false;
◆ DeSerializeOld()
bool tesseract::WeightMatrix::DeSerializeOld |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 216 of file weightmatrix.cpp.
223 for (
int i = 0; i < old_scales.
size(); ++i) scales_[i] = old_scales[i];
◆ FloatToDouble()
Definition at line 399 of file weightmatrix.cpp.
401 int dim1 = wf.
dim1();
402 int dim2 = wf.
dim2();
404 for (
int i = 0; i < dim1; ++i) {
405 const float* wfi = wf[i];
406 double* wdi = (*wd)[i];
407 for (
int j = 0; j < dim2; ++j) wdi[j] = static_cast<double>(wfi[j]);
◆ 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 |
( |
| ) |
|
◆ InitWeightsFloat()
int tesseract::WeightMatrix::InitWeightsFloat |
( |
int |
no, |
|
|
int |
ni, |
|
|
bool |
use_adam, |
|
|
float |
weight_range, |
|
|
TRand * |
randomizer |
|
) |
| |
Definition at line 76 of file weightmatrix.cpp.
80 if (randomizer !=
nullptr) {
81 for (
int i = 0; i < no; ++i) {
82 for (
int j = 0; j < ni; ++j) {
83 wf_[i][j] = randomizer->SignedRand(weight_range);
◆ 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 243 of file weightmatrix.cpp.
245 MatrixDotVectorInternal(wf_,
true,
false, u, v);
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const int8_t * |
u, |
|
|
double * |
v |
|
) |
| const |
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const double * |
v, |
|
|
double * |
inout |
|
) |
| |
Definition at line 260 of file weightmatrix.cpp.
262 assert(wf_.
dim1() == 1);
264 const double* u = wf_[0];
265 for (
int i = 0; i < n; ++i) {
266 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 97 of file weightmatrix.cpp.
99 int old_no = wf_.
dim1();
100 int new_no = code_map.size();
102 std::vector<double> means(ni, 0.0);
103 for (
int c = 0; c < old_no; ++c) {
104 const double* weights = wf_[c];
105 for (
int i = 0; i < ni; ++i) means[i] += weights[i];
107 for (
double& mean : means) mean /= old_no;
111 int src = code_map[
dest];
112 const double* src_data = src >= 0 ? old_wf[src] : means.data();
113 memcpy(wf_[
dest], src_data, ni *
sizeof(*src_data));
◆ RoundInputs()
int tesseract::WeightMatrix::RoundInputs |
( |
int |
size | ) |
const |
|
inline |
◆ Serialize()
bool tesseract::WeightMatrix::Serialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| const |
Definition at line 172 of file weightmatrix.cpp.
177 if (!fp->Serialize(&mode))
return false;
180 if (!scales_.
Serialize(fp))
return false;
183 if (training && !updates_.
Serialize(fp))
return false;
184 if (training && use_adam_ && !dw_sq_sum_.
Serialize(fp))
return false;
◆ SumOuterTransposed()
Definition at line 284 of file weightmatrix.cpp.
288 int num_outputs = dw_.
dim1();
289 assert(u.dim1() == num_outputs);
290 assert(u.dim2() == v.dim2());
291 int num_inputs = dw_.
dim2() - 1;
292 int num_samples = u.dim2();
294 assert(v.dim1() == num_inputs);
296 #pragma omp parallel for num_threads(4) if (in_parallel)
298 for (
int i = 0; i < num_outputs; ++i) {
299 double* dwi = dw_[i];
300 const double* ui = u[i];
301 for (
int j = 0; j < num_inputs; ++j) {
306 for (
int k = 0; k < num_samples; ++k) total += ui[k];
307 dwi[num_inputs] = total;
◆ Update()
void tesseract::WeightMatrix::Update |
( |
double |
learning_rate, |
|
|
double |
momentum, |
|
|
double |
adam_beta, |
|
|
int |
num_samples |
|
) |
| |
Definition at line 314 of file weightmatrix.cpp.
318 learning_rate *= sqrt(1.0 - pow(adam_beta, num_samples));
319 learning_rate /= 1.0 - pow(momentum, num_samples);
321 if (use_adam_ && num_samples > 0 && momentum > 0.0) {
323 dw_ *= learning_rate * (1.0 - momentum);
324 updates_ *= momentum;
328 dw_ *= learning_rate;
330 if (momentum > 0.0) wf_ += updates_;
331 if (momentum >= 0.0) updates_ *= momentum;
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const double * |
u, |
|
|
double * |
v |
|
) |
| const |
Definition at line 274 of file weightmatrix.cpp.
276 MatrixDotVectorInternal(wf_t_,
false,
true, u, v);
The documentation for this class was generated from the following files: