#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 TFloat * | GetWeights (int index) const |
|
TFloat | 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 TFloat *u, TFloat *v) const |
|
void | MatrixDotVector (const int8_t *u, TFloat *v) const |
|
void | MultiplyAccumulate (const TFloat *v, TFloat *inout) |
|
void | VectorDotMatrix (const TFloat *u, TFloat *v) const |
|
void | SumOuterTransposed (const TransposedArray &u, const TransposedArray &v, bool parallel) |
|
void | Update (float learning_rate, float momentum, float adam_beta, int num_samples) |
|
void | AddDeltas (const WeightMatrix &other) |
|
void | CountAlternators (const WeightMatrix &other, TFloat *same, TFloat *changed) const |
|
void | Debug2D (const char *msg) |
|
Definition at line 70 of file weightmatrix.h.
◆ WeightMatrix()
tesseract::WeightMatrix::WeightMatrix |
( |
| ) |
|
|
inline |
Definition at line 72 of file weightmatrix.h.
72 : int_mode_(
false), use_adam_(
false) {}
◆ AddDeltas()
void tesseract::WeightMatrix::AddDeltas |
( |
const WeightMatrix & |
other | ) |
|
Definition at line 486 of file weightmatrix.cpp.
487 assert(dw_.
dim1() == other.dw_.dim1());
488 assert(dw_.
dim2() == other.dw_.dim2());
◆ ConvertToInt()
void tesseract::WeightMatrix::ConvertToInt |
( |
| ) |
|
Definition at line 183 of file weightmatrix.cpp.
185 scales_.reserve(wi_.
dim1());
186 int dim2 = wi_.
dim2();
187 for (
int t = 0; t < wi_.
dim1(); ++t) {
189 int8_t *i_line = wi_[t];
191 for (
int f = 0; f < dim2; ++f) {
192 TFloat abs_val = fabs(f_line[f]);
193 if (abs_val > max_abs) {
197 TFloat scale = max_abs / INT8_MAX;
198 scales_.push_back(scale / INT8_MAX);
202 for (
int f = 0; f < dim2; ++f) {
209 int32_t rounded_num_out;
211 scales_.resize(rounded_num_out);
int IntCastRounded(double x)
void Resize(int size1, int size2, const T &empty)
void ResizeNoInit(int size1, int size2, int pad=0)
static const IntSimdMatrix * intSimdMatrix
void Init(const GENERIC_2D_ARRAY< int8_t > &w, std::vector< int8_t > &shaped_w, int32_t &rounded_num_out) const
◆ CountAlternators()
Definition at line 495 of file weightmatrix.cpp.
497 int num_outputs = updates_.
dim1();
498 int num_inputs = updates_.
dim2();
499 assert(num_outputs == other.updates_.dim1());
500 assert(num_inputs == other.updates_.dim2());
501 for (
int i = 0; i < num_outputs; ++i) {
502 const TFloat *this_i = updates_[i];
503 const TFloat *other_i = other.updates_[i];
504 for (
int j = 0; j < num_inputs; ++j) {
505 TFloat product = this_i[j] * other_i[j];
◆ Debug2D()
void tesseract::WeightMatrix::Debug2D |
( |
const char * |
msg | ) |
|
Definition at line 527 of file weightmatrix.cpp.
530 for (
int i = 0; i < wi_.
dim1(); ++i) {
531 for (
int j = 0; j < wi_.
dim2(); ++j) {
532 HistogramWeight(wi_[i][j] * scales_[i], &histogram);
536 for (
int i = 0; i < wf_.
dim1(); ++i) {
537 for (
int j = 0; j < wf_.
dim2(); ++j) {
538 HistogramWeight(wf_[i][j], &histogram);
void tprintf(const char *format,...)
const int kHistogramBuckets
◆ DeSerialize()
bool tesseract::WeightMatrix::DeSerialize |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 280 of file weightmatrix.cpp.
282 if (!fp->DeSerialize(&mode)) {
295 if (!fp->DeSerialize(&size)) {
299 scales_.reserve(size);
300 for (
auto n = size; n > 0; n--) {
302 if (!fp->DeSerialize(&val)) {
305 scales_.push_back(val / INT8_MAX);
308 scales_.resize(size);
309 if (!fp->DeSerialize(&scales_[0], size)) {
312 for (
auto &scale : scales_) {
317 int32_t rounded_num_out;
319 scales_.resize(rounded_num_out);
bool DeSerialize(bool swap, FILE *fp, std::vector< T > &data)
bool DeSerialize(bool swap, FILE *fp)
bool DeSerializeOld(bool training, TFile *fp)
◆ DeSerializeOld()
bool tesseract::WeightMatrix::DeSerializeOld |
( |
bool |
training, |
|
|
TFile * |
fp |
|
) |
| |
Definition at line 342 of file weightmatrix.cpp.
352 std::vector<float> old_scales;
353 if (!fp->DeSerialize(old_scales)) {
356 scales_.reserve(old_scales.size());
357 for (
float old_scale : old_scales) {
358 scales_.push_back(old_scale);
361 GENERIC_2D_ARRAY<float> float_array;
362 if (!float_array.DeSerialize(fp)) {
365 FloatToDouble(float_array, wf_);
369 GENERIC_2D_ARRAY<float> float_array;
370 if (!float_array.DeSerialize(fp)) {
373 FloatToDouble(float_array, updates_);
375 if (!float_array.DeSerialize(fp)) {
◆ GetDW()
TFloat tesseract::WeightMatrix::GetDW |
( |
int |
i, |
|
|
int |
j |
|
) |
| const |
|
inline |
◆ GetWeights()
const TFloat* tesseract::WeightMatrix::GetWeights |
( |
int |
index | ) |
const |
|
inline |
◆ InitBackward()
void tesseract::WeightMatrix::InitBackward |
( |
| ) |
|
Definition at line 217 of file weightmatrix.cpp.
218 int no = int_mode_ ? wi_.
dim1() : wf_.
dim1();
219 int ni = int_mode_ ? wi_.
dim2() : wf_.
dim2();
221 updates_.
Resize(no, ni, 0.0);
224 dw_sq_sum_.
Resize(no, ni, 0.0);
void Transpose(const GENERIC_2D_ARRAY< TFloat > &input)
◆ InitWeightsFloat()
int tesseract::WeightMatrix::InitWeightsFloat |
( |
int |
no, |
|
|
int |
ni, |
|
|
bool |
use_adam, |
|
|
float |
weight_range, |
|
|
TRand * |
randomizer |
|
) |
| |
Definition at line 130 of file weightmatrix.cpp.
134 if (randomizer !=
nullptr) {
135 for (
int i = 0; i < no; ++i) {
136 for (
int j = 0; j < ni; ++j) {
137 wf_[i][j] = randomizer->SignedRand(weight_range);
141 use_adam_ = use_adam;
◆ is_int_mode()
bool tesseract::WeightMatrix::is_int_mode |
( |
| ) |
const |
|
inline |
◆ MatrixDotVector() [1/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const int8_t * |
u, |
|
|
TFloat * |
v |
|
) |
| const |
Definition at line 393 of file weightmatrix.cpp.
static void MatrixDotVector(const GENERIC_2D_ARRAY< int8_t > &w, const std::vector< TFloat > &scales, const int8_t *u, TFloat *v)
MatrixDotVectorFunction matrixDotVectorFunction
◆ MatrixDotVector() [2/2]
void tesseract::WeightMatrix::MatrixDotVector |
( |
const TFloat * |
u, |
|
|
TFloat * |
v |
|
) |
| const |
Definition at line 388 of file weightmatrix.cpp.
390 MatrixDotVectorInternal(wf_,
true,
false, u, v);
◆ MultiplyAccumulate()
void tesseract::WeightMatrix::MultiplyAccumulate |
( |
const TFloat * |
v, |
|
|
TFloat * |
inout |
|
) |
| |
Definition at line 405 of file weightmatrix.cpp.
407 assert(wf_.
dim1() == 1);
410 for (
int i = 0; i < n; ++i) {
411 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 151 of file weightmatrix.cpp.
152 GENERIC_2D_ARRAY<TFloat> old_wf(wf_);
153 int old_no = wf_.
dim1();
154 int new_no = code_map.size();
156 std::vector<TFloat> means(ni, 0.0);
157 for (
int c = 0; c < old_no; ++c) {
158 const TFloat *weights = wf_[c];
159 for (
int i = 0; i < ni; ++i) {
160 means[i] += weights[i];
163 for (
auto &mean : means) {
166 wf_.
Resize(new_no, ni, 0.0);
168 for (
int dest = 0; dest < new_no; ++dest) {
169 int src = code_map[dest];
170 const TFloat *src_data = src >= 0 ? old_wf[src] : means.data();
171 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 238 of file weightmatrix.cpp.
242 if (!fp->Serialize(&mode)) {
249 uint32_t size = scales_.size();
250 if (!fp->Serialize(&size)) {
253 for (
auto scale : scales_) {
257 double value = scale * INT8_MAX;
258 if (!fp->Serialize(&value)) {
bool Serialize(FILE *fp, const std::vector< T > &data)
bool Serialize(FILE *fp) const
◆ SumOuterTransposed()
Definition at line 429 of file weightmatrix.cpp.
432 int num_outputs = dw_.
dim1();
433 assert(u.dim1() == num_outputs);
434 assert(u.dim2() == v.dim2());
435 int num_inputs = dw_.
dim2() - 1;
436 int num_samples = u.dim2();
438 assert(v.dim1() == num_inputs);
440 # pragma omp parallel for num_threads(4) if (in_parallel)
442 for (
int i = 0; i < num_outputs; ++i) {
445 for (
int j = 0; j < num_inputs; ++j) {
450 for (
int k = 0; k < num_samples; ++k) {
453 dwi[num_inputs] = total;
DotProductFunction DotProduct
◆ Update()
void tesseract::WeightMatrix::Update |
( |
float |
learning_rate, |
|
|
float |
momentum, |
|
|
float |
adam_beta, |
|
|
int |
num_samples |
|
) |
| |
Definition at line 460 of file weightmatrix.cpp.
463 learning_rate *= sqrt(1.0f - pow(adam_beta, num_samples));
464 learning_rate /= 1.0f - pow(momentum, num_samples);
466 if (use_adam_ && num_samples > 0 && momentum > 0.0f) {
468 dw_ *= learning_rate * (1.0f - momentum);
469 updates_ *= momentum;
473 dw_ *= learning_rate;
475 if (momentum > 0.0f) {
478 if (momentum >= 0.0f) {
479 updates_ *= momentum;
const TFloat kAdamEpsilon
const int kAdamCorrectionIterations
void AdamUpdate(const GENERIC_2D_ARRAY< T > &sum, const GENERIC_2D_ARRAY< T > &sqsum, const T &epsilon)
void SumSquares(const GENERIC_2D_ARRAY< T > &src, const T &decay_factor)
◆ VectorDotMatrix()
void tesseract::WeightMatrix::VectorDotMatrix |
( |
const TFloat * |
u, |
|
|
TFloat * |
v |
|
) |
| const |
Definition at line 419 of file weightmatrix.cpp.
421 MatrixDotVectorInternal(wf_t_,
false,
true, u, v);
The documentation for this class was generated from the following files: