- use RnnTextHelper
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@837 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: RnnTextHelper.cpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 18. Januar 2022, 19:12
|
||||
*/
|
||||
|
||||
#include "RnnTextHelper.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
RnnTextHelper::RnnTextHelper()
|
||||
{
|
||||
}
|
||||
|
||||
RnnTextHelper::~RnnTextHelper()
|
||||
{
|
||||
}
|
||||
|
||||
int RnnTextHelper::char_is(char c, const char* pTable)
|
||||
{
|
||||
int found = 0;
|
||||
while (*pTable)
|
||||
{
|
||||
if (c == *pTable)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
pTable++;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
int RnnTextHelper::ch2idx(char c)
|
||||
{
|
||||
c = toupper(c);
|
||||
|
||||
int index = 0;
|
||||
if (isalpha(c))
|
||||
{
|
||||
index = 1 + (int) (c - 'A');
|
||||
}
|
||||
if (isdigit(c))
|
||||
{
|
||||
index = 1 + 26 + (int) (c - '0');
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
char RnnTextHelper::idx2ch(int index)
|
||||
{
|
||||
char c = '?';
|
||||
if (index == 0)
|
||||
{
|
||||
c = ' ';
|
||||
}
|
||||
else if (index >= 1 and index < 27)
|
||||
{
|
||||
c = (char) (index + 'A' - 1);
|
||||
}
|
||||
else if (index >= 27 and index < 38)
|
||||
{
|
||||
c = (char) (index + '0' - 27);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
arma::mat RnnTextHelper::createTraining(const string& filename, size_t seq_len)
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(filename.c_str(), "r");
|
||||
|
||||
if (!pFile)
|
||||
{
|
||||
std::cout << "Could not open " << filename << "!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int numTraining = 0;
|
||||
int numVisible = seq_len*NUM_CODES;
|
||||
|
||||
arma::mat batch = arma::zeros(numTraining, numVisible);
|
||||
arma::mat pattern = arma::zeros(seq_len, NUM_CODES);
|
||||
|
||||
for (int i = 0; i < seq_len; i++)
|
||||
{
|
||||
pattern.row(i)[0] = 1;
|
||||
}
|
||||
|
||||
int last_index = ch2idx(' ');
|
||||
while (!feof(pFile))
|
||||
{
|
||||
char c;
|
||||
int result = fread(&c, 1, 1, pFile);
|
||||
if (result < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
int index = ch2idx(c);
|
||||
if (index == 0 and last_index == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
last_index = index;
|
||||
pattern = arma::shift(pattern, 1);
|
||||
arma::mat data = arma::zeros(1, NUM_CODES);
|
||||
data[index] = 1;
|
||||
pattern.row(0) = data;
|
||||
arma::mat pattern_vector = pattern.as_row();
|
||||
batch.insert_rows(batch.n_rows, pattern_vector);
|
||||
}
|
||||
fclose(pFile);
|
||||
|
||||
return batch;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: RnnTextHelper.hpp
|
||||
* Author: jens
|
||||
*
|
||||
* Created on 18. Januar 2022, 19:12
|
||||
*/
|
||||
|
||||
#ifndef RNNTEXTHELPER_HPP
|
||||
#define RNNTEXTHELPER_HPP
|
||||
|
||||
#include <armadillo>
|
||||
|
||||
class RnnTextHelper
|
||||
{
|
||||
public:
|
||||
RnnTextHelper();
|
||||
RnnTextHelper(const RnnTextHelper& orig) = delete;
|
||||
virtual ~RnnTextHelper();
|
||||
|
||||
const char punctuation[5] = {' ', '.', '!', '?', 0};
|
||||
static constexpr int NUM_CODES = 1 + 26 + 10;
|
||||
static constexpr int SEQ_LENGTH = 2;
|
||||
|
||||
static int char_is(char c, const char *pTable);
|
||||
static int ch2idx(char c);
|
||||
static char idx2ch(int index);
|
||||
|
||||
static arma::mat createTraining(const std::string &filename, size_t seq_len);
|
||||
|
||||
private:
|
||||
|
||||
};
|
||||
|
||||
#endif /* RNNTEXTHELPER_HPP */
|
||||
|
||||
+14
-106
@@ -11,6 +11,7 @@
|
||||
#include "Layer.hpp"
|
||||
#include "RnnStack.hpp"
|
||||
#include "StackCreator.hpp"
|
||||
#include "RnnTextHelper.hpp"
|
||||
|
||||
using namespace std;
|
||||
using namespace arma;
|
||||
@@ -33,116 +34,23 @@ class RbmListener : public Rbm::IListener
|
||||
}
|
||||
};
|
||||
|
||||
const char punctuation[] = {' ', '.', '!', '?', 0};
|
||||
const int NUM_CODES = 1 + 26 + 10;
|
||||
const int SEQ_LENGTH = 2;
|
||||
|
||||
int char_is(char c, const char *pTable)
|
||||
{
|
||||
int found = 0;
|
||||
while(*pTable)
|
||||
{
|
||||
if (c == *pTable)
|
||||
{
|
||||
found = 1;
|
||||
break;
|
||||
}
|
||||
pTable++;
|
||||
}
|
||||
return found;
|
||||
}
|
||||
|
||||
int ch2idx(char c)
|
||||
{
|
||||
c = toupper(c);
|
||||
|
||||
int index = 0;
|
||||
if (isalpha(c))
|
||||
{
|
||||
index = 1+ (int)(c-'A');
|
||||
}
|
||||
if (isdigit(c))
|
||||
{
|
||||
index = 1+26+(int)(c-'0');
|
||||
}
|
||||
return index;
|
||||
}
|
||||
|
||||
char idx2ch(int index)
|
||||
{
|
||||
char c = '?';
|
||||
if (index == 0)
|
||||
{
|
||||
c = ' ';
|
||||
}
|
||||
else if (index >= 1 and index < 27)
|
||||
{
|
||||
c = (char)(index + 'A' - 1);
|
||||
}
|
||||
else if (index >= 27 and index < 38)
|
||||
{
|
||||
c = (char)(index + '0' - 27);
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
arma::mat createTraining(const string &filename, size_t seq_len)
|
||||
{
|
||||
FILE *pFile;
|
||||
|
||||
pFile = fopen(filename.c_str(), "r");
|
||||
|
||||
if (!pFile)
|
||||
{
|
||||
std::cout << "Could not open " << filename << "!" << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int numTraining = 0;
|
||||
int numVisible = seq_len*NUM_CODES;
|
||||
|
||||
arma::mat batch = arma::zeros(numTraining, numVisible);
|
||||
arma::mat pattern = arma::zeros(seq_len, NUM_CODES);
|
||||
|
||||
for (int i=0; i < seq_len; i++)
|
||||
{
|
||||
pattern.row(i)[0] = 1;
|
||||
}
|
||||
|
||||
int last_index = ch2idx(' ');
|
||||
while(!feof(pFile))
|
||||
{
|
||||
char c;
|
||||
int result = fread(&c, 1, 1, pFile);
|
||||
if (result < 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
int index = ch2idx(c);
|
||||
if (index == 0 and last_index == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
last_index = index;
|
||||
pattern = arma::shift(pattern, 1);
|
||||
arma::mat data = arma::zeros(1, NUM_CODES);
|
||||
data[index] = 1;
|
||||
pattern.row(0) = data;
|
||||
arma::mat pattern_vector = pattern.as_row();
|
||||
batch.insert_rows(batch.n_rows, pattern_vector);
|
||||
}
|
||||
fclose(pFile);
|
||||
|
||||
return batch;
|
||||
}
|
||||
|
||||
#define CREATE_TRAINING 0
|
||||
#define DO_TRAINING 1
|
||||
#define DO_TRAINING 0
|
||||
#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 = createTraining("moby_ch1.txt", SEQ_LENGTH);
|
||||
batch.save("poet2.training.dat", arma::arma_ascii);
|
||||
@@ -195,14 +103,14 @@ int main()
|
||||
curr = stack->to_curr(t_vc.row(i));
|
||||
arma::mat r = stack->step_forward(state, curr);
|
||||
arma::mat next = stack->to_next(r);
|
||||
char c = idx2ch(next.index_max());
|
||||
char c = RnnTextHelper::idx2ch(next.index_max());
|
||||
putchar(c);
|
||||
}
|
||||
printf("\n");
|
||||
for (int i=0; i < 40; i++)
|
||||
{
|
||||
curr = stack->to_next(stack->step_forward(state, curr));
|
||||
char c = idx2ch(curr.index_max());
|
||||
char c = RnnTextHelper::idx2ch(curr.index_max());
|
||||
putchar(c);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
Reference in New Issue
Block a user