- added char2vec and vec2char

- refactored

git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@859 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
2022-01-21 15:27:30 +00:00
parent a967f08874
commit 97c9dcce7c
2 changed files with 22 additions and 6 deletions
+18
View File
@@ -14,6 +14,8 @@
#ifndef MATUTILS_HPP
#define MATUTILS_HPP
#include "RnnTextHelper.hpp"
#include <math.h>
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 */
+4 -6
View File
@@ -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");