forked from facebookresearch/fastText
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cc
133 lines (115 loc) · 2.83 KB
/
matrix.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Copyright (c) 2016-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include "matrix.h"
#include <exception>
#include <random>
#include <stdexcept>
#include "utils.h"
#include "vector.h"
namespace fasttext {
Matrix::Matrix() : Matrix(0, 0) {}
Matrix::Matrix(int64_t m, int64_t n) : data_(m * n), m_(m), n_(n) {}
void Matrix::zero() {
std::fill(data_.begin(), data_.end(), 0.0);
}
void Matrix::uniform(real a) {
std::minstd_rand rng(1);
std::uniform_real_distribution<> uniform(-a, a);
for (int64_t i = 0; i < (m_ * n_); i++) {
data_[i] = uniform(rng);
}
}
real Matrix::dotRow(const Vector& vec, int64_t i) const {
assert(i >= 0);
assert(i < m_);
assert(vec.size() == n_);
real d = 0.0;
for (int64_t j = 0; j < n_; j++) {
d += at(i, j) * vec[j];
}
if (std::isnan(d)) {
throw std::runtime_error("Encountered NaN.");
}
return d;
}
void Matrix::addRow(const Vector& vec, int64_t i, real a) {
assert(i >= 0);
assert(i < m_);
assert(vec.size() == n_);
for (int64_t j = 0; j < n_; j++) {
data_[i * n_ + j] += a * vec[j];
}
}
void Matrix::multiplyRow(const Vector& nums, int64_t ib, int64_t ie) {
if (ie == -1) {
ie = m_;
}
assert(ie <= nums.size());
for (auto i = ib; i < ie; i++) {
real n = nums[i - ib];
if (n != 0) {
for (auto j = 0; j < n_; j++) {
at(i, j) *= n;
}
}
}
}
void Matrix::divideRow(const Vector& denoms, int64_t ib, int64_t ie) {
if (ie == -1) {
ie = m_;
}
assert(ie <= denoms.size());
for (auto i = ib; i < ie; i++) {
real n = denoms[i - ib];
if (n != 0) {
for (auto j = 0; j < n_; j++) {
at(i, j) /= n;
}
}
}
}
real Matrix::l2NormRow(int64_t i) const {
auto norm = 0.0;
for (auto j = 0; j < n_; j++) {
norm += at(i, j) * at(i, j);
}
if (std::isnan(norm)) {
throw std::runtime_error("Encountered NaN.");
}
return std::sqrt(norm);
}
void Matrix::l2NormRow(Vector& norms) const {
assert(norms.size() == m_);
for (auto i = 0; i < m_; i++) {
norms[i] = l2NormRow(i);
}
}
void Matrix::save(std::ostream& out) {
out.write((char*)&m_, sizeof(int64_t));
out.write((char*)&n_, sizeof(int64_t));
out.write((char*)data_.data(), m_ * n_ * sizeof(real));
}
void Matrix::load(std::istream& in) {
in.read((char*)&m_, sizeof(int64_t));
in.read((char*)&n_, sizeof(int64_t));
data_ = std::vector<real>(m_ * n_);
in.read((char*)data_.data(), m_ * n_ * sizeof(real));
}
void Matrix::dump(std::ostream& out) const {
out << m_ << " " << n_ << std::endl;
for (int64_t i = 0; i < m_; i++) {
for (int64_t j = 0; j < n_; j++) {
if (j > 0) {
out << " ";
}
out << at(i, j);
}
out << std::endl;
}
};
} // namespace fasttext