From 7232870123774a772766eb829bdf3701ee53a42d Mon Sep 17 00:00:00 2001 From: Jens Ahrensfeld Date: Wed, 19 Jan 2022 11:02:11 +0000 Subject: [PATCH] - added one hot gen git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@841 b431acfa-c32f-4a4a-93f1-934dc6c82436 --- source/RnnStack.cpp | 2 +- source/RnnStack.hpp | 26 ++++++++++++++++++++++---- source/poet.cpp | 41 ++++++++++++++++++++--------------------- 3 files changed, 43 insertions(+), 26 deletions(-) diff --git a/source/RnnStack.cpp b/source/RnnStack.cpp index ceb4b33..f6da572 100644 --- a/source/RnnStack.cpp +++ b/source/RnnStack.cpp @@ -77,7 +77,7 @@ void RnnStack::train(const arma::mat& batch, Rbm::IListener* pListener) arma::mat batch_shifted = batch_padded.rows(k, numTraining+k-1); arma::mat vc = v_to_vc(batch_shifted, c); pLayer->train(vc, pListener); - c = pLayer->to_h_gibbs(vc); + pLayer->gibbs_vh(vc, c); pLayer = pLayer->next; } } diff --git a/source/RnnStack.hpp b/source/RnnStack.hpp index 2541cba..d47d1a8 100644 --- a/source/RnnStack.hpp +++ b/source/RnnStack.hpp @@ -36,14 +36,32 @@ public: arma::mat step_forward(arma::mat &state, const arma::mat &v); - arma::mat to_next(const arma::mat &v) + arma::mat row_one_hot(size_t len, size_t index) { - return v.cols(0, NUM_CODES-1); + arma::mat data = arma::zeros(1, len); + data[index] = 1; + + return data; + } + + arma::mat to_next(const arma::mat &v, bool clamp=false) + { + arma::mat res = v.cols(0, NUM_CODES-1); + if (clamp) + { + res = row_one_hot(res.n_cols, res.index_max()); + } + return res; } - arma::mat to_curr(const arma::mat &v) + arma::mat to_curr(const arma::mat &v, bool clamp=false) { - return v.cols(NUM_CODES, 2*NUM_CODES-1); + 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; } void setParams(Rbm::Params const ¶m) diff --git a/source/poet.cpp b/source/poet.cpp index 5a0e3c0..2333ced 100644 --- a/source/poet.cpp +++ b/source/poet.cpp @@ -36,21 +36,11 @@ class RbmListener : public Rbm::IListener #define CREATE_TRAINING 0 -#define DO_TRAINING 0 +#define DO_TRAINING 1 #define DO_FORWARD 1 int main() { - arma::mat sm = arma::zeros(8, 4); - - for (int i=0; i < sm.n_rows; i++) - { - sm = arma::shift(sm, 1, 0); - //sm(0,0) = i; - sm.row(0) = i*arma::eye(1,4); - sm.print("sm"); - } - #if CREATE_TRAINING arma::mat batch = RnnTextHelper::createTraining("moby_ch1.txt", RnnTextHelper::SEQ_LENGTH); batch.save("poet2.training.dat", arma::arma_ascii); @@ -58,7 +48,7 @@ int main() #endif // Load project - RnnStack *stack = reinterpret_cast(StackCreator::fromFile(".", "poet2")); + RnnStack *stack = reinterpret_cast(StackCreator::fromFile(".", "poet_2v_5s")); // Load weights stack->loadWeights("."); @@ -98,22 +88,31 @@ int main() #if DO_FORWARD arma::mat state; arma::mat curr; + arma::mat next; + std::string curr_str; + std::string next_str; for (int i=0; i < t_vc.n_rows; 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); - arma::mat next = stack->to_next(r); - char c = RnnTextHelper::idx2ch(next.index_max()); - putchar(c); + next = stack->to_next(r, true); + next_str.append(1,RnnTextHelper::idx2ch(next.index_max())); } - printf("\n"); - for (int i=0; i < 40; i++) + cout << "Curr: " << curr_str << std::endl; + cout << "Next: " << next_str << std::endl; + + for (int i=0; i < 430; i++) { - curr = stack->to_next(stack->step_forward(state, curr)); - char c = RnnTextHelper::idx2ch(curr.index_max()); - putchar(c); + 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())); } - printf("\n"); + cout << "Curr: " << curr_str << std::endl; + cout << "Next: " << next_str << std::endl; #endif printf("\nEnd of program\n");