/************************************************************************* Serializer: serialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void dfserialize(alglib.serializer s, decisionforest forest) { s.serialize_int(scodes.getrdfserializationcode()); s.serialize_int(dffirstversion); s.serialize_int(forest.nvars); s.serialize_int(forest.nclasses); s.serialize_int(forest.ntrees); s.serialize_int(forest.bufsize); apserv.serializerealarray(s, forest.trees, forest.bufsize); }
/************************************************************************* Serializer: unserialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void mlpunserialize(alglib.serializer s, multilayerperceptron network) { int i0 = 0; int i1 = 0; int i = 0; int j = 0; int k = 0; int fkind = 0; double threshold = 0; double v0 = 0; double v1 = 0; int nin = 0; int nout = 0; bool issoftmax = new bool(); int[] layersizes = new int[0]; // // check correctness of header // i0 = s.unserialize_int(); ap.assert(i0 == scodes.getmlpserializationcode(), "MLPUnserialize: stream header corrupted"); i1 = s.unserialize_int(); ap.assert(i1 == mlpfirstversion, "MLPUnserialize: stream header corrupted"); // // Create network // issoftmax = s.unserialize_bool(); apserv.unserializeintegerarray(s, ref layersizes); ap.assert((ap.len(layersizes) == 2 | ap.len(layersizes) == 3) | ap.len(layersizes) == 4, "MLPUnserialize: too many hidden layers!"); nin = layersizes[0]; nout = layersizes[ap.len(layersizes) - 1]; if(ap.len(layersizes) == 2) { if(issoftmax) { mlpcreatec0(layersizes[0], layersizes[1], network); } else { mlpcreate0(layersizes[0], layersizes[1], network); } } if(ap.len(layersizes) == 3) { if(issoftmax) { mlpcreatec1(layersizes[0], layersizes[1], layersizes[2], network); } else { mlpcreate1(layersizes[0], layersizes[1], layersizes[2], network); } } if(ap.len(layersizes) == 4) { if(issoftmax) { mlpcreatec2(layersizes[0], layersizes[1], layersizes[2], layersizes[3], network); } else { mlpcreate2(layersizes[0], layersizes[1], layersizes[2], layersizes[3], network); } } // // Load neurons and weights // for(i = 1; i <= ap.len(layersizes) - 1; i++) { for(j = 0; j <= layersizes[i] - 1; j++) { fkind = s.unserialize_int(); threshold = s.unserialize_double(); mlpsetneuroninfo(network, i, j, fkind, threshold); for(k = 0; k <= layersizes[i - 1] - 1; k++) { v0 = s.unserialize_double(); mlpsetweight(network, i - 1, k, i, j, v0); } } } // // Load standartizator // for(j = 0; j <= nin - 1; j++) { v0 = s.unserialize_double(); v1 = s.unserialize_double(); mlpsetinputscaling(network, j, v0, v1); } for(j = 0; j <= nout - 1; j++) { v0 = s.unserialize_double(); v1 = s.unserialize_double(); mlpsetoutputscaling(network, j, v0, v1); } }
/************************************************************************* Serializer: allocation -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void dfalloc(alglib.serializer s, decisionforest forest) { s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); apserv.allocrealarray(s, forest.trees, forest.bufsize); }
/************************************************************************* Serializer: allocation -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void mlpalloc(alglib.serializer s, multilayerperceptron network) { int i = 0; int j = 0; int k = 0; int fkind = 0; double threshold = 0; double v0 = 0; double v1 = 0; int nin = 0; int nout = 0; nin = network.hllayersizes[0]; nout = network.hllayersizes[ap.len(network.hllayersizes) - 1]; s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); apserv.allocintegerarray(s, network.hllayersizes, -1); for(i = 1; i <= ap.len(network.hllayersizes) - 1; i++) { for(j = 0; j <= network.hllayersizes[i] - 1; j++) { mlpgetneuroninfo(network, i, j, ref fkind, ref threshold); s.alloc_entry(); s.alloc_entry(); for(k = 0; k <= network.hllayersizes[i - 1] - 1; k++) { s.alloc_entry(); } } } for(j = 0; j <= nin - 1; j++) { mlpgetinputscaling(network, j, ref v0, ref v1); s.alloc_entry(); s.alloc_entry(); } for(j = 0; j <= nout - 1; j++) { mlpgetoutputscaling(network, j, ref v0, ref v1); s.alloc_entry(); s.alloc_entry(); } }
/************************************************************************* Serializer: serialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void mlpserialize(alglib.serializer s, multilayerperceptron network) { int i = 0; int j = 0; int k = 0; int fkind = 0; double threshold = 0; double v0 = 0; double v1 = 0; int nin = 0; int nout = 0; nin = network.hllayersizes[0]; nout = network.hllayersizes[ap.len(network.hllayersizes) - 1]; s.serialize_int(scodes.getmlpserializationcode()); s.serialize_int(mlpfirstversion); s.serialize_bool(mlpissoftmax(network)); apserv.serializeintegerarray(s, network.hllayersizes, -1); for(i = 1; i <= ap.len(network.hllayersizes) - 1; i++) { for(j = 0; j <= network.hllayersizes[i] - 1; j++) { mlpgetneuroninfo(network, i, j, ref fkind, ref threshold); s.serialize_int(fkind); s.serialize_double(threshold); for(k = 0; k <= network.hllayersizes[i - 1] - 1; k++) { s.serialize_double(mlpgetweight(network, i - 1, k, i, j)); } } } for(j = 0; j <= nin - 1; j++) { mlpgetinputscaling(network, j, ref v0, ref v1); s.serialize_double(v0); s.serialize_double(v1); } for(j = 0; j <= nout - 1; j++) { mlpgetoutputscaling(network, j, ref v0, ref v1); s.serialize_double(v0); s.serialize_double(v1); } }
/************************************************************************* Serializer: serialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void kdtreeserialize(alglib.serializer s, kdtree tree) { // // Header // s.serialize_int(scodes.getkdtreeserializationcode()); s.serialize_int(kdtreefirstversion); // // Data // s.serialize_int(tree.n); s.serialize_int(tree.nx); s.serialize_int(tree.ny); s.serialize_int(tree.normtype); apserv.serializerealmatrix(s, tree.xy, -1, -1); apserv.serializeintegerarray(s, tree.tags, -1); apserv.serializerealarray(s, tree.boxmin, -1); apserv.serializerealarray(s, tree.boxmax, -1); apserv.serializeintegerarray(s, tree.nodes, -1); apserv.serializerealarray(s, tree.splits, -1); }
/************************************************************************* Unserialization: complex value *************************************************************************/ public static void unserializerealmatrix(alglib.serializer s, ref double[,] v) { int i = 0; int j = 0; int n0 = 0; int n1 = 0; double t = 0; v = new double[0, 0]; n0 = s.unserialize_int(); n1 = s.unserialize_int(); if(n0 == 0 | n1 == 0) { return; } v = new double[n0, n1]; for(i = 0; i <= n0 - 1; i++) { for(j = 0; j <= n1 - 1; j++) { t = s.unserialize_double(); v[i, j] = t; } } }
/************************************************************************* Allocation of serializer: Integer array *************************************************************************/ public static void allocintegerarray(alglib.serializer s, int[] v, int n) { int i = 0; if(n < 0) { n = ap.len(v); } s.alloc_entry(); for(i = 0; i <= n - 1; i++) { s.alloc_entry(); } }
/************************************************************************* Serialization: Integer array *************************************************************************/ public static void serializeintegerarray(alglib.serializer s, int[] v, int n) { int i = 0; if(n < 0) { n = ap.len(v); } s.serialize_int(n); for(i = 0; i <= n - 1; i++) { s.serialize_int(v[i]); } }
/************************************************************************* Serialization: complex value *************************************************************************/ public static void serializerealarray(alglib.serializer s, double[] v, int n) { int i = 0; if(n < 0) { n = ap.len(v); } s.serialize_int(n); for(i = 0; i <= n - 1; i++) { s.serialize_double(v[i]); } }
/************************************************************************* Unserialization: complex value *************************************************************************/ public static void unserializerealarray(alglib.serializer s, ref double[] v) { int n = 0; int i = 0; double t = 0; v = new double[0]; n = s.unserialize_int(); if(n == 0) { return; } v = new double[n]; for(i = 0; i <= n - 1; i++) { t = s.unserialize_double(); v[i] = t; } }
/************************************************************************* Unserialization: complex value *************************************************************************/ public static complex unserializecomplex(alglib.serializer s) { complex result = 0; result.x = s.unserialize_double(); result.y = s.unserialize_double(); return result; }
/************************************************************************* Serialization: complex value *************************************************************************/ public static void serializecomplex(alglib.serializer s, complex v) { s.serialize_double(v.x); s.serialize_double(v.y); }
/************************************************************************* Allocation of serializer: complex value *************************************************************************/ public static void alloccomplex(alglib.serializer s, complex v) { s.alloc_entry(); s.alloc_entry(); }
/************************************************************************* Serializer: unserialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void dfunserialize(alglib.serializer s, decisionforest forest) { int i0 = 0; int i1 = 0; // // check correctness of header // i0 = s.unserialize_int(); ap.assert(i0 == scodes.getrdfserializationcode(), "DFUnserialize: stream header corrupted"); i1 = s.unserialize_int(); ap.assert(i1 == dffirstversion, "DFUnserialize: stream header corrupted"); // // Unserialize data // forest.nvars = s.unserialize_int(); forest.nclasses = s.unserialize_int(); forest.ntrees = s.unserialize_int(); forest.bufsize = s.unserialize_int(); apserv.unserializerealarray(s, ref forest.trees); }
/************************************************************************* Unserialization: complex value *************************************************************************/ public static void unserializeintegerarray(alglib.serializer s, ref int[] v) { int n = 0; int i = 0; int t = 0; v = new int[0]; n = s.unserialize_int(); if(n == 0) { return; } v = new int[n]; for(i = 0; i <= n - 1; i++) { t = s.unserialize_int(); v[i] = t; } }
/************************************************************************* Serializer: allocation -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void kdtreealloc(alglib.serializer s, kdtree tree) { // // Header // s.alloc_entry(); s.alloc_entry(); // // Data // s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); s.alloc_entry(); apserv.allocrealmatrix(s, tree.xy, -1, -1); apserv.allocintegerarray(s, tree.tags, -1); apserv.allocrealarray(s, tree.boxmin, -1); apserv.allocrealarray(s, tree.boxmax, -1); apserv.allocintegerarray(s, tree.nodes, -1); apserv.allocrealarray(s, tree.splits, -1); }
/************************************************************************* Allocation of serializer: real matrix *************************************************************************/ public static void allocrealmatrix(alglib.serializer s, double[,] v, int n0, int n1) { int i = 0; int j = 0; if(n0 < 0) { n0 = ap.rows(v); } if(n1 < 0) { n1 = ap.cols(v); } s.alloc_entry(); s.alloc_entry(); for(i = 0; i <= n0 - 1; i++) { for(j = 0; j <= n1 - 1; j++) { s.alloc_entry(); } } }
/************************************************************************* Serializer: unserialization -- ALGLIB -- Copyright 14.03.2011 by Bochkanov Sergey *************************************************************************/ public static void kdtreeunserialize(alglib.serializer s, kdtree tree) { int i0 = 0; int i1 = 0; // // check correctness of header // i0 = s.unserialize_int(); ap.assert(i0 == scodes.getkdtreeserializationcode(), "KDTreeUnserialize: stream header corrupted"); i1 = s.unserialize_int(); ap.assert(i1 == kdtreefirstversion, "KDTreeUnserialize: stream header corrupted"); // // Unserialize data // tree.n = s.unserialize_int(); tree.nx = s.unserialize_int(); tree.ny = s.unserialize_int(); tree.normtype = s.unserialize_int(); apserv.unserializerealmatrix(s, ref tree.xy); apserv.unserializeintegerarray(s, ref tree.tags); apserv.unserializerealarray(s, ref tree.boxmin); apserv.unserializerealarray(s, ref tree.boxmax); apserv.unserializeintegerarray(s, ref tree.nodes); apserv.unserializerealarray(s, ref tree.splits); kdtreealloctemporaries(tree, tree.n, tree.nx, tree.ny); }
/************************************************************************* Serialization: complex value *************************************************************************/ public static void serializerealmatrix(alglib.serializer s, double[,] v, int n0, int n1) { int i = 0; int j = 0; if(n0 < 0) { n0 = ap.rows(v); } if(n1 < 0) { n1 = ap.cols(v); } s.serialize_int(n0); s.serialize_int(n1); for(i = 0; i <= n0 - 1; i++) { for(j = 0; j <= n1 - 1; j++) { s.serialize_double(v[i, j]); } } }