- added one hot gen
git-svn-id: http://moon:8086/svn/software/trunk/projects/Rbm@841 b431acfa-c32f-4a4a-93f1-934dc6c82436
This commit is contained in:
+1
-1
@@ -77,7 +77,7 @@ void RnnStack::train(const arma::mat& batch, Rbm::IListener* pListener)
|
||||
arma::mat batch_shifted = batch_padded.rows(k, numTraining+k-1);
|
||||
arma::mat vc = v_to_vc(batch_shifted, c);
|
||||
pLayer->train(vc, pListener);
|
||||
c = pLayer->to_h_gibbs(vc);
|
||||
pLayer->gibbs_vh(vc, c);
|
||||
pLayer = pLayer->next;
|
||||
}
|
||||
}
|
||||
|
||||
+22
-4
@@ -36,14 +36,32 @@ public:
|
||||
|
||||
arma::mat step_forward(arma::mat &state, const arma::mat &v);
|
||||
|
||||
arma::mat to_next(const arma::mat &v)
|
||||
arma::mat row_one_hot(size_t len, size_t index)
|
||||
{
|
||||
return v.cols(0, NUM_CODES-1);
|
||||
arma::mat data = arma::zeros(1, len);
|
||||
data[index] = 1;
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
arma::mat to_next(const arma::mat &v, bool clamp=false)
|
||||
{
|
||||
arma::mat res = 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)
|
||||
arma::mat to_curr(const arma::mat &v, bool clamp=false)
|
||||
{
|
||||
return v.cols(NUM_CODES, 2*NUM_CODES-1);
|
||||
arma::mat res = 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)
|
||||
|
||||
+20
-21
@@ -36,21 +36,11 @@ class RbmListener : public Rbm::IListener
|
||||
|
||||
|
||||
#define CREATE_TRAINING 0
|
||||
#define DO_TRAINING 0
|
||||
#define DO_TRAINING 1
|
||||
#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 = RnnTextHelper::createTraining("moby_ch1.txt", RnnTextHelper::SEQ_LENGTH);
|
||||
batch.save("poet2.training.dat", arma::arma_ascii);
|
||||
@@ -58,7 +48,7 @@ int main()
|
||||
#endif
|
||||
|
||||
// Load project
|
||||
RnnStack *stack = reinterpret_cast<RnnStack*>(StackCreator::fromFile(".", "poet2"));
|
||||
RnnStack *stack = reinterpret_cast<RnnStack*>(StackCreator::fromFile(".", "poet_2v_5s"));
|
||||
|
||||
// Load weights
|
||||
stack->loadWeights(".");
|
||||
@@ -98,22 +88,31 @@ int main()
|
||||
#if DO_FORWARD
|
||||
arma::mat state;
|
||||
arma::mat curr;
|
||||
arma::mat next;
|
||||
std::string curr_str;
|
||||
std::string next_str;
|
||||
for (int i=0; i < t_vc.n_rows; 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);
|
||||
arma::mat next = stack->to_next(r);
|
||||
char c = RnnTextHelper::idx2ch(next.index_max());
|
||||
putchar(c);
|
||||
next = stack->to_next(r, true);
|
||||
next_str.append(1,RnnTextHelper::idx2ch(next.index_max()));
|
||||
}
|
||||
printf("\n");
|
||||
for (int i=0; i < 40; i++)
|
||||
cout << "Curr: " << curr_str << std::endl;
|
||||
cout << "Next: " << next_str << std::endl;
|
||||
|
||||
for (int i=0; i < 430; i++)
|
||||
{
|
||||
curr = stack->to_next(stack->step_forward(state, curr));
|
||||
char c = RnnTextHelper::idx2ch(curr.index_max());
|
||||
putchar(c);
|
||||
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()));
|
||||
}
|
||||
printf("\n");
|
||||
cout << "Curr: " << curr_str << std::endl;
|
||||
cout << "Next: " << next_str << std::endl;
|
||||
#endif
|
||||
|
||||
printf("\nEnd of program\n");
|
||||
|
||||
Reference in New Issue
Block a user