Neural Network
Started on implementing neural network from NNFS. I've done ReLU and stopped at p.104. Softmax is not ready.
This commit is contained in:
@@ -0,0 +1,229 @@
|
||||
#ifndef _matadd_n_
|
||||
#define _matadd_n_
|
||||
|
||||
|
||||
#include "./utils/matrix.h"
|
||||
#include "./core/omp_config.h"
|
||||
|
||||
namespace numerics{
|
||||
|
||||
// =================================================
|
||||
// y = A * x (Matrix–Vector product)
|
||||
// =================================================
|
||||
template <typename T>
|
||||
void inplace_matadd_colvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
if (rows != x.size()) {
|
||||
throw std::runtime_error("inplace_matadd_colvec: dimension mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < cols; ++i) {
|
||||
for (uint64_t j = 0; j < rows; ++j) {
|
||||
A(j, i) += x[j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void inplace_matadd_rowvec(utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
if (cols != x.size()) {
|
||||
throw std::runtime_error("inplace_matadd_rowvec: dimension mismatch");
|
||||
}
|
||||
|
||||
for (uint64_t i = 0; i < cols; ++i) {
|
||||
for (uint64_t j = 0; j < rows; ++j) {
|
||||
A(j, i) += x[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Matrix<T> matadd_colvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
||||
|
||||
//const uint64_t rows = A.rows();
|
||||
//const uint64_t cols = A.cols();
|
||||
|
||||
utils::Matrix<T> B = A;
|
||||
|
||||
inplace_matadd_colvec(B, x);
|
||||
|
||||
return B;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Matrix<T> matadd_rowvec(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
||||
|
||||
//const uint64_t rows = A.rows();
|
||||
//const uint64_t cols = A.cols();
|
||||
|
||||
utils::Matrix<T> B = A;
|
||||
|
||||
inplace_matadd_rowvec(B, x);
|
||||
|
||||
return B;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Matrix<T> matadd(const utils::Matrix<T>& A, const utils::Vector<T>& x, std::string method = "auto"){
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
const uint64_t N = x.size();
|
||||
|
||||
if (method=="auto"){
|
||||
|
||||
if (rows==cols){
|
||||
throw std::runtime_error("matadd: too many options for dimensions");
|
||||
} else if (rows == N){
|
||||
return matadd_rowvec(A, x);
|
||||
} else if (cols == N){
|
||||
return matadd_colvec(A, x);
|
||||
}else{
|
||||
throw std::runtime_error("matadd: undefined fault - auto");
|
||||
}
|
||||
}else if(method=="row"){
|
||||
return matadd_rowvec(A, x);
|
||||
} else if (method=="col"){
|
||||
return matadd_colvec(A, x);
|
||||
}else{
|
||||
throw std::runtime_error("matadd: undefined fault - defined method");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
// -------------- Collapse(2) OpenMP ----------------
|
||||
template <typename T>
|
||||
utils::Vector<T> matvec_omp(const utils::Matrix<T>& A, const utils::Vector<T>& x) {
|
||||
if (A.cols() != x.size()) {
|
||||
throw std::runtime_error("matvec: dimension mismatch");
|
||||
}
|
||||
|
||||
const uint64_t m = A.rows();
|
||||
const uint64_t n = A.cols();
|
||||
|
||||
utils::Vector<T> y(m, T{0}); // <-- y has length m (rows)
|
||||
|
||||
|
||||
const T* xptr = x.data();
|
||||
const T* Aptr = A.data(); // row-major: A(i,j) == Aptr[i*n + j]
|
||||
|
||||
// Each row i is an independent dot product: y[i] = dot(A[i,*], x)
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (uint64_t i = 0; i < m; ++i) {
|
||||
const T* row = Aptr + i * n; // contiguous row i
|
||||
T acc = T{0};
|
||||
#pragma omp simd reduction(+:acc)
|
||||
for (uint64_t j = 0; j < n; ++j) {
|
||||
acc += row[j] * xptr[j];
|
||||
}
|
||||
y[i] = acc;
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
|
||||
// -------------- Auto OpenMP ----------------
|
||||
template <typename T>
|
||||
utils::Vector<T> matvec_auto(const utils::Matrix<T>& A,
|
||||
const utils::Vector<T>& x) {
|
||||
|
||||
|
||||
uint64_t work = A.rows() * A.cols();
|
||||
|
||||
bool can_parallel = omp_config::omp_parallel_allowed();
|
||||
#ifdef _OPENMP
|
||||
int threads = omp_get_max_threads();
|
||||
#else
|
||||
int threads = 1;
|
||||
#endif
|
||||
|
||||
if (can_parallel || work > static_cast<uint64_t>(threads) * 4ull) {
|
||||
return matvec_omp(A,x);
|
||||
}
|
||||
else{
|
||||
// Safe fallback
|
||||
return matvec(A,x);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// =================================================
|
||||
// y = x * A (Vector–Matrix product)
|
||||
// =================================================
|
||||
template <typename T>
|
||||
utils::Vector<T> vecmat(const utils::Vector<T>& x, const utils::Matrix<T>& A) {
|
||||
if (x.size() != A.rows()) {
|
||||
throw std::runtime_error("vecmat: dimension mismatch");
|
||||
}
|
||||
const uint64_t m = A.rows();
|
||||
const uint64_t n = A.cols();
|
||||
|
||||
utils::Vector<T> y(n, T{0});
|
||||
|
||||
for (uint64_t j = 0; j < n; ++j) {
|
||||
for (uint64_t i = 0; i < m; ++i) {
|
||||
y[j] += x[i] * A(i, j);
|
||||
}
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
// -------------- Collapse(2) OpenMP ----------------
|
||||
template <typename T>
|
||||
utils::Vector<T> vecmat_omp(const utils::Vector<T>& x, const utils::Matrix<T>& A) {
|
||||
if (x.size() != A.rows()) {
|
||||
throw std::runtime_error("vecmat: dimension mismatch");
|
||||
}
|
||||
const uint64_t m = A.rows();
|
||||
const uint64_t n = A.cols();
|
||||
|
||||
utils::Vector<T> y(n, T{0});
|
||||
#pragma omp parallel for schedule(static)
|
||||
for (uint64_t j = 0; j < n; ++j) {
|
||||
T acc = T{0};
|
||||
for (uint64_t i = 0; i < m; ++i) {
|
||||
acc += x[i] * A(i, j);
|
||||
}
|
||||
y[j] = acc;
|
||||
}
|
||||
return y;
|
||||
}
|
||||
|
||||
// -------------- Auto OpenMP ----------------
|
||||
template <typename T>
|
||||
utils::Vector<T> vecmat_auto(const utils::Vector<T>& x,
|
||||
const utils::Matrix<T>& A) {
|
||||
|
||||
uint64_t work = A.rows() * A.cols();
|
||||
|
||||
bool can_parallel = omp_config::omp_parallel_allowed();
|
||||
#ifdef _OPENMP
|
||||
int threads = omp_get_max_threads();
|
||||
#else
|
||||
int threads = 1;
|
||||
#endif
|
||||
|
||||
if (can_parallel || work > static_cast<uint64_t>(threads) * 4ull) {
|
||||
return vecmat_omp(x,A);
|
||||
}
|
||||
else{
|
||||
// Safe fallback
|
||||
return vecmat(x,A);
|
||||
}
|
||||
|
||||
}
|
||||
*/
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
#endif // _matadd_n_
|
||||
@@ -11,11 +11,11 @@ namespace numerics{
|
||||
// ---------------- Serial baseline ----------------
|
||||
template <typename T>
|
||||
utils::Matrix<T> matmul(const utils::Matrix<T>& A, const utils::Matrix<T>& B){
|
||||
|
||||
|
||||
if(A.cols() != B.rows()){
|
||||
throw std::runtime_error("matmul: dimension mismatch");
|
||||
}
|
||||
|
||||
|
||||
const uint64_t m = A.rows();
|
||||
const uint64_t n = A.cols(); // also B.rows()
|
||||
const uint64_t p = B.cols();
|
||||
@@ -98,6 +98,7 @@ utils::Matrix<T> matmul_auto(const utils::Matrix<T>& A,
|
||||
|
||||
// Tiny problems: serial is cheapest.
|
||||
if (!can_parallel || work < threads*4ull) {
|
||||
|
||||
return matmul(A,B);
|
||||
}
|
||||
// Plenty of (i,j) work → collapse(2) is a great default.
|
||||
|
||||
@@ -17,6 +17,35 @@ namespace numerics{
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void inplace_max(utils::Matrix<T>& A, const T b){
|
||||
|
||||
const uint64_t rows = A.rows();
|
||||
const uint64_t cols = A.cols();
|
||||
|
||||
for (uint64_t i = 0; i < rows; ++i){
|
||||
for (uint64_t j = 0; j < cols; ++j){
|
||||
|
||||
if (b > A(i,j)){
|
||||
//std::cout << A(i,j) << std::endl;
|
||||
A(i,j) = b;
|
||||
//std::cout << A(i,j) << std::endl;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
utils::Matrix<T> max(const utils::Matrix<T>& A, const T b){
|
||||
|
||||
utils::Matrix<T> B = A;
|
||||
|
||||
inplace_max(B, b);
|
||||
|
||||
|
||||
return B;
|
||||
}
|
||||
|
||||
|
||||
|
||||
} // namespace numerics
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "./numerics/inverse.h"
|
||||
#include "./numerics/matmul.h"
|
||||
#include "./numerics/matvec.h"
|
||||
#include "./numerics/matadd.h"
|
||||
#include "./numerics/min.h"
|
||||
#include "./numerics/max.h"
|
||||
#include "./numerics/abs.h"
|
||||
|
||||
@@ -103,6 +103,7 @@ namespace numerics{
|
||||
uint64_t threads = 1;
|
||||
#endif
|
||||
|
||||
|
||||
if (can_parallel && work > threads * 4ull) {
|
||||
inplace_transpose_square_omp(A);
|
||||
}else {
|
||||
@@ -118,8 +119,10 @@ namespace numerics{
|
||||
|
||||
uint64_t work = A.rows() * A.cols();
|
||||
|
||||
|
||||
|
||||
if (rows==cols){
|
||||
utils::Matrix<T> B(rows, cols, T{0});
|
||||
utils::Matrix<T> B = A;
|
||||
inplace_transpose_square_auto(B);
|
||||
return B;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user