- create training one th e fly from file name git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@860 b431acfa-c32f-4a4a-93f1-934dc6c82436
129 lines
2.1 KiB
C++
129 lines
2.1 KiB
C++
/*
|
|
* 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"
|
|
#include "matutils.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)
|
|
{
|
|
const char SPACE = ' ';
|
|
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) = Matutils::char2vec(SPACE, NUM_CODES);
|
|
}
|
|
|
|
int last_index = ch2idx(SPACE);
|
|
while (!feof(pFile))
|
|
{
|
|
char ch = SPACE;
|
|
int result = fread(&ch, 1, 1, pFile);
|
|
if (result < 0)
|
|
{
|
|
break;
|
|
}
|
|
|
|
int index = ch2idx(ch);
|
|
if (index == 0 and last_index == 0)
|
|
{
|
|
continue;
|
|
}
|
|
last_index = index;
|
|
|
|
if (seq_len > 1)
|
|
{
|
|
pattern = arma::shift(pattern, 1);
|
|
}
|
|
pattern.row(0) = Matutils::char2vec(ch, NUM_CODES);
|
|
arma::mat pattern_vector = pattern.as_row();
|
|
batch.insert_rows(batch.n_rows, pattern_vector);
|
|
}
|
|
fclose(pFile);
|
|
|
|
return batch;
|
|
}
|