fix matrix type
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include <cmath>
|
||||
#include <initializer_list>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
#include <cassert>
|
||||
#include <stdexcept>
|
||||
#include <hack/logger/logger.hpp>
|
||||
@@ -14,33 +19,36 @@ namespace utils
|
||||
// функции активации. можно добавить еще для поиграться
|
||||
namespace activations
|
||||
{
|
||||
inline auto sigmoid = [](auto x) -> float {
|
||||
return 1.f / (1.f + std::exp(-x));
|
||||
inline auto sigmoid = [](auto x) {
|
||||
using value_type = decltype(x);
|
||||
return value_type{1} / (value_type{1} + std::exp(-x));
|
||||
};
|
||||
}
|
||||
|
||||
namespace derivative
|
||||
{
|
||||
inline auto sigmoid = [](auto x) -> float {
|
||||
return x * (1 - x);
|
||||
inline auto sigmoid = [](auto 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 в матрицах в учебниках
|
||||
// т.е. строка и столбец соответственно
|
||||
template<std::size_t Rows, std::size_t Cols, typename Type = float>
|
||||
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");
|
||||
|
||||
public:
|
||||
matrix() : m_data(Rows * Cols, Type{}) {}
|
||||
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");
|
||||
m_data.reserve(Rows * Cols);
|
||||
@@ -138,7 +146,7 @@ namespace utils
|
||||
std::cout << '\n';
|
||||
}
|
||||
private:
|
||||
std::vector<float> m_data;
|
||||
std::vector<Type> m_data;
|
||||
};
|
||||
|
||||
// применяем эта если нужно оставить промежуточные результаты
|
||||
|
||||
Reference in New Issue
Block a user