#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;
}