Skip to content

Commit

Permalink
fixed one bug with the matrix code; still buggy though
Browse files Browse the repository at this point in the history
  • Loading branch information
hainan-xv committed Aug 22, 2016
1 parent 1da2406 commit 7bee77d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 140 deletions.
6 changes: 3 additions & 3 deletions egs/ami/s5/local/run_rnnlm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ for decode_set in eval dev; do
--N 50 --cmd "$decode_cmd --mem 16G" --inv-acwt 10 0.5 \
data/lang_$LM data/$mic/mik_rnn \
data/$mic/$decode_set ${decode_dir} \
${decode_dir}.rnnlm.mik.50-best || exit 1
${decode_dir}.rnnlm.mik.50-best || exit 1 ) &

# Lattice rescoring with Tomas Mikolov's version.
steps/lmrescore_rnnlm_lat.sh \
( steps/lmrescore_rnnlm_lat.sh \
--weight 0.5 --cmd "$decode_cmd --mem 16G" --max-ngram-order 5 \
data/lang_$LM data/$mic/mik_rnn \
data/$mic/$decode_set ${decode_dir} \
${decode_dir}.rnnlm.mik.lat || exit 1; )&
${decode_dir}.rnnlm.mik.lat || exit 1;) &
done

wait
Expand Down
4 changes: 4 additions & 0 deletions egs/wsj/s5/utils/rnnlm_compute_scores.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ if [ "$rnnlm_ver" == "cuedrnnlm" ]; then

cat $tempdir/text > $tempdir/text.nounk2

echo "cued-rnnlm-eval -ppl -readmodel $dir/rnnlm -testfile $tempdir/text.nounk2 \
-fullvocsize $total_nwords -inputwlist $dir/rnnlm.input.wlist.index \
-outputwlist $dir/rnnlm.output.wlist.index -debug 0 "

cued-rnnlm-eval -ppl -readmodel $dir/rnnlm -testfile $tempdir/text.nounk2 \
-fullvocsize $total_nwords -inputwlist $dir/rnnlm.input.wlist.index \
-outputwlist $dir/rnnlm.output.wlist.index -debug 0 | \
Expand Down
45 changes: 14 additions & 31 deletions src/lm/cued-rnnlm-lib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1040,10 +1040,8 @@ float RNNLM::forward (int prevword, int curword) {
ncol = layersizes[1];
dstac->SetZero();

dstac->CopyFromMat(layers[0]->getMatrix()->Range(0, nrow, prevword, 1));
// for (b = 0; b < ncol; b++) {
// dstac[b] = layers[0]->fetchhostvalue(prevword, b);
// }
dstac->CopyFromMat(layers[0]->getMatrix()->Range(prevword, 1, 0, ncol),
kaldi::kTrans);

KALDI_ASSERT(dim_fea == 0);
}
Expand All @@ -1068,11 +1066,11 @@ float RNNLM::forward (int prevword, int curword) {
int swordid = classinfo[clsid*3];
int ewordid = classinfo[clsid*3+1];
int nword = classinfo[clsid*3+2];
// matrixXvector(srcac, wgt + swordid * nrow, dstac + swordid, nrow, nword);
SubMatrix<real> dstac_sub(*dstac, swordid, dstac->NumRows() - swordid,
0, dstac->NumCols());
matrixXvector(*srcac, wgt->ColRange(swordid, wgt->NumCols() - swordid),
dstac_sub, nrow, nword);

SubMatrix<real> dstac_sub(*dstac, swordid, nword, 0, 1);

matrixXvector(*srcac, wgt->ColRange(swordid, nword),
dstac_sub, nrow, nword);
neu_ac[a+1]->hostpartsoftmax(swordid, ewordid);
}
else {
Expand Down Expand Up @@ -1116,28 +1114,13 @@ float RNNLM::forward (int prevword, int curword) {
void RNNLM::matrixXvector (const MatrixBase<real> &src,
const MatrixBase<real> &wgt,
MatrixBase<real> &dst, int nr, int nc) {
KALDI_ASSERT(wgt.NumRows() == nr);
KALDI_ASSERT(wgt.NumCols() == nc);
KALDI_ASSERT(src.NumRows() == nr);
KALDI_ASSERT(src.NumCols() == 1);
KALDI_ASSERT(dst.NumRows() == nc);
KALDI_ASSERT(dst.NumCols() == 1);
dst.AddMatMat(1, wgt, kaldi::kNoTrans, src, kaldi::kNoTrans, 0);
//
// int i, j;
//#if 1
//#ifdef NUM_THREAD
//#pragma omp parallel for num_threads(NUM_THREAD)
//#else
//#pragma omp parallel for num_threads(nthread)
//#endif
//#endif
// for (i = 0; i < nc; i++) {
// for (j = 0; j < nr; j++) {
// dst[i] += src[j] * wgt[j + i * nr];
// }
// }
// return;
// KALDI_ASSERT(wgt.NumRows() == nr);
// KALDI_ASSERT(wgt.NumCols() == nc);
// KALDI_ASSERT(src.NumRows() == nr);
// KALDI_ASSERT(src.NumCols() == 1);
// KALDI_ASSERT(dst.NumRows() == nc);
// KALDI_ASSERT(dst.NumCols() == 1);
dst.AddMatMat(1, wgt, kaldi::kTrans, src, kaldi::kNoTrans, 0);
}

} // namespace cued_rnnlm
106 changes: 10 additions & 96 deletions src/lm/cued-rnnlm-lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,18 +252,10 @@ class matrix
{
private:
Matrix<real>* host_data;
// real* host_data;
// size_t nrows;
// size_t ncols;
// size_t size;
public:
matrix(): host_data(NULL) {}
matrix(size_t nr, size_t nc) {
host_data = new Matrix<real>(nr, nc);
// nrows = nr;
// ncols = nc;
// size = sizeof(real) * ncols * nrows;
// host_data = (real *) malloc (size);
}
~matrix () {
if (host_data) {
Expand All @@ -279,31 +271,20 @@ class matrix
}
// asign value on CPU
void assignhostvalue (size_t i, size_t j, real v) {
(*host_data)(j, i) = v;
// host_data[i + j * nrows] = v;
(*host_data)(i, j) = v;
}
void addhostvalue (size_t i, size_t j, real v) {
(*host_data)(j, i) += v;
// host_data[i + j * nrows] += v;
(*host_data)(i, j) += v;
}
real fetchhostvalue (size_t i, size_t j) {
return (*host_data)(j, i);
// return host_data[i + j * nrows];
return (*host_data)(i, j);
}

void setnrows (size_t nr) {
// nrows = nr;
}
void setncols (size_t nc) {
// ncols = nc;
}
size_t rows () {
return host_data->NumRows();
// return nrows;
}
size_t cols () {
return host_data->NumCols();
// return ncols;
}
void freemem () {
if (host_data) {
Expand All @@ -312,15 +293,11 @@ class matrix
}

real& operator() (int i, int j) const {
return (*host_data)(j, i);
// assert ((i >= 0) && (i < nrows) && (j >= 0) && (j < ncols));
// return host_data[i + j * nrows];
return (*host_data)(i, j);
}

const real& operator() (int i, int j) {
return (*host_data)(j, i);
// assert ((i >= 0) && (i < nrows) && (j >= 0) && (j < ncols));
// return host_data[i + j * nrows];
return (*host_data)(i, j);
}

Matrix<real>* getMatrix() {
Expand All @@ -332,13 +309,12 @@ class matrix
}

real *gethostdataptr(int i, int j) {
return &(*host_data)(j, i);
return &(*host_data)(i, j);
}

// initialize all element (both GPU and CPU) in matrx with v
void initmatrix (int v = 0) {
host_data->Set(v);
// memset (host_data, v, Sizeof());
}

void hostrelu (float ratio)
Expand All @@ -347,85 +323,24 @@ class matrix

host_data->Scale(ratio);
host_data->ApplyFloor(0);

// assert (ncols == 1);
// for (int i = 0; i < nrows; i++) {
// if (host_data[i] > 0) {
// host_data[i] *= ratio;
// }
// else {
// host_data[i] = 0;
// }
// }
}

void hostsigmoid() {
KALDI_ASSERT(host_data->NumCols() == 1);

// TODO(hxu) not sure
host_data->Sigmoid(*host_data);

// assert (ncols == 1);
// for (int i = 0; i< nrows; i++) {
// host_data[i] = 1.0 / (1 + exp(-host_data[i]));
// }
}

void hostsoftmax() {
KALDI_ASSERT(host_data->NumCols() == 1);
host_data->ApplySoftMax();
//// int a, maxi;
// int a;
// float v, norm, maxv = 1e-8;
// assert (ncols == 1);
// maxv = 1e-10;
// for (a = 0; a < nrows; a++) {
// v = host_data[a];
// if (v > maxv) {
// maxv = v;
//// maxi = a;
// }
// }
// norm = 0;
//
// for (a = 0; a < nrows; a++) {
// v = host_data[a] - maxv;
// host_data[a] = exp(v);
// norm += host_data[a];
// }
// for (a = 0; a < nrows; a++) {
// v = host_data[a] / norm;
// host_data[a] = v;
// }
}

void hostpartsoftmax(int swordid, int ewordid) {
KALDI_ASSERT(host_data->NumCols() == 1);
SubMatrix<real> t(*host_data, swordid, ewordid - swordid, 0, 1);
SubMatrix<real> t(*host_data, swordid, 1 + ewordid - swordid, 0, 1);
t.ApplySoftMax();

//// int a, maxi;
// int a;
// float v, norm, maxv = 1e-8;
// assert (ncols == 1);
// maxv = 1e-10;
// for (a = swordid; a <= ewordid; a++) {
// v = host_data[a];
// if (v > maxv) {
// maxv = v;
//// maxi = a;
// }
// }
// norm = 0;
//
// for (a = swordid; a <= ewordid; a++) {
// v = host_data[a] - maxv;
// host_data[a] = exp(v);
// norm += host_data[a];
// }
// for (a = swordid; a <= ewordid; a++) {
// v = host_data[a] / norm;
// host_data[a] = v;
// }
}

void random(float min, float max) {
Expand All @@ -434,8 +349,7 @@ class matrix
for (i = 0; i < host_data->NumRows(); i++) {
for (j = 0; j < host_data->NumCols(); j++) {
v = randomv(min, max) + randomv(min,max) + randomv(min, max);
(*host_data)(j, i) = v;
// host_data[i + j * nrows] = v;
(*host_data)(i, j) = v;
}
}
}
Expand Down Expand Up @@ -507,7 +421,7 @@ class RNNLM
float forward (int prevword, int curword);
void ReadUnigramFile (string unigramfile);
void matrixXvector (const MatrixBase<real> &src, const MatrixBase<real> &wgt,
MatrixBase<real> &dst, int nr, int nc);
MatrixBase<real> &dst, int nr, int nc);
void allocMem (vector<int> &layersizes);

};
Expand Down
20 changes: 10 additions & 10 deletions src/lmbin/cued-rnnlm-eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ int main (int argc, char **argv)
RNNLM rnnlm (inmodelname, inputwlist, outputwlist, layersizes, fullvocsize, binformat, debug);
rnnlm.setLognormConst (lognormconst); // TODO(hxu) figure out what this does
rnnlm.setNthread(nthread);
if (flag_feature)
{
rnnlm.setFeafile (feafile);
rnnlm.ReadFeaFile (feafile);
}
// if (flag_feature)
// {
// rnnlm.setFeafile (feafile);
// rnnlm.ReadFeaFile (feafile);
// }
rnnlm.calppl(testfile, lambda, nglmstfile);
}
else if (!isEmpty(arg.find ("-nbest")))
Expand All @@ -93,11 +93,11 @@ int main (int argc, char **argv)
rnnlm.setNthread (nthread);
rnnlm.setLmscale (lmscale);
rnnlm.setIp (ip);
if (flag_feature)
{
rnnlm.setFeafile (feafile);
rnnlm.ReadFeaFile (feafile);
}
// if (flag_feature)
// {
// rnnlm.setFeafile (feafile);
// rnnlm.ReadFeaFile (feafile);
// }

rnnlm.calnbest(testfile, lambda, nglmstfile);
}
Expand Down

0 comments on commit 7bee77d

Please sign in to comment.