mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
fix comments
This commit is contained in:
parent
a0060119ab
commit
8dc640a6e2
23 changed files with 136 additions and 134 deletions
|
@ -27,33 +27,33 @@ public:
|
|||
private:
|
||||
struct worker {
|
||||
public:
|
||||
// 入力
|
||||
// in
|
||||
sprout::array<value_type, In + 1> xi1;
|
||||
sprout::array<value_type, Hid + 1> xi2;
|
||||
sprout::array<value_type, Out> xi3;
|
||||
// 出力
|
||||
// out
|
||||
sprout::array<value_type, In + 1> o1;
|
||||
sprout::array<value_type, Hid + 1> o2;
|
||||
sprout::array<value_type, Out> o3;
|
||||
};
|
||||
private:
|
||||
// 誤差
|
||||
// error
|
||||
sprout::array<value_type, Hid + 1> d2;
|
||||
sprout::array<value_type, Out> d3;
|
||||
// 重み
|
||||
// weight
|
||||
sprout::array<value_type, (In + 1) * Hid> w1;
|
||||
sprout::array<value_type, (Hid + 1) * Out> w2;
|
||||
private:
|
||||
// 順伝播
|
||||
// forward propagation
|
||||
template<typename ForwardIterator>
|
||||
SPROUT_CXX14_CONSTEXPR void
|
||||
forward_propagation(ForwardIterator in_first, ForwardIterator in_last, worker& work) const {
|
||||
// 入力層の順伝播
|
||||
// forward propagation with input layer
|
||||
sprout::copy(in_first, in_last, sprout::begin(work.xi1));
|
||||
work.xi1[In] = 1;
|
||||
sprout::copy(sprout::begin(work.xi1), sprout::end(work.xi1), sprout::begin(work.o1));
|
||||
|
||||
// 隠れ層の順伝播
|
||||
// forward propagation with hidden layer
|
||||
for (std::size_t i = 0; i != Hid; ++i) {
|
||||
work.xi2[i] = 0;
|
||||
for (std::size_t j = 0; j != In + 1; ++j) {
|
||||
|
@ -63,7 +63,7 @@ private:
|
|||
}
|
||||
work.o2[Hid] = 1;
|
||||
|
||||
// 出力層の順伝播
|
||||
// forward propagation with output layer
|
||||
for (std::size_t i = 0; i != Hid; ++i) {
|
||||
work.xi3[i] = 0;
|
||||
for (std::size_t j = 0; j != In + 1; ++j) {
|
||||
|
@ -80,9 +80,9 @@ public:
|
|||
, w2(sprout::random::generate_array<(Hid + 1) * Out>(rng, sprout::random::uniform_01<value_type>()))
|
||||
{}
|
||||
|
||||
// ニューラルネットの訓練
|
||||
// [in_first, in_last) : 訓練データ (N*In 個)
|
||||
// [t_first, t_last) : 教師データ (N 個)
|
||||
// training of neural network
|
||||
// [in_first, in_last) : training data (N*In elements)
|
||||
// [t_first, t_last) : training data (N elements)
|
||||
template<typename ForwardIterator1, typename ForwardIterator2>
|
||||
SPROUT_CXX14_CONSTEXPR void
|
||||
train(
|
||||
|
@ -99,24 +99,24 @@ public:
|
|||
ForwardIterator1 in_it = in_first;
|
||||
ForwardIterator2 t_it = t_first;
|
||||
for (; in_it != in_last; sprout::advance(in_it, In), ++t_it) {
|
||||
// 順伝播
|
||||
// forward propagation
|
||||
forward_propagation(in_it, sprout::next(in_it, In), work);
|
||||
|
||||
// 出力層の誤差計算
|
||||
// error calculation of output layer
|
||||
for (std::size_t i = 0; i != Out; ++i) {
|
||||
d3[i] = *t_it == i ? work.o3[i] - 1
|
||||
: work.o3[i]
|
||||
;
|
||||
}
|
||||
|
||||
// 出力層の重み更新
|
||||
// weight update of output layer
|
||||
for (std::size_t i = 0; i != Hid + 1; ++i) {
|
||||
for (std::size_t j = 0; j != Out; ++j) {
|
||||
w2[i * Out + j] -= eta * d3[j] * work.o2[i];
|
||||
}
|
||||
}
|
||||
|
||||
// 隠れ層の誤差計算
|
||||
// error calculation of hidden layer
|
||||
for (std::size_t i = 0; i != Hid + 1; ++i) {
|
||||
d2[i] = 0;
|
||||
for (std::size_t j = 0; j != Out; ++j) {
|
||||
|
@ -125,7 +125,7 @@ public:
|
|||
d2[i] *= sprout::math::d_sigmoid(work.xi2[i]);
|
||||
}
|
||||
|
||||
// 隠れ層の重み更新
|
||||
// weight update of hidden layer
|
||||
for (std::size_t i = 0; i != In + 1; ++i) {
|
||||
for (std::size_t j = 0; j != Hid; ++j) {
|
||||
w1[i * Hid + j] -= eta * d2[j] * work.o1[i];
|
||||
|
@ -135,7 +135,7 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
// 与えられたデータに対して最も可能性の高いクラスを返す
|
||||
// returns to predict the most likely class for a given data
|
||||
template<typename ForwardIterator>
|
||||
SPROUT_CXX14_CONSTEXPR std::size_t
|
||||
predict(ForwardIterator in_first, ForwardIterator in_last) const {
|
||||
|
@ -143,10 +143,10 @@ public:
|
|||
|
||||
worker work{};
|
||||
|
||||
// 順伝播による予測
|
||||
// prediction by forward propagation
|
||||
forward_propagation(in_first, in_last, work);
|
||||
|
||||
// 出力が最大になるクラスを判定
|
||||
// determining a class which output is maximum
|
||||
return sprout::distance(
|
||||
sprout::begin(work.o3),
|
||||
sprout::max_element(sprout::begin(work.o3), sprout::end(work.o3))
|
||||
|
@ -161,11 +161,11 @@ public:
|
|||
#include <sprout/random/unique_seed.hpp>
|
||||
#include <sprout/static_assert.hpp>
|
||||
|
||||
// 訓練データ
|
||||
// training data
|
||||
SPROUT_CONSTEXPR auto train_data = sprout::make_array<double>(
|
||||
# include "g3_train.csv"
|
||||
);
|
||||
// 教師データ
|
||||
// teaching data
|
||||
SPROUT_CONSTEXPR auto teach_data = sprout::make_array<std::size_t>(
|
||||
# include "g3_teach.csv"
|
||||
);
|
||||
|
@ -173,17 +173,17 @@ SPROUT_CONSTEXPR auto teach_data = sprout::make_array<std::size_t>(
|
|||
SPROUT_STATIC_ASSERT(train_data.size() % 2 == 0);
|
||||
SPROUT_STATIC_ASSERT(train_data.size() / 2 == teach_data.size());
|
||||
|
||||
// 訓練済みパーセプトロンを生成
|
||||
// generate a trained perceptron
|
||||
template<typename FloatType, std::size_t In, std::size_t Hid, std::size_t Out>
|
||||
SPROUT_CXX14_CONSTEXPR ::perceptron<FloatType, In, Hid, Out>
|
||||
make_trained_perceptron() {
|
||||
// 乱数生成器
|
||||
// random number generator
|
||||
sprout::random::default_random_engine rng(SPROUT_UNIQUE_SEED);
|
||||
|
||||
// パーセプトロン
|
||||
// perceptron
|
||||
::perceptron<FloatType, In, Hid, Out> per(rng);
|
||||
|
||||
// 訓練
|
||||
// training
|
||||
per.train(
|
||||
train_data.begin(), train_data.end(),
|
||||
teach_data.begin(), teach_data.end(),
|
||||
|
@ -194,10 +194,10 @@ make_trained_perceptron() {
|
|||
}
|
||||
|
||||
int main() {
|
||||
// パーセプトロンを生成(入力2 隠れ3 出力3)
|
||||
// generate a Perceptron (input 2, hidden 3, output 3)
|
||||
SPROUT_CXX14_CONSTEXPR auto per = ::make_trained_perceptron<double, 2, 3, 3>();
|
||||
|
||||
// 結果の表示
|
||||
// print results
|
||||
for (auto it = train_data.begin(), last = train_data.end(); it != last; it += 2) {
|
||||
std::cout << per.predict(it, it + 2) << std::endl;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue