Commit 99ff1a38 authored by mzed's avatar mzed
Browse files

numHiddenNodes working in C++

parent b2884c15
......@@ -89,6 +89,9 @@ int main(int argc, const char * argv[]) {
////////////////////////////////////////////////////////////////////////////////
regression myNN;
regression myNN_nodes;
myNN_nodes.setNumHiddenNodes(10);
assert(myNN_nodes.getNumHiddenNodes()[0] == 10);
classification myKnn;
//classification mySVM(classification::svm);
......@@ -103,6 +106,7 @@ int main(int argc, const char * argv[]) {
trainingSet.push_back(tempExample);
myNN.train(trainingSet);
myNN_nodes.train(trainingSet);
// std::cout << myNN.getJSON() << std::endl;
std::string filepath = "/var/tmp/modelSetDescription.json";
myNN.writeJSON(filepath);
......@@ -121,6 +125,7 @@ int main(int argc, const char * argv[]) {
//std::cout << "from file: " << myNNfromFile.run(inputVec)[0] << std::endl;
assert(myNN.run(inputVec)[0] == 20.14);
assert(myNN_nodes.run(inputVec)[0] == 20.14);
//assert(myNN.run(inputVec)[0] == myNNfromString.run(inputVec)[0]);
//assert(myNN.run(inputVec)[0] == myNNfromFile.run(inputVec)[0]);
......
......@@ -210,6 +210,13 @@ int neuralNetwork<T>::getNumHiddenNodes() const {
return numHiddenNodes;
}
template<typename T>
void neuralNetwork<T>::setNumHiddenNodes(int num_hidden_nodes) {
numHiddenNodes = num_hidden_nodes;
reset();
initTrainer();
}
template<typename T>
void neuralNetwork<T>::setEpochs(const int &epochs) {
numEpochs = epochs;
......@@ -445,4 +452,4 @@ void neuralNetwork<T>::updateWeights() {
//explicit instantiation
template class neuralNetwork<double>;
template class neuralNetwork<float>;
\ No newline at end of file
template class neuralNetwork<float>;
......@@ -71,6 +71,7 @@ public:
void setNumHiddenLayers(int num_hidden_layers);
int getNumHiddenNodes() const;
void setNumHiddenNodes(int num_hidden_nodes);
void setEpochs(const int &epochs);
......
......@@ -22,6 +22,7 @@ regressionTemplate<T>::regressionTemplate() {
modelSet<T>::numInputs = -1;
modelSet<T>::numOutputs = -1;
numHiddenLayers = 1;
numHiddenNodes = 0; //this will be changed by training
numEpochs = 500;
modelSet<T>::created = false;
};
......@@ -32,13 +33,14 @@ regressionTemplate<T>::regressionTemplate(const int &num_inputs, const int &num_
modelSet<T>::numOutputs = num_outputs;
numHiddenLayers = 1;
numEpochs = 500;
numHiddenNodes = num_inputs;
modelSet<T>::created = false;
std::vector<int> whichInputs;
for (int i = 0; i < modelSet<T>::numInputs; ++i) {
whichInputs.push_back(i);
}
for (int i = 0; i < modelSet<T>::numOutputs; ++i) {
modelSet<T>::myModelSet.push_back(new neuralNetwork<T>(modelSet<T>::numInputs, whichInputs, numHiddenLayers, modelSet<T>::numInputs));
modelSet<T>::myModelSet.push_back(new neuralNetwork<T>(modelSet<T>::numInputs, whichInputs, numHiddenLayers, numHiddenNodes));
}
modelSet<T>::created = true;
};
......@@ -77,6 +79,32 @@ void regressionTemplate<T>::setNumHiddenLayers(const int &num_hidden_layers){
}
}
template<typename T>
std::vector<int> regressionTemplate<T>::getNumHiddenNodes() {
std::vector<int> vecNumHiddenNodes;
if (std::begin(modelSet<T>::myModelSet) != std::end(modelSet<T>::myModelSet)) {
for (baseModel<T>* model : modelSet<T>::myModelSet) {
neuralNetwork<T>* nnModel = dynamic_cast<neuralNetwork<T>*>(model); //FIXME: I really dislike this design
vecNumHiddenNodes.push_back(nnModel->getNumHiddenNodes());
}
} else {
vecNumHiddenNodes = { numHiddenNodes };
}
return vecNumHiddenNodes;
}
template<typename T>
void regressionTemplate<T>::setNumHiddenNodes(const int &num_hidden_nodes){
numHiddenNodes = num_hidden_nodes;
//Set any existing models
if (std::begin(modelSet<T>::myModelSet) != std::end(modelSet<T>::myModelSet)) {
for (baseModel<T>* model : modelSet<T>::myModelSet) {
neuralNetwork<T>* nnModel = dynamic_cast<neuralNetwork<T>*>(model); //FIXME: I really dislike this design
nnModel->setNumHiddenNodes(num_hidden_nodes);
}
}
}
template<typename T>
void regressionTemplate<T>::setNumEpochs(const int &epochs) {
numEpochs = epochs;
......@@ -112,12 +140,17 @@ bool regressionTemplate<T>::train(const std::vector<trainingExampleTemplate<T> >
return false;
}
}
if(numHiddenNodes == 0) { //not yet set
numHiddenNodes = modelSet<T>::numInputs;
}
std::vector<int> whichInputs;
for (int j = 0; j < modelSet<T>::numInputs; ++j) {
whichInputs.push_back(j);
}
std::cout << "numHiddenNodes: " << numHiddenNodes << std::endl;
std::cout << "modelSet<T>::numInputs: " << modelSet<T>::numInputs << std::endl;
for (int i = 0; i < modelSet<T>::numOutputs; ++i) {
modelSet<T>::myModelSet.push_back(new neuralNetwork<T>(modelSet<T>::numInputs, whichInputs, numHiddenLayers, modelSet<T>::numInputs));
modelSet<T>::myModelSet.push_back(new neuralNetwork<T>(modelSet<T>::numInputs, whichInputs, numHiddenLayers, numHiddenNodes));
}
if (numEpochs != 500) {
for (baseModel<T>* model : modelSet<T>::myModelSet) {
......
......@@ -43,9 +43,16 @@ public:
/** Set how many hidden layers are in all models. This feature is temporary, and will be replaced by a different design. */
void setNumHiddenLayers(const int &num_hidden_layers);
/** Check how many hidden nodes are in each model. This feature is temporary, and will be replaced by a different design. */
std::vector<int> getNumHiddenNodes();
/** Set how many hidden layers are in all models. This feature is temporary, and will be replaced by a different design. */
void setNumHiddenNodes(const int &num_hidden_nodes);
private:
int numHiddenLayers; //Temporary -- this should be part of the nn class. -mz
int numEpochs; //Temporary -- also should be part of nn only. -mz
int numHiddenNodes; //Temporary -- also should be part of nn only. -mz
};
//This is here so the old API still works
......
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