Commit 267f5dd3 authored by mzed's avatar mzed
Browse files

knn serialization is almost working

parent 0427ef82
......@@ -36,5 +36,20 @@ int main(int argc, const char * argv[]) {
std::cout << "from string: " << myNNfromString.process(inputVec)[0] << std::endl;
std::cout << "from file: " << myNNfromFile.process(inputVec)[0] << std::endl;
///////////////////////////
myKnn.train(trainingSet);
std::cout << myKnn.getJSON() << std::endl;
std::string filepath2 = "/var/tmp/modelSetDescription_knn.json";
myKnn.writeJSON(filepath2);
classification myKnnFromString;
myKnnFromString.putJSON(myKnn.getJSON());
std::cout << "before: " << myKnn.process(inputVec)[0] << std::endl;
std::cout << "from string: " << myKnnFromString.process(inputVec)[0] << std::endl;
//std::cout << "from file: " << myKnnfromFile.process(inputVec)[0] << std::endl;
return 0;
}
......@@ -110,8 +110,9 @@ double knnClassification::process(std::vector<double> inputVector) {
#ifndef EMSCRIPTEN
Json::Value knnClassification::getJSONDescription() {
Json::Value jsonModelDescription;
jsonModelDescription["modelType"] = "kNN Classificiation";
jsonModelDescription["modelType"] = "kNN Classificiation";
jsonModelDescription["numInputs"] = numInputs;
jsonModelDescription["whichInputs"] = vector2json(whichInputs);
jsonModelDescription["k"] = numNeighbours;
Json::Value examples;
for (std::vector<trainingExample>::iterator it = neighbours.begin(); it != neighbours.end(); ++it) {
......
......@@ -135,16 +135,31 @@ void modelSet::json2modelSet(Json::Value root) {
for (int i = 0; i < model["whichInputs"].size(); ++i) { //TODO: factor these
whichInputs.push_back(model["whichInputs"][i].asDouble());
}
int numHiddenLayers = model["numHiddenLayers"].asInt();
int numHiddenNodes = model["numHiddenNodes"].asInt();
std::vector<double> weights = json2vector(model["weights"]);
std::vector<double> wHiddenOutput = json2vector(model["wHiddenOutput"]);
std::vector<double> inBases = json2vector(model["inBases"]);
std::vector<double> inRanges = json2vector(model["inRanges"]);
double outRange = model["outRange"].asDouble();
double outBase = model["outBase"].asDouble();
myModelSet.push_back(new neuralNetwork(modelNumInputs, whichInputs, numHiddenLayers, numHiddenNodes, weights, wHiddenOutput, inRanges, inBases, outRange, outBase));
if (model["modelType"].asString() == "Neural Network") {
int numHiddenLayers = model["numHiddenLayers"].asInt();
int numHiddenNodes = model["numHiddenNodes"].asInt();
std::vector<double> weights = json2vector(model["weights"]);
std::vector<double> wHiddenOutput = json2vector(model["wHiddenOutput"]);
std::vector<double> inBases = json2vector(model["inBases"]);
std::vector<double> inRanges = json2vector(model["inRanges"]);
double outRange = model["outRange"].asDouble();
double outBase = model["outBase"].asDouble();
myModelSet.push_back(new neuralNetwork(modelNumInputs, whichInputs, numHiddenLayers, numHiddenNodes, weights, wHiddenOutput, inRanges, inBases, outRange, outBase));
} else if (model["modelType"].asString() == "kNN Classification") {
std::vector<trainingExample> trainingSet;
const Json::Value examples = model["examples"];
for (int i = 0; i < examples.size(); ++i) {
trainingExample tempExample;
tempExample.input = json2vector(examples[i]["features"]);
tempExample.output = json2vector(examples[i]["class"]);
trainingSet.push_back(tempExample);
}
int k = model["k"].asInt();
myModelSet.push_back(new knnClassification(modelNumInputs, whichInputs, trainingSet, k));
}
}
created = true;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment