- refactored
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@853 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+8
-19
@@ -33,32 +33,21 @@ public:
|
|||||||
|
|
||||||
arma::mat step_forward(arma::mat &state, const arma::mat &v);
|
arma::mat step_forward(arma::mat &state, const arma::mat &v);
|
||||||
|
|
||||||
arma::mat row_one_hot(size_t len, size_t index)
|
void clamp_one_hot(arma::mat &srcDst)
|
||||||
{
|
{
|
||||||
arma::mat data = arma::zeros(1, len);
|
int index = srcDst.index_max();
|
||||||
data[index] = 1;
|
srcDst = arma::zeros(arma::size(srcDst));
|
||||||
|
srcDst[index] = 1;
|
||||||
return data;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
arma::mat to_next(const arma::mat &v, bool clamp=false)
|
arma::mat to_next(const arma::mat &v)
|
||||||
{
|
{
|
||||||
arma::mat res = v.cols(0, NUM_CODES-1);
|
return 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, bool clamp=false)
|
arma::mat to_curr(const arma::mat &v)
|
||||||
{
|
{
|
||||||
arma::mat res = v.cols(NUM_CODES, 2*NUM_CODES-1);
|
return 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)
|
void setParams(Rbm::Params const ¶m)
|
||||||
|
|||||||
+7
-31
@@ -61,7 +61,8 @@ class RbmListener : public Rbm::IListener
|
|||||||
curr = stack->to_curr(t_vc.row(i));
|
curr = stack->to_curr(t_vc.row(i));
|
||||||
curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max()));
|
curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max()));
|
||||||
arma::mat r = stack->step_forward(state, curr);
|
arma::mat r = stack->step_forward(state, curr);
|
||||||
next = stack->to_next(r, true);
|
next = stack->to_next(r);
|
||||||
|
stack->clamp_one_hot(next);
|
||||||
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
||||||
}
|
}
|
||||||
cout << "Curr: " << curr_str << std::endl;
|
cout << "Curr: " << curr_str << std::endl;
|
||||||
@@ -72,7 +73,8 @@ class RbmListener : public Rbm::IListener
|
|||||||
curr = next;
|
curr = next;
|
||||||
curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max()));
|
curr_str.append(1,RnnTextHelper::idx2ch(curr.index_max()));
|
||||||
arma::mat r = stack->step_forward(state, curr);
|
arma::mat r = stack->step_forward(state, curr);
|
||||||
next = stack->to_next(r, true);
|
next = stack->to_next(r);
|
||||||
|
stack->clamp_one_hot(next);
|
||||||
curr = stack->to_curr(r);
|
curr = stack->to_curr(r);
|
||||||
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
||||||
}
|
}
|
||||||
@@ -127,6 +129,8 @@ int main(int argc, char *argv[])
|
|||||||
stack->loadTrainingBatch(".");
|
stack->loadTrainingBatch(".");
|
||||||
arma::mat t_vc = stack->trainingBatch();
|
arma::mat t_vc = stack->trainingBatch();
|
||||||
|
|
||||||
|
RbmListener listener(*stack);
|
||||||
|
|
||||||
if (command == Command::Reset)
|
if (command == Command::Reset)
|
||||||
{
|
{
|
||||||
for (int i=0; i < stack->numLayers(); i++)
|
for (int i=0; i < stack->numLayers(); i++)
|
||||||
@@ -138,8 +142,6 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (command == Command::Train)
|
if (command == Command::Train)
|
||||||
{
|
{
|
||||||
RbmListener listener(*stack);
|
|
||||||
|
|
||||||
// Phase 10
|
// Phase 10
|
||||||
stack->train(t_vc, &listener);
|
stack->train(t_vc, &listener);
|
||||||
|
|
||||||
@@ -168,33 +170,7 @@ int main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (command == Command::Forward)
|
if (command == Command::Forward)
|
||||||
{
|
{
|
||||||
arma::mat state;
|
listener.forward();
|
||||||
arma::mat curr;
|
|
||||||
arma::mat next;
|
|
||||||
std::string curr_str;
|
|
||||||
std::string next_str;
|
|
||||||
for (int i=0; i < stack->getSeqLen(); 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);
|
|
||||||
next = stack->to_next(r, true);
|
|
||||||
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
|
||||||
}
|
|
||||||
cout << "Curr: " << curr_str << std::endl;
|
|
||||||
cout << "Next: " << next_str << std::endl;
|
|
||||||
|
|
||||||
for (int i=stack->getSeqLen(); i < t_vc.n_rows; i++)
|
|
||||||
{
|
|
||||||
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()));
|
|
||||||
}
|
|
||||||
cout << "Curr: " << curr_str << std::endl;
|
|
||||||
cout << "Next: " << next_str << std::endl;
|
|
||||||
}
|
}
|
||||||
printf("\nEnd of program\n");
|
printf("\nEnd of program\n");
|
||||||
return 0;
|
return 0;
|
||||||
|
|||||||
Reference in New Issue
Block a user