-
Notifications
You must be signed in to change notification settings - Fork 36
/
CNN.hpp
90 lines (76 loc) · 2.77 KB
/
CNN.hpp
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
//-------------------------------------------------------------------------------
// @brief
// Deep CNN neural network
//
// @author
// Millhaus.Chen @time 2017/10/07 10:58
//-------------------------------------------------------------------------------
#pragma once
#include "math/sigfunc.h"
#include "math/Matrix.hpp"
#include "util/UnpackArgs.hpp"
#include "util/TupleTool.hpp"
#include "include/Parameter.hpp"
#include <tuple>
#include <utility>
namespace mtl {
namespace cnn
{
/// Type helper
template<typename I, int... Layers>
struct Type;
template<std::size_t... I, int... Layers>
struct Type<std::index_sequence<I...>, Layers...>
{
typedef /// Weights type
std::tuple<
Matrix<
double,
UnpackInts<I, Layers...>::value,
UnpackInts<I + 1, Layers...>::value
>...
> Weights;
typedef /// Thresholds type
std::tuple<
Matrix<
double,
1,
UnpackInts<I + 1, Layers...>::value
>...
> Thresholds;
};
}
/// The neural network class
template<int... Layers>
class CNN : public NNParam
{
static const int N = sizeof...(Layers);
using expander = int[];
public:
using InMatrix = Matrix<double, 1, UnpackInts<0, Layers...>::value>;
using OutMatrix = Matrix<double, 1, UnpackInts<N - 1, Layers...>::value>;
public:
CNN<Layers...>& init();
template<class LX, class LY, class W, class T>
void forward(LX& layerX, LY& layerY, W& weight, T& threshold);
template<class LX, class W, class T, class DX, class DY>
void backward(LX& layerX, W& weight, T& threshold, DX& deltaX, DY& deltaY);
template<std::size_t... I>
bool train(const InMatrix& input, const OutMatrix& output, int times, double nor, std::index_sequence<I...>);
bool train(const InMatrix& input, const OutMatrix& output, int times = 1, double nor = 1)
{ return train(input, output, times, nor, std::make_index_sequence<N - 1>());
}
template<std::size_t... I>
double simulate(const InMatrix& input, OutMatrix& output, OutMatrix& expect, double nor, std::index_sequence<I...>);
double simulate(const InMatrix& input, OutMatrix& output, OutMatrix& expect, double nor = 1)
{ return simulate(input, output, expect, nor, std::make_index_sequence<N - 1>());
}
public:
std::tuple<Matrix<double, 1, Layers>...> m_layers;
typename cnn::Type<std::make_index_sequence<N - 1>, Layers...>::Weights m_weights;
typename cnn::Type<std::make_index_sequence<N - 1>, Layers...>::Thresholds m_thresholds;
std::tuple<Matrix<double, 1, Layers>...> m_deltas;
OutMatrix m_aberrmx;
};
}
#include "include/CNN.inl"