Started on regulaization in Loss.h. I need to refactor the matsum.h since I need a total sum over the matrix. Also matmul needs a elementwise matmul function, which is the next this in the ragulaization
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include "./numerics/abs.h"
|
||||
#include "./utils/matrix.h"
|
||||
|
||||
namespace numerics{
|
||||
|
||||
template <typename T>
|
||||
void inplace_matabs(utils::Matrix<T>& A){
|
||||
|
||||
for (uint64_t i = 0; i < A.rows(); ++i){
|
||||
for (uint64_t j = 0; j < A.cols(); ++j){
|
||||
A(i,j) = numerics::abs(A(i,j));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
utils::Matrix<T> matabs(const utils::Matrix<T>& A){
|
||||
utils::Matrix<T> B = A;
|
||||
inplace_matabs(B);
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
+10
-13
@@ -1,5 +1,4 @@
|
||||
#ifndef _mean_n_
|
||||
#define _mean_n_
|
||||
#pragma once
|
||||
|
||||
#include "./utils/vector.h"
|
||||
#include "./utils/matrix.h"
|
||||
@@ -8,7 +7,7 @@
|
||||
namespace numerics{
|
||||
|
||||
template <typename T>
|
||||
T matmean(utils::Matrix<T>& A) {
|
||||
T matmean(const utils::Matrix<T>& A) {
|
||||
|
||||
T mean(T{0});
|
||||
|
||||
@@ -27,7 +26,7 @@ namespace numerics{
|
||||
|
||||
|
||||
template <typename T>
|
||||
void inplace_matmean_row(utils::Matrix<T>& A, utils::Vector<T>& b) {
|
||||
void inplace_matmean_row(const utils::Matrix<T>& A, utils::Vector<T>& b) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
@@ -40,12 +39,12 @@ namespace numerics{
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
b[j] += A(i, j);
|
||||
}
|
||||
b[j] =/ static_cast<T>(rows);
|
||||
b[j] /= static_cast<T>(rows);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void inplace_matmean_cols(utils::Matrix<T>& A) {
|
||||
void inplace_matmean_cols(const utils::Matrix<T>& A, utils::Vector<T>& b) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
@@ -58,15 +57,15 @@ namespace numerics{
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
b[i] += A(i, j);
|
||||
}
|
||||
b[j] =/ static_cast<T>(cols);
|
||||
b[i] /= static_cast<T>(cols);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> matmean_row(utils::Matrix<T>& A) {
|
||||
utils::Vector<T> matmean_row(const utils::Matrix<T>& A) {
|
||||
|
||||
utils:Vector<T> b(A.rows(), T{0});
|
||||
utils::Vector<T> b(A.rows(), T{0});
|
||||
|
||||
inplace_matmean_row(A, b);
|
||||
|
||||
@@ -74,9 +73,9 @@ namespace numerics{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> matmean_col(utils::Matrix<T>& A) {
|
||||
utils::Vector<T> matmean_col(const utils::Matrix<T>& A) {
|
||||
|
||||
utils:Vector<T> b(A.cols(), T{0});
|
||||
utils::Vector<T> b(A.cols(), T{0});
|
||||
|
||||
inplace_matmean_cols(A, b);
|
||||
|
||||
@@ -84,5 +83,3 @@ namespace numerics{
|
||||
}
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
#endif // _mean_n_
|
||||
@@ -7,6 +7,20 @@
|
||||
|
||||
namespace numerics{
|
||||
|
||||
template <typename T>
|
||||
T matsum_coeff(const utils::Matrix<T>& A) {
|
||||
|
||||
T b;
|
||||
|
||||
for (uint64_t i = 0; i < A.cols(); ++i){
|
||||
for (uint64_t j = 0; j < A.rows(); ++j){
|
||||
b += A(i, j);
|
||||
}
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
utils::Vector<T> matsum(const utils::Matrix<T>& A, std::string method) {
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
#include "./numerics/matmul.h"
|
||||
#include "./numerics/matscalar.h"
|
||||
#include "./numerics/matmax.h"
|
||||
#include "./numerics/matabs.h"
|
||||
#include "./numerics/matdiv.h"
|
||||
#include "./numerics/matvec.h"
|
||||
#include "./numerics/matadd.h"
|
||||
|
||||
Reference in New Issue
Block a user