From 02b40561ec51a466e382ba7605d911d2354380c6 Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Thu, 20 Jan 2022 18:53:10 +0000 Subject: [PATCH] - refactored git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@853 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- poet_2v_5s.prj | 2 +- source/RnnStack.hpp | 27 ++++++++------------------- source/poet.cpp | 38 +++++++------------------------------- 3 files changed, 16 insertions(+), 51 deletions(-) diff --git a/poet_2v_5s.prj b/poet_2v_5s.prj index b8eb524..a1366b0 100644 --- a/poet_2v_5s.prj +++ b/poet_2v_5s.prj @@ -128,4 +128,4 @@ "type" : 2, "type_string" : "Rnn" } -} \ No newline at end of file +} diff --git a/source/RnnStack.hpp b/source/RnnStack.hpp index 990f1c6..9a1eeeb 100644 --- a/source/RnnStack.hpp +++ b/source/RnnStack.hpp @@ -33,32 +33,21 @@ public: arma::mat step_forward(arma::mat &state, const arma::mat &v); - arma::mat row_one_hot(size_t len, size_t index) + void clamp_one_hot(arma::mat &srcDst) { - arma::mat data = arma::zeros(1, len); - data[index] = 1; - - return data; + int index = srcDst.index_max(); + srcDst = arma::zeros(arma::size(srcDst)); + srcDst[index] = 1; } - arma::mat to_next(const arma::mat &v, bool clamp=false) + arma::mat to_next(const arma::mat &v) { - arma::mat res = v.cols(0, NUM_CODES-1); - if (clamp) - { - res = row_one_hot(res.n_cols, res.index_max()); - } - return res; + return v.cols(0, NUM_CODES-1); } - arma::mat to_curr(const arma::mat &v, bool clamp=false) + arma::mat to_curr(const arma::mat &v) { - arma::mat res = v.cols(NUM_CODES, 2*NUM_CODES-1); - if (clamp) - { - res = row_one_hot(res.n_cols, res.index_max()); - } - return res; + return v.cols(NUM_CODES, 2*NUM_CODES-1); } void setParams(Rbm::Params const ¶m) diff --git a/source/poet.cpp b/source/poet.cpp index fa68358..431e55e 100644 --- a/source/poet.cpp +++ b/source/poet.cpp @@ -61,7 +61,8 @@ class RbmListener : public Rbm::IListener curr = stack->to_curr(t_vc.row(i)); curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); arma::mat r = stack->step_forward(state, curr); - next = stack->to_next(r, true); + next = stack->to_next(r); + stack->clamp_one_hot(next); next_str.append(1,RnnTextHelper::idx2ch(next.index_max())); } cout << "Curr: " << curr_str << std::endl; @@ -72,7 +73,8 @@ class RbmListener : public Rbm::IListener curr = next; curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); arma::mat r = stack->step_forward(state, curr); - next = stack->to_next(r, true); + next = stack->to_next(r); + stack->clamp_one_hot(next); curr = stack->to_curr(r); next_str.append(1,RnnTextHelper::idx2ch(next.index_max())); } @@ -127,6 +129,8 @@ int main(int argc, char *argv[]) stack->loadTrainingBatch("."); arma::mat t_vc = stack->trainingBatch(); + RbmListener listener(*stack); + if (command == Command::Reset) { for (int i=0; i < stack->numLayers(); i++) @@ -138,8 +142,6 @@ int main(int argc, char *argv[]) if (command == Command::Train) { - RbmListener listener(*stack); - // Phase 10 stack->train(t_vc, &listener); @@ -168,33 +170,7 @@ int main(int argc, char *argv[]) if (command == Command::Forward) { - arma::mat state; - arma::mat curr; - arma::mat next; - std::string curr_str; - std::string next_str; - for (int i=0; i < stack->getSeqLen(); i++) - { - curr = stack->to_curr(t_vc.row(i)); - curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); - arma::mat r = stack->step_forward(state, curr); - next = stack->to_next(r, true); - next_str.append(1,RnnTextHelper::idx2ch(next.index_max())); - } - cout << "Curr: " << curr_str << std::endl; - cout << "Next: " << next_str << std::endl; - - for (int i=stack->getSeqLen(); i < t_vc.n_rows; i++) - { - curr = next; - curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); - arma::mat r = stack->step_forward(state, curr); - next = stack->to_next(r, true); - curr = stack->to_curr(r); - next_str.append(1,RnnTextHelper::idx2ch(next.index_max())); - } - cout << "Curr: " << curr_str << std::endl; - cout << "Next: " << next_str << std::endl; + listener.forward(); } printf("\nEnd of program\n"); return 0;