From 2196e0a591b9bc254aa95e180adf188fd70ded68 Mon Sep 17 00:00:00 2001 From: uphantom Date: Mon, 31 Aug 2015 09:01:10 +0800 Subject: transfer nerv to tnet model. --- htk_io/tools/nerv_to_tnet.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 htk_io/tools/nerv_to_tnet.cpp 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 +#include +#include +#include +#include +#include + +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(" %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(" %d %d\n", bp[i]->ncol, bp[i]->ncol); + } + printf(" %d %d\n", bp[layers - 1]->ncol, bp[layers - 1]->ncol); + perror("done!\n"); + + return 0; +} + -- cgit v1.2.3-70-g09d2