Skip to content

Commit

Permalink
made CNeuralNetwork inherit from CMachine
Browse files Browse the repository at this point in the history
  • Loading branch information
khalednasr committed Mar 23, 2014
1 parent c58e077 commit 80b9ce5
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 170 deletions.
75 changes: 47 additions & 28 deletions doc/ipython-notebooks/neuralnets/neuralnets_digits.ipynb
Expand Up @@ -28,26 +28,23 @@
"input": [
"import numpy as np\n",
"from scipy.io import loadmat\n",
"from modshogun import NeuralNetwork\n",
"from modshogun import NeuralLogisticLayer\n",
"from modshogun import DynamicObjectArray\n",
"from modshogun import RealFeatures\n",
"from modshogun import NeuralNetwork, NeuralLogisticLayer\n",
"from modshogun import DynamicObjectArray, RealFeatures\n",
"from modshogun import MulticlassLabels, MulticlassAccuracy\n",
"\n",
"# load the dataset\n",
"dataset = loadmat('../../../data/multiclass/usps.mat')\n",
"\n",
"Xall = dataset['data']\n",
"# the usps dataset has the digits labeled from 1 to 10 \n",
"# we'll subtract 1 to make them in the 0-9 range instead\n",
"Yall = dataset['label']-1 \n",
"\n",
"# the neural network will have 10 neurons in its output layer, one for each digit\n",
"# therefore we need to give it the label for each example needs to be a vector of 10 elements\n",
"Yall_expanded = np.eye(10)[:,np.squeeze(Yall)]\n",
"Yall = np.array(dataset['label'].squeeze(), dtype=np.double)-1 \n",
"\n",
"# use the first 5000 examples for training, the rest will be used for testing\n",
"Xtrain = Xall[:,0:5000]\n",
"Ytrain = Yall_expanded[:,0:5000]\n",
"Xtrain = RealFeatures(Xall[:,0:5000])\n",
"Ytrain = MulticlassLabels(Yall[0:5000])\n",
"Xtest = RealFeatures(Xall[:,5001:-1])\n",
"Ytest = MulticlassLabels(Yall[5001:-1])\n",
"\n",
"# setup the network's layers\n",
"layers = DynamicObjectArray()\n",
Expand All @@ -61,24 +58,21 @@
"# turn on regularization to reduce overfitting\n",
"net.set_L2_regularization(0.001)\n",
"\n",
"# train the network, the error each iteration is printed to the console\n",
"net.train_gradient_descent(RealFeatures(Xtrain), \n",
" RealFeatures(Ytrain),\n",
" 300, # number of iterations over the training set\n",
" 1000); # mini-batch size\n",
"\n",
"# prepere the test set\n",
"Xtest = Xall[:,5001:-1]\n",
"Ytest = Yall[:,5001:-1]\n",
"# set training parameters\n",
"net.learning_rate = 0.5\n",
"net.mini_batch_size = 1000\n",
"net.max_num_epochs = 300\n",
"\n",
"# apply the network to the test inputs\n",
"predictions = net.apply(RealFeatures(Xtest)).get_feature_matrix()\n",
"predictions = np.argmax(predictions, axis=0)\n",
"# train the network\n",
"net.set_labels(Ytrain)\n",
"net.train(Xtrain)\n",
"\n",
"# measure the test error\n",
"test_error = float(np.sum(predictions!=Ytest))/Ytest.shape[1] * 100\n",
"# test the network\n",
"predictions = net.apply_multiclass(Xtest)\n",
"evaluator = MulticlassAccuracy()\n",
"accuracy = evaluator.evaluate(predictions, Ytest)\n",
"\n",
"print \"Test Error =\", test_error, \"%\""
"print \"Accuracy on the test set =\", accuracy * 100"
],
"language": "python",
"metadata": {},
Expand All @@ -87,11 +81,36 @@
"output_type": "stream",
"stream": "stdout",
"text": [
"Test Error = 8.54283054004 %\n"
"Training the network..\n",
"Accuracy on the test set ="
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
" 91.5037243948\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "pyerr",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-4-ed28a4285fc2>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Training the network..\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 40\u001b[0m \u001b[0mnet\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mset_labels\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mYtrain\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 41\u001b[0;31m \u001b[0mnet\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mXtrain\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 42\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 43\u001b[0m \u001b[0;31m# test the network\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Training the network..\n"
]
}
],
"prompt_number": 1
"prompt_number": 4
},
{
"cell_type": "code",
Expand Down
39 changes: 32 additions & 7 deletions src/interfaces/modular/NeuralNets.i
@@ -1,13 +1,38 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) Khaled Nasr
* Copyright (c) 2014, Shogun Toolbox Foundation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2014 Khaled Nasr
*/

%newobject apply();
%newobject apply(CFeatures* data);
%newobject apply_multiclass(CFeatures* data);

/* Remove C Prefix */
%rename(NeuralNetwork) CNeuralNetwork;
Expand Down
1 change: 1 addition & 0 deletions src/shogun/machine/Machine.h
Expand Up @@ -85,6 +85,7 @@ enum EMachineType
CT_GAUSSIANPROCESSBINARY = 530,
CT_GAUSSIANPROCESSMULTICLASS = 540,
CT_STOCHASTICSOSVM = 550,
CT_NEURALNETWORK = 560,
CT_BAGGING
};

Expand Down
34 changes: 29 additions & 5 deletions src/shogun/neuralnets/NeuralLayer.cpp
@@ -1,9 +1,33 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (c) 2014, Shogun Toolbox Foundation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2014 Khaled Nasr
*/

Expand Down
34 changes: 29 additions & 5 deletions src/shogun/neuralnets/NeuralLayer.h
@@ -1,9 +1,33 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (c) 2014, Shogun Toolbox Foundation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2014 Khaled Nasr
*/

Expand Down
60 changes: 45 additions & 15 deletions src/shogun/neuralnets/NeuralLinearLayer.cpp
@@ -1,9 +1,33 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (c) 2014, Shogun Toolbox Foundation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2014 Khaled Nasr
*/

Expand Down Expand Up @@ -57,12 +81,15 @@ void CNeuralLinearLayer::compute_activations(float64_t* parameters,
float64_t* biases = parameters + m_num_neurons*m_previous_layer_num_neurons;

#ifdef HAVE_EIGEN3
Eigen::Map<Eigen::MatrixXd> W(weights, m_num_neurons,
typedef Eigen::Map<Eigen::MatrixXd> EMappedMatrix;
typedef Eigen::Map<Eigen::VectorXd> EMappedVector;

EMappedMatrix W(weights, m_num_neurons,
m_previous_layer_num_neurons);
Eigen::Map<Eigen::MatrixXd> X(previous_layer_activations,
EMappedMatrix X(previous_layer_activations,
m_previous_layer_num_neurons, m_batch_size);
Eigen::Map<Eigen::MatrixXd> A(m_activations, m_num_neurons, m_batch_size);
Eigen::Map<Eigen::VectorXd> B(biases, m_num_neurons);
EMappedMatrix A(m_activations, m_num_neurons, m_batch_size);
EMappedVector B(biases, m_num_neurons);

A = W*X;
A.colwise() += B;
Expand All @@ -79,17 +106,20 @@ void CNeuralLinearLayer::compute_gradients(float64_t* parameters,
compute_local_gradients(is_output, p);

#ifdef HAVE_EIGEN3
Eigen::Map<Eigen::MatrixXd> X(previous_layer_activations,
typedef Eigen::Map<Eigen::MatrixXd> EMappedMatrix;
typedef Eigen::Map<Eigen::VectorXd> EMappedVector;

EMappedMatrix X(previous_layer_activations,
m_previous_layer_num_neurons, m_batch_size);
Eigen::Map<Eigen::MatrixXd> W(weights, m_num_neurons,
EMappedMatrix W(weights, m_num_neurons,
m_previous_layer_num_neurons);
Eigen::Map<Eigen::MatrixXd> LG(m_local_gradients, m_num_neurons,
EMappedMatrix LG(m_local_gradients, m_num_neurons,
m_batch_size);
Eigen::Map<Eigen::MatrixXd> WG(parameter_gradients,
EMappedMatrix WG(parameter_gradients,
m_num_neurons, m_previous_layer_num_neurons);
Eigen::Map<Eigen::VectorXd> BG(parameter_gradients +
EMappedVector BG(parameter_gradients +
m_num_neurons*m_previous_layer_num_neurons, m_num_neurons);
Eigen::Map<Eigen::MatrixXd> IG(m_input_gradients,
EMappedMatrix IG(m_input_gradients,
m_previous_layer_num_neurons, m_batch_size);

// compute parameter gradients
Expand Down
34 changes: 29 additions & 5 deletions src/shogun/neuralnets/NeuralLinearLayer.h
@@ -1,9 +1,33 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Copyright (c) 2014, Shogun Toolbox Foundation
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from this
* software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* Written (W) 2014 Khaled Nasr
*/

Expand Down

0 comments on commit 80b9ce5

Please sign in to comment.