fix matrix type
This commit is contained in:
@@ -1,8 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <math.h>
|
#include <cmath>
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <type_traits>
|
||||||
|
#include <iostream>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <string>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <hack/logger/logger.hpp>
|
#include <hack/logger/logger.hpp>
|
||||||
@@ -14,33 +19,36 @@ namespace utils
|
|||||||
// функции активации. можно добавить еще для поиграться
|
// функции активации. можно добавить еще для поиграться
|
||||||
namespace activations
|
namespace activations
|
||||||
{
|
{
|
||||||
inline auto sigmoid = [](auto x) -> float {
|
inline auto sigmoid = [](auto x) {
|
||||||
return 1.f / (1.f + std::exp(-x));
|
using value_type = decltype(x);
|
||||||
|
return value_type{1} / (value_type{1} + std::exp(-x));
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace derivative
|
namespace derivative
|
||||||
{
|
{
|
||||||
inline auto sigmoid = [](auto x) -> float {
|
inline auto sigmoid = [](auto x) {
|
||||||
return x * (1 - x);
|
using value_type = decltype(x);
|
||||||
|
return x * (value_type{1} - x);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// коеффициент обучения
|
// коеффициент обучения
|
||||||
const float LEARNING_RATE = 0.1;
|
inline constexpr float LEARNING_RATE = 0.1f;
|
||||||
|
|
||||||
// i, j - в матрице тут - это как m, n в матрицах в учебниках
|
// i, j - в матрице тут - это как m, n в матрицах в учебниках
|
||||||
// т.е. строка и столбец соответственно
|
// т.е. строка и столбец соответственно
|
||||||
template<std::size_t Rows, std::size_t Cols, typename Type = float>
|
template<std::size_t Rows, std::size_t Cols, typename Type = float>
|
||||||
class matrix
|
class matrix
|
||||||
{
|
{
|
||||||
static_assert(std::is_arithmetic_v<Type>, "matrix element type must be arithmetic");
|
static_assert(std::is_arithmetic_v<Type> && !std::is_same_v<Type, bool>,
|
||||||
|
"matrix element type must be arithmetic and not bool");
|
||||||
static_assert(Rows > 0 && Cols > 0, "matrix dimensions must be positive");
|
static_assert(Rows > 0 && Cols > 0, "matrix dimensions must be positive");
|
||||||
|
|
||||||
public:
|
public:
|
||||||
matrix() : m_data(Rows * Cols, Type{}) {}
|
matrix() : m_data(Rows * Cols, Type{}) {}
|
||||||
matrix(Type fill_value) : m_data(Rows * Cols, fill_value) {}
|
matrix(Type fill_value) : m_data(Rows * Cols, fill_value) {}
|
||||||
matrix(std::initializer_list<std::initializer_list<float>> init)
|
matrix(std::initializer_list<std::initializer_list<Type>> init)
|
||||||
{
|
{
|
||||||
if (init.size() != Rows) throw std::invalid_argument("matrix: row count mismatch");
|
if (init.size() != Rows) throw std::invalid_argument("matrix: row count mismatch");
|
||||||
m_data.reserve(Rows * Cols);
|
m_data.reserve(Rows * Cols);
|
||||||
@@ -138,7 +146,7 @@ namespace utils
|
|||||||
std::cout << '\n';
|
std::cout << '\n';
|
||||||
}
|
}
|
||||||
private:
|
private:
|
||||||
std::vector<float> m_data;
|
std::vector<Type> m_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
// применяем эта если нужно оставить промежуточные результаты
|
// применяем эта если нужно оставить промежуточные результаты
|
||||||
|
|||||||
Reference in New Issue
Block a user