diff --git a/source/matutils.hpp b/source/matutils.hpp index f7468c5..4a8692d 100644 --- a/source/matutils.hpp +++ b/source/matutils.hpp @@ -14,6 +14,8 @@ #ifndef MATUTILS_HPP #define MATUTILS_HPP +#include "RnnTextHelper.hpp" + #include namespace Matutils @@ -66,6 +68,22 @@ namespace Matutils return x/stddev; } + inline arma::mat char2vec(char c, size_t len) + { + arma::mat result = arma::zeros(1, len); + int idx = RnnTextHelper::ch2idx(c); + result[idx] = 1; + + return result; + } + + inline char vec2char(arma::mat const &vec) + { + char result = RnnTextHelper::idx2ch(vec.index_max()); + + return result; + } + } #endif /* MATUTILS_HPP */ diff --git a/source/poet.cpp b/source/poet.cpp index 3d7fad2..8dff32a 100644 --- a/source/poet.cpp +++ b/source/poet.cpp @@ -60,10 +60,8 @@ class RbmListener : public Rbm::IListener for (int i=0; i < start.size(); i++) { - arma::mat curr = zeros(1, RnnTextHelper::NUM_CODES); - int idx = RnnTextHelper::ch2idx(start.at(i)); - curr[idx] = 1; - curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); + curr = Matutils::char2vec(start.at(i), RnnTextHelper::NUM_CODES); + curr_str.append(1, Matutils::vec2char(curr)); arma::mat r = stack->step_forward(state, curr); next = stack->to_next(r); stack->clamp_one_hot(next); @@ -73,7 +71,7 @@ class RbmListener : public Rbm::IListener for (int i=stack->getSeqLen(); i < t_vc.n_rows; i++) { curr = next; - curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max())); + curr_str.append(1, Matutils::vec2char(curr)); arma::mat r = stack->step_forward(state, curr); next = stack->to_next(r); stack->clamp_one_hot(next); @@ -150,7 +148,7 @@ int main(int argc, char *argv[]) for (int i=0; i < t_vc.n_rows; i++) { arma::mat curr = stack->to_curr(t_vc.row(i)); - char c = RnnTextHelper::idx2ch(curr.index_max()); + char c = Matutils::vec2char(curr); putchar(c); } printf("\n");