summaryrefslogtreecommitdiff
path: root/htk_io/tools
diff options
context:
space:
mode:
authorTed Yin <[email protected]>2015-08-31 16:16:00 +0800
committerTed Yin <[email protected]>2015-08-31 16:16:00 +0800
commit014bbfb7e64999a75f9e0dc52267a36741281624 (patch)
tree127888bcefaf8ce4991bb4c173d6538f1172f35f /htk_io/tools
parent9e1a0931be43ea80fe7d41154007839b637d4e08 (diff)
parent2196e0a591b9bc254aa95e180adf188fd70ded68 (diff)
Merge pull request #4 from uphantom/master
support fastnn multi-thread TNetReader
Diffstat (limited to 'htk_io/tools')
-rw-r--r--htk_io/tools/nerv_to_tnet.cpp89
-rw-r--r--htk_io/tools/tnet_to_nerv.cpp78
2 files changed, 159 insertions, 8 deletions
diff --git a/htk_io/tools/nerv_to_tnet.cpp b/htk_io/tools/nerv_to_tnet.cpp
new file mode 100644
index 0000000..91be7db
--- /dev/null
+++ b/htk_io/tools/nerv_to_tnet.cpp
@@ -0,0 +1,89 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string>
+#include <cstring>
+#include <iostream>
+#include <fstream>
+
+char token[1024];
+typedef struct node
+{
+ double **mat;
+ int nrow;
+ int ncol;
+ int index;
+}node;
+
+int main(int argc, char **argv)
+{
+ if (argc != 3)
+ {
+ printf("%s nerv.model.in tnet.model.out\n", argv[0]);
+ }
+
+ freopen(argv[1], "r", stdin);
+ freopen(argv[2], "w", stdout);
+ node* trans[100], *bp[100];
+ int layers = 0;
+ int i, j, k;
+ char type[128];
+ int idx = 0;
+ char *flag = "affine";
+ while (NULL != gets(token))
+ {
+ int nrow, ncol;
+ if (token[0] == '{')
+ {
+ char *ss = strstr(token, flag);
+ if (sscanf(ss, "affine%d_%[a-z]", &idx, type) == 2)
+ {
+ node *no = new node;
+ scanf("%d %d", &nrow, &ncol);
+ no->nrow = nrow;
+ no->ncol = ncol;
+ no->index = idx;
+ no->mat = (double **)malloc(nrow * sizeof(double *));
+ for (i = 0; i < nrow; i++)
+ no->mat[i] = (double *)malloc(ncol * sizeof(double));
+ for (i = 0; i < nrow; i++)
+ for (j = 0; j < ncol; j++)
+ scanf("%lf", no->mat[i] + j);
+
+ if (nrow == 1)
+ bp[idx] = no;
+ else
+ trans[idx] = no;
+ layers++;
+ }
+ }
+ }
+ if (layers%2)
+ {
+ perror("number layers of LinearTrans and Bias is not the same.\n");
+ return -1;
+ }
+
+ layers /= 2;
+ for (i = 0; i < layers; i++)
+ {
+ printf("<biasedlinearity> %d %d\n", trans[i]->ncol, trans[i]->nrow);
+ printf("m %d %d\n", trans[i]->ncol, trans[i]->nrow);
+ for (j = 0; j < trans[i]->ncol; j++)
+ {
+ for (k = 0; k < trans[i]->nrow; k++)
+ printf("%.10lf ", trans[i]->mat[k][j]);
+ printf("\n");
+ }
+ printf("v %d", bp[i]->ncol);
+ for (j = 0; j < bp[i]->ncol; j++)
+ printf(" %.10lf", bp[i]->mat[0][j]);
+ printf("\n");
+ if (i != layers - 1 && trans[i+1]->ncol < trans[i+1]->nrow)
+ printf("<sigmoid> %d %d\n", bp[i]->ncol, bp[i]->ncol);
+ }
+ printf("<softmax> %d %d\n", bp[layers - 1]->ncol, bp[layers - 1]->ncol);
+ perror("done!\n");
+
+ return 0;
+}
+
diff --git a/htk_io/tools/tnet_to_nerv.cpp b/htk_io/tools/tnet_to_nerv.cpp
index a779a25..f96781a 100644
--- a/htk_io/tools/tnet_to_nerv.cpp
+++ b/htk_io/tools/tnet_to_nerv.cpp
@@ -2,14 +2,22 @@
#include <fstream>
#include <string>
#include <cstring>
-#include <cstdlib>
+#include <stdlib.h>
char token[1024];
char output[1024];
double **mat;
int main(int argc, char **argv) {
+
+ if (argc != 3)
+ {
+ printf("%s tnet.model.in nerv.model.out\n", argv[0]);
+ }
+
std::ofstream fout;
- fout.open(argv[1]);
- int cnt = 0;
+ freopen(argv[1], "r", stdin);
+ fout.open(argv[2]);
+ int cnt = 0, bias = 1, win = 1;
+ long length = 0, base = 0;
while (scanf("%s", token) != EOF)
{
int nrow, ncol;
@@ -19,13 +27,13 @@ int main(int argc, char **argv) {
scanf("%d %d", &ncol, &nrow);
scanf("%s %d %d", token, &ncol, &nrow);
printf("%d %d\n", nrow, ncol);
- mat = (double **)malloc(nrow * sizeof(double *));
+ mat = (double **)malloc(nrow * sizeof(double *));
for (i = 0; i < nrow; i++)
mat[i] = (double *)malloc(ncol * sizeof(double));
for (j = 0; j < ncol; j++)
for (i = 0; i < nrow; i++)
scanf("%lf", mat[i] + j);
- long base = fout.tellp();
+ base = fout.tellp();
sprintf(output, "%16d", 0);
fout << output;
sprintf(output, "{type=\"nerv.LinearTransParam\",id=\"affine%d_ltp\"}\n",
@@ -38,10 +46,8 @@ int main(int argc, char **argv) {
for (j = 0; j < ncol; j++)
fout << mat[i][j] << " ";
fout << std::endl;
- free(mat[i]);
}
- free(mat);
- long length = fout.tellp() - base;
+ length = fout.tellp() - base;
fout.seekp(base);
sprintf(output, "[%13lu]\n", length);
fout << output;
@@ -69,6 +75,62 @@ int main(int argc, char **argv) {
cnt++;
}
}
+ else if (strcmp(token, "<bias>") == 0)
+ {
+ scanf("%d %d", &ncol, &nrow);
+ scanf("%s %d", token, &ncol);
+ base = fout.tellp();
+ nrow = 1;
+ mat = (double **)malloc(nrow * sizeof(double *));
+ for (i = 0; i < nrow; i++)
+ mat[i] = (double *)malloc(ncol * sizeof(double));
+ for (j = 0; j < ncol; j++)
+ for (i = 0; i < nrow; i++)
+ scanf("%lf", mat[i] + j);
+ sprintf(output, "%16d", 0);
+ fout << output;
+ sprintf(output, "{type=\"nerv.MatrixParam\",id=\"bias%d\"}\n",bias);
+ fout << output;
+ sprintf(output, "1 %d\n", ncol);
+ fout << output;
+ for (j = 0; j < ncol; j++)
+ fout << mat[0][j] << " ";
+ fout << std::endl;
+ length = fout.tellp() - base;
+ fout.seekp(base);
+ sprintf(output, "[%13lu]\n", length);
+ fout << output;
+ fout.seekp(0, std::ios_base::end);
+ bias++;
+ }
+ else if (strcmp(token, "<window>") == 0)
+ {
+ scanf("%d %d", &ncol, &nrow);
+ scanf("%s %d", token, &ncol);
+ base = fout.tellp();
+ nrow = 1;
+ mat = (double **)malloc(nrow * sizeof(double *));
+ for (i = 0; i < nrow; i++)
+ mat[i] = (double *)malloc(ncol * sizeof(double));
+ for (j = 0; j < ncol; j++)
+ for (i = 0; i < nrow; i++)
+ scanf("%lf", mat[i] + j);
+ sprintf(output, "%16d", 0);
+ fout << output;
+ sprintf(output, "{type=\"nerv.MatrixParam\",id=\"window%d\"}\n",win);
+ fout << output;
+ sprintf(output, "1 %d\n", ncol);
+ fout << output;
+ for (j = 0; j < ncol; j++)
+ fout << mat[0][j] << " ";
+ fout << std::endl;
+ length = fout.tellp() - base;
+ fout.seekp(base);
+ sprintf(output, "[%13lu]\n", length);
+ fout << output;
+ fout.seekp(0, std::ios_base::end);
+ win++;
+ }
}
return 0;
}