Created
May 3, 2015 18:25
-
-
Save sanuj/a9c5a4ec3fe2fb8af5b8 to your computer and use it in GitHub Desktop.
neural nets basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <shogun/base/init.h> | |
#include <iostream> | |
#include <shogun/io/SGIO.h> | |
#include <shogun/lib/common.h> | |
#include <shogun/mathematics/Math.h> | |
#include <shogun/features/DataGenerator.h> | |
#include <shogun/features/DenseFeatures.h> | |
#include <shogun/labels/MulticlassLabels.h> | |
#include <shogun/evaluation/MulticlassAccuracy.h> | |
#include <shogun/neuralnets/NeuralNetwork.h> | |
#include <shogun/neuralnets/NeuralLayers.h> | |
using namespace shogun; | |
void print_message(FILE* target, const char* str) | |
{ | |
fprintf(target, "%s", str); | |
} | |
void print_warning(FILE* target, const char* str) | |
{ | |
fprintf(target, "%s", str); | |
} | |
void print_error(FILE* target, const char* str) | |
{ | |
fprintf(target, "%s", str); | |
} | |
int main(int, char*[]) | |
{ | |
init_shogun(&print_message, &print_warning, | |
&print_error); | |
// initialize the random number generator with a fixed seed, for repeatability | |
CMath::init_random(10); | |
// Prepare the training data | |
const int num_classes = 4; | |
const int num_features = 10; | |
const int num_examples_per_class = 20; | |
SGMatrix<float64_t> X; | |
SGVector<float64_t> Y; | |
try | |
{ | |
X = CDataGenerator::generate_gaussians( | |
num_examples_per_class,num_classes,num_features); | |
Y = SGVector<float64_t>(num_classes*num_examples_per_class); | |
} | |
catch (ShogunException e) | |
{ | |
// out of memory | |
SG_SPRINT(e.get_exception_string()); | |
std::cout << "Error occured." << std::endl; | |
return 0; | |
} | |
std::cout << "Features, parameters set." << std::endl; | |
for (int32_t i = 0; i < num_classes; i++) | |
for (int32_t j = 0; j < num_examples_per_class; j++) | |
Y[i*num_examples_per_class + j] = i; | |
CDenseFeatures<float64_t>* features = new CDenseFeatures<float64_t>(X); | |
CMulticlassLabels* labels = new CMulticlassLabels(Y); | |
// Create a small network single hidden layer network | |
CNeuralLayers* layers = new CNeuralLayers(); | |
layers->input(num_features)->rectified_linear(10)->softmax(num_classes); | |
CNeuralNetwork* network = new CNeuralNetwork(layers->done()); | |
// initialize the network | |
network->quick_connect(); | |
network->initialize(); | |
// uncomment this line to enable info logging | |
// network->io->set_loglevel(MSG_INFO); | |
// train using default parameters | |
network->set_labels(labels); | |
network->train(features); | |
std::cout << "Training done." << std::endl; | |
// evaluate | |
CMulticlassLabels* predictions = network->apply_multiclass(features); | |
CMulticlassAccuracy* evaluator = new CMulticlassAccuracy(); | |
float64_t accuracy = evaluator->evaluate(predictions, labels); | |
SG_SINFO("Accuracy = %f %\n", accuracy*100); | |
// Clean up | |
SG_UNREF(network); | |
SG_UNREF(layers); | |
SG_UNREF(features); | |
SG_UNREF(predictions); | |
SG_UNREF(evaluator); | |
exit_shogun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment