[mlpack-git] master: Adjust CNN class so that it works with the mlpack optimizers; Add Train() method regarding the design guidelines. (43f0f13)
gitdub at mlpack.org
gitdub at mlpack.org
Fri Feb 19 08:34:44 EST 2016
Repository : https://github.com/mlpack/mlpack
On branch : master
Link : https://github.com/mlpack/mlpack/compare/f6dd2f7a9752a7db8ec284a938b3e84a13d0bfb2...6205f3e0b62b56452b2a4afc4da24fce5b21e72f
>---------------------------------------------------------------
commit 43f0f13fe84e11709b9d6254ad11827c6eaed80b
Author: marcus <marcus.edel at fu-berlin.de>
Date: Fri Feb 19 14:34:44 2016 +0100
Adjust CNN class so that it works with the mlpack optimizers; Add Train() method regarding the design guidelines.
>---------------------------------------------------------------
43f0f13fe84e11709b9d6254ad11827c6eaed80b
src/mlpack/methods/ann/cnn.hpp | 433 +++++++++++----------
.../methods/ann/{ffn_impl.hpp => cnn_impl.hpp} | 86 ++--
2 files changed, 259 insertions(+), 260 deletions(-)
diff --git a/src/mlpack/methods/ann/cnn.hpp b/src/mlpack/methods/ann/cnn.hpp
index 51215e7..7e6158c 100644
--- a/src/mlpack/methods/ann/cnn.hpp
+++ b/src/mlpack/methods/ann/cnn.hpp
@@ -10,9 +10,11 @@
#include <mlpack/core.hpp>
-#include <mlpack/methods/ann/network_traits.hpp>
+#include <mlpack/methods/ann/network_util.hpp>
#include <mlpack/methods/ann/layer/layer_traits.hpp>
+#include <mlpack/methods/ann/init_rules/nguyen_widrow_init.hpp>
#include <mlpack/methods/ann/performance_functions/cee_function.hpp>
+#include <mlpack/core/optimizers/rmsprop/rmsprop.hpp>
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
@@ -22,122 +24,197 @@ namespace ann /** Artificial Neural Network. */ {
*
* @tparam LayerTypes Contains all layer modules used to construct the network.
* @tparam OutputLayerType The outputlayer type used to evaluate the network.
- * @tparam PerformanceFunction Performance strategy used to claculate the error.
+ * @tparam PerformanceFunction Performance strategy used to calculate the error.
*/
template <
typename LayerTypes,
typename OutputLayerType,
+ typename InitializationRuleType = NguyenWidrowInitialization,
class PerformanceFunction = CrossEntropyErrorFunction<>
>
class CNN
{
public:
+ //! Convenience typedef for the internal model construction.
+ using NetworkType = CNN<LayerTypes,
+ OutputLayerType,
+ InitializationRuleType,
+ PerformanceFunction>;
+
/**
- * Construct the CNN object, which will construct a feed forward neural
- * network with the specified layers.
+ * Create the CNN object with the given predictors and responses set (this is
+ * the set that is used to train the network) and the given optimizer.
+ * Optionally, specify which initialize rule and performance function should
+ * be used.
*
- * @param network The network modules used to construct the network.
- * @param outputLayer The outputlayer used to evaluate the network.
- * @param performanceFunction Performance strategy used to claculate the error.
+ * @param network Network modules used to construct the network.
+ * @param outputLayer Outputlayer used to evaluate the network.
+ * @param predictors Input training variables.
+ * @param responses Outputs resulting from input training variables.
+ * @param optimizer Instantiated optimizer used to train the model.
+ * @param initializeRule Optional instantiated InitializationRule object
+ * for initializing the network paramter.
+ * @param performanceFunction Optional instantiated PerformanceFunction
+ * object used to claculate the error.
*/
- CNN(const LayerTypes& network, OutputLayerType& outputLayer,
- PerformanceFunction performanceFunction = PerformanceFunction())
- : network(network), outputLayer(outputLayer),
- performanceFunction(std::move(performanceFunction)),
- trainError(0)
- {
- // Nothing to do here.
- }
+ template<typename LayerType,
+ typename OutputType,
+ template<typename> class OptimizerType>
+ CNN(LayerType &&network,
+ OutputType &&outputLayer,
+ const arma::cube& predictors,
+ const arma::mat& responses,
+ OptimizerType<NetworkType>& optimizer,
+ InitializationRuleType initializeRule = InitializationRuleType(),
+ PerformanceFunction performanceFunction = PerformanceFunction());
/**
- * Run a single iteration of the feed forward algorithm, using the given
- * input and target vector, store the calculated error into the error
- * parameter.
+ * Create the CNN object with the given predictors and responses set (this is
+ * the set that is used to train the network). Optionally, specify which
+ * initialize rule and performance function should be used.
*
- * @param input Input data used to evaluate the network.
- * @param target Target data used to calculate the network error.
- * @param error The calulated error of the output layer.
+ * @param network Network modules used to construct the network.
+ * @param outputLayer Outputlayer used to evaluate the network.
+ * @param predictors Input training variables.
+ * @param responses Outputs resulting from input training variables.
+ * @param initializeRule Optional instantiated InitializationRule object
+ * for initializing the network paramter.
+ * @param performanceFunction Optional instantiated PerformanceFunction
+ * object used to claculate the error.
*/
- template <typename InputType, typename TargetType, typename ErrorType>
- void FeedForward(const InputType& input,
- const TargetType& target,
- ErrorType& error)
- {
- deterministic = false;
- trainError += Evaluate(input, target, error);
- }
+ template<typename LayerType, typename OutputType>
+ CNN(LayerType &&network,
+ OutputType &&outputLayer,
+ const arma::cube& predictors,
+ const arma::mat& responses,
+ InitializationRuleType initializeRule = InitializationRuleType(),
+ PerformanceFunction performanceFunction = PerformanceFunction());
/**
- * Run a single iteration of the feed backward algorithm, using the given
- * error of the output layer.
+ * Create the CNN object with an empty predictors and responses set and
+ * default optimizer. Make sure to call Train(predictors, responses) when
+ * training.
*
- * @param error The calulated error of the output layer.
+ * @param network Network modules used to construct the network.
+ * @param outputLayer Outputlayer used to evaluate the network.
+ * @param initializeRule Optional instantiated InitializationRule object
+ * for initializing the network paramter.
+ * @param performanceFunction Optional instantiated PerformanceFunction
+ * object used to claculate the error.
*/
- template <typename InputType, typename ErrorType>
- void FeedBackward(const InputType& /* unused */, const ErrorType& error)
- {
- Backward(error, network);
- UpdateGradients<>(network);
- }
+ template<typename LayerType, typename OutputType>
+ CNN(LayerType &&network,
+ OutputType &&outputLayer,
+ InitializationRuleType initializeRule = InitializationRuleType(),
+ PerformanceFunction performanceFunction = PerformanceFunction());
+ /**
+ * Train the convolutional neural network on the given input data. By default, the
+ * RMSprop optimization algorithm is used, but others can be specified
+ * (such as mlpack::optimization::SGD).
+ *
+ * This will use the existing model parameters as a starting point for the
+ * optimization. If this is not what you want, then you should access the
+ * parameters vector directly with Parameters() and modify it as desired.
+ *
+ * @tparam OptimizerType Type of optimizer to use to train the model.
+ * @param predictors Input training variables.
+ * @param responses Outputs results from input training variables.
+ */
+ template<
+ template<typename> class OptimizerType = mlpack::optimization::RMSprop
+ >
+ void Train(const arma::cube& predictors, const arma::mat& responses);
/**
- * Update the weights using the layer defined optimizer.
+ * Train the convolutional neural network with the given instantiated optimizer.
+ * Using this overload allows configuring the instantiated optimizer before
+ * training is performed.
+ *
+ * This will use the existing model parameters as a starting point for the
+ * optimization. If this is not what you want, then you should access the
+ * parameters vector directly with Parameters() and modify it as desired.
+ *
+ * @param optimizer Instantiated optimizer used to train the model.
*/
- void ApplyGradients()
- {
- ApplyGradients<>(network);
+ template<
+ template<typename> class OptimizerType = mlpack::optimization::RMSprop
+ >
+ void Train(OptimizerType<NetworkType>& optimizer);
- // Reset the overall error.
- trainError = 0;
- }
+ /**
+ * Train the convolutional neural network on the given input data using the
+ * given optimizer.
+ *
+ * This will use the existing model parameters as a starting point for the
+ * optimization. If this is not what you want, then you should access the
+ * parameters vector directly with Parameters() and modify it as desired.
+ *
+ * @tparam OptimizerType Type of optimizer to use to train the model.
+ * @param predictors Input training variables.
+ * @param responses Outputs results from input training variables.
+ * @param optimizer Instantiated optimizer used to train the model.
+ */
+ template<
+ template<typename> class OptimizerType = mlpack::optimization::RMSprop
+ >
+ void Train(const arma::cube& predictors,
+ const arma::mat& responses,
+ OptimizerType<NetworkType>& optimizer);
/**
- * Evaluate the network using the given input. The output activation is
- * stored into the output parameter.
+ * Predict the responses to a given set of predictors. The responses will
+ * reflect the output of the given output layer as returned by the
+ * OutputClass() function.
*
- * @param input Input data used to evaluate the network.
- * @param output Output data used to store the output activation
+ * @param predictors Input predictors.
+ * @param responses Matrix to put output predictions of responses into.
*/
- template <typename InputDataType, typename OutputDataType>
- void Predict(const InputDataType& input, OutputDataType& output)
- {
- deterministic = true;
- ResetParameter<>(network);
+ void Predict(arma::cube& predictors, arma::mat& responses);
- Forward(input, network);
- OutputPrediction(output, network);
- }
+ /**
+ * Evaluate the convolutional neural network with the given parameters. This
+ * function is usually called by the optimizer to train the model.
+ *
+ * @param parameters Matrix model parameters.
+ * @param i Index of point to use for objective function evaluation.
+ * @param deterministic Whether or not to train or test the model. Note some
+ * layer act differently in training or testing mode.
+ */
+ double Evaluate(const arma::mat& parameters,
+ const size_t i,
+ const bool deterministic = false);
/**
- * Evaluate the trained network using the given input and compare the output
- * with the given target vector.
+ * Evaluate the gradient of the convolutional neural network with the given
+ * parameters, and with respect to only one point in the dataset. This is
+ * useful for optimizers such as SGD, which require a separable objective
+ * function.
*
- * @param input Input data used to evaluate the trained network.
- * @param target Target data used to calculate the network error.
- * @param error The calulated error of the output layer.
+ * @param parameters Matrix of the model parameters to be optimized.
+ * @param i Index of points to use for objective function gradient evaluation.
+ * @param gradient Matrix to output gradient into.
*/
- template <typename InputType, typename TargetType, typename ErrorType>
- double Evaluate(const InputType& input,
- const TargetType& target,
- ErrorType& error)
- {
- deterministic = false;
- ResetParameter<>(network);
+ void Gradient(const arma::mat& parameters,
+ const size_t i,
+ arma::mat& gradient);
- Forward(input, network);
- return OutputError(target, error, network);
- }
+ //! Return the number of separable functions (the number of predictor points).
+ size_t NumFunctions() const { return numFunctions; }
- //! Get the error of the network.
- double Error() const { return trainError; }
+ //! Return the initial point for the optimization.
+ const arma::mat& Parameters() const { return parameter; }
+ //! Modify the initial point for the optimization.
+ arma::mat& Parameters() { return parameter; }
+
+ /**
+ * Serialize the convolutional neural network.
+ */
+ template<typename Archive>
+ void Serialize(Archive& ar, const unsigned int /* version */);
private:
/**
* Reset the network by setting the layer status.
- *
- * enable_if (SFINAE) is used to iterate through the network. The general
- * case peels off the first type and recurses, as usual with
- * variadic function templates.
*/
template<size_t I = 0, typename... Tp>
typename std::enable_if<I == sizeof...(Tp), void>::type
@@ -145,26 +222,22 @@ class CNN
template<size_t I = 0, typename... Tp>
typename std::enable_if<I < sizeof...(Tp), void>::type
- ResetParameter(std::tuple<Tp...>& t)
+ ResetParameter(std::tuple<Tp...>& network)
{
- ResetDeterministic(std::get<I>(t));
- ResetParameter<I + 1, Tp...>(t);
+ ResetDeterministic(std::get<I>(network));
+ ResetParameter<I + 1, Tp...>(network);
}
/**
* Reset the layer status by setting the current deterministic parameter
* through all layer that implement the Deterministic function.
- *
- * enable_if (SFINAE) is used to iterate through the network. The general
- * case peels off the first type and recurses, as usual with
- * variadic function templates.
*/
template<typename T>
typename std::enable_if<
HasDeterministicCheck<T, bool&(T::*)(void)>::value, void>::type
- ResetDeterministic(T& t)
+ ResetDeterministic(T& layer)
{
- t.Deterministic() = deterministic;
+ layer.Deterministic() = deterministic;
}
template<typename T>
@@ -177,46 +250,37 @@ class CNN
* Run a single iteration of the feed forward algorithm, using the given
* input and target vector, store the calculated error into the error
* vector.
- *
- * enable_if (SFINAE) is used to select between two template overloads of
- * the get function - one for when I is equal the size of the tuple of
- * layer, and one for the general case which peels off the first type
- * and recurses, as usual with variadic function templates.
*/
template<size_t I = 0, typename DataType, typename... Tp>
- void Forward(const DataType& input, std::tuple<Tp...>& t)
+ void Forward(const DataType& input, std::tuple<Tp...>& network)
{
- std::get<I>(t).InputParameter() = input;
+ std::get<I>(network).InputParameter() = input;
- std::get<I>(t).Forward(std::get<I>(t).InputParameter(),
- std::get<I>(t).OutputParameter());
+ std::get<I>(network).Forward(std::get<I>(network).InputParameter(),
+ std::get<I>(network).OutputParameter());
- ForwardTail<I + 1, Tp...>(t);
+ ForwardTail<I + 1, Tp...>(network);
}
template<size_t I = 1, typename... Tp>
typename std::enable_if<I == sizeof...(Tp), void>::type
- ForwardTail(std::tuple<Tp...>& /* unused */)
+ ForwardTail(std::tuple<Tp...>& network)
{
LinkParameter(network);
}
template<size_t I = 1, typename... Tp>
typename std::enable_if<I < sizeof...(Tp), void>::type
- ForwardTail(std::tuple<Tp...>& t)
+ ForwardTail(std::tuple<Tp...>& network)
{
- std::get<I>(t).Forward(std::get<I - 1>(t).OutputParameter(),
- std::get<I>(t).OutputParameter());
+ std::get<I>(network).Forward(std::get<I - 1>(network).OutputParameter(),
+ std::get<I>(network).OutputParameter());
- ForwardTail<I + 1, Tp...>(t);
+ ForwardTail<I + 1, Tp...>(network);
}
/**
* Link the calculated activation with the connection layer.
- *
- * enable_if (SFINAE) is used to iterate through the network. The general
- * case peels off the first type and recurses, as usual with
- * variadic function templates.
*/
template<size_t I = 1, typename... Tp>
typename std::enable_if<I == sizeof...(Tp), void>::type
@@ -224,15 +288,16 @@ class CNN
template<size_t I = 1, typename... Tp>
typename std::enable_if<I < sizeof...(Tp), void>::type
- LinkParameter(std::tuple<Tp...>& t)
+ LinkParameter(std::tuple<Tp...>& network)
{
if (!LayerTraits<typename std::remove_reference<
- decltype(std::get<I>(t))>::type>::IsBiasLayer)
+ decltype(std::get<I>(network))>::type>::IsBiasLayer)
{
- std::get<I>(t).InputParameter() = std::get<I - 1>(t).OutputParameter();
+ std::get<I>(network).InputParameter() = std::get<I - 1>(
+ network).OutputParameter();
}
- LinkParameter<I + 1, Tp...>(t);
+ LinkParameter<I + 1, Tp...>(network);
}
/*
@@ -241,36 +306,31 @@ class CNN
template<typename DataType, typename ErrorType, typename... Tp>
double OutputError(const DataType& target,
ErrorType& error,
- const std::tuple<Tp...>& t)
+ const std::tuple<Tp...>& network)
{
// Calculate and store the output error.
outputLayer.CalculateError(
- std::get<sizeof...(Tp) - 1>(t).OutputParameter(), target, error);
+ std::get<sizeof...(Tp) - 1>(network).OutputParameter(), target, error);
// Masures the network's performance with the specified performance
// function.
- return performanceFunction.Error(network, target, error);
+ return performanceFunc.Error(network, target, error);
}
/**
* Run a single iteration of the feed backward algorithm, using the given
* error of the output layer. Note that we iterate backward through the
* layer modules.
- *
- * enable_if (SFINAE) is used to select between two template overloads of
- * the get function - one for when I is equal the size of the tuple of
- * layer, and one for the general case which peels off the first type
- * and recurses, as usual with variadic function templates.
*/
template<size_t I = 1, typename DataType, typename... Tp>
typename std::enable_if<I < (sizeof...(Tp) - 1), void>::type
- Backward(const DataType& error, std::tuple<Tp...>& t)
+ Backward(const DataType& error, std::tuple<Tp...>& network)
{
- std::get<sizeof...(Tp) - I>(t).Backward(
- std::get<sizeof...(Tp) - I>(t).OutputParameter(), error,
- std::get<sizeof...(Tp) - I>(t).Delta());
+ std::get<sizeof...(Tp) - I>(network).Backward(
+ std::get<sizeof...(Tp) - I>(network).OutputParameter(), error,
+ std::get<sizeof...(Tp) - I>(network).Delta());
- BackwardTail<I + 1, DataType, Tp...>(error, t);
+ BackwardTail<I + 1, DataType, Tp...>(error, network);
}
template<size_t I = 1, typename DataType, typename... Tp>
@@ -280,23 +340,19 @@ class CNN
template<size_t I = 1, typename DataType, typename... Tp>
typename std::enable_if<I < (sizeof...(Tp)), void>::type
- BackwardTail(const DataType& error, std::tuple<Tp...>& t)
+ BackwardTail(const DataType& error, std::tuple<Tp...>& network)
{
- std::get<sizeof...(Tp) - I>(t).Backward(
- std::get<sizeof...(Tp) - I>(t).OutputParameter(),
- std::get<sizeof...(Tp) - I + 1>(t).Delta(),
- std::get<sizeof...(Tp) - I>(t).Delta());
+ std::get<sizeof...(Tp) - I>(network).Backward(
+ std::get<sizeof...(Tp) - I>(network).OutputParameter(),
+ std::get<sizeof...(Tp) - I + 1>(network).Delta(),
+ std::get<sizeof...(Tp) - I>(network).Delta());
- BackwardTail<I + 1, DataType, Tp...>(error, t);
+ BackwardTail<I + 1, DataType, Tp...>(error, network);
}
/**
* Iterate through all layer modules and update the the gradient using the
* layer defined optimizer.
- *
- * enable_if (SFINAE) is used to iterate through the network layer.
- * The general case peels off the first type and recurses, as usual with
- * variadic function templates.
*/
template<
size_t I = 0,
@@ -312,21 +368,20 @@ class CNN
typename... Tp
>
typename std::enable_if<I < Max, void>::type
- UpdateGradients(std::tuple<Tp...>& t)
+ UpdateGradients(std::tuple<Tp...>& network)
{
- Update(std::get<I>(t), std::get<I>(t).OutputParameter(),
- std::get<I + 1>(t).Delta());
+ Update(std::get<I>(network), std::get<I>(network).OutputParameter(),
+ std::get<I + 1>(network).Delta());
- UpdateGradients<I + 1, Max, Tp...>(t);
+ UpdateGradients<I + 1, Max, Tp...>(network);
}
template<typename T, typename P, typename D>
typename std::enable_if<
HasGradientCheck<T, void(T::*)(const D&, P&)>::value, void>::type
- Update(T& t, P& /* unused */, D& delta)
+ Update(T& layer, P& /* unused */, D& delta)
{
- t.Gradient(delta, t.Gradient());
- t.Optimizer().Update();
+ layer.Gradient(delta, layer.Gradient());
}
template<typename T, typename P, typename D>
@@ -337,99 +392,49 @@ class CNN
/* Nothing to do here */
}
- /**
- * Update the weights using the calulated gradients.
- *
- * enable_if (SFINAE) is used to iterate through the network connections.
- * The general case peels off the first type and recurses, as usual with
- * variadic function templates.
- */
- template<
- size_t I = 0,
- size_t Max = std::tuple_size<LayerTypes>::value - 1,
- typename... Tp
- >
- typename std::enable_if<I == Max, void>::type
- ApplyGradients(std::tuple<Tp...>& /* unused */)
- {
- /* Nothing to do here */
- }
-
- template<
- size_t I = 0,
- size_t Max = std::tuple_size<LayerTypes>::value - 1,
- typename... Tp
- >
- typename std::enable_if<I < Max, void>::type
- ApplyGradients(std::tuple<Tp...>& t)
- {
- Apply(std::get<I>(t), std::get<I>(t).OutputParameter(),
- std::get<I + 1>(t).Delta());
-
- ApplyGradients<I + 1, Max, Tp...>(t);
- }
-
- template<typename T, typename P, typename D>
- typename std::enable_if<
- HasGradientCheck<T, void(T::*)(const D&, P&)>::value, void>::type
- Apply(T& t, P& /* unused */, D& /* unused */)
- {
- t.Optimizer().Optimize();
- t.Optimizer().Reset();
- }
-
- template<typename T, typename P, typename D>
- typename std::enable_if<
- !HasGradientCheck<T, void(T::*)(const P&, D&)>::value, void>::type
- Apply(T& /* unused */, P& /* unused */, D& /* unused */)
- {
- /* Nothing to do here */
- }
-
/*
* Calculate and store the output activation.
*/
template<typename DataType, typename... Tp>
- void OutputPrediction(DataType& output, std::tuple<Tp...>& t)
+ void OutputPrediction(DataType& output, std::tuple<Tp...>& network)
{
// Calculate and store the output prediction.
- outputLayer.OutputClass(std::get<sizeof...(Tp) - 1>(t).OutputParameter(),
- output);
+ outputLayer.OutputClass(std::get<sizeof...(Tp) - 1>(
+ network).OutputParameter(), output);
}
- //! The layer modules used to build the network.
+ //! Instantiated convolutional neural network.
LayerTypes network;
//! The outputlayer used to evaluate the network
OutputLayerType& outputLayer;
- //! The class used to evaluate the performance of the network
- PerformanceFunction performanceFunction;
-
- //! The current training error of the network.
- double trainError;
+ //! Performance strategy used to claculate the error.
+ PerformanceFunction performanceFunc;
//! The current evaluation mode (training or testing).
bool deterministic;
-}; // class CNN
-//! Network traits for the CNN network.
-template <
- typename LayerTypes,
- typename OutputLayerType,
- class PerformanceFunction
->
-class NetworkTraits<
- CNN<LayerTypes, OutputLayerType, PerformanceFunction> >
-{
- public:
- static const bool IsFNN = false;
- static const bool IsRNN = false;
- static const bool IsCNN = true;
- static const bool IsSAE = false;
-};
+ //! Matrix of (trained) parameters.
+ arma::mat parameter;
+
+ //! The matrix of data points (predictors).
+ arma::cube predictors;
+
+ //! The matrix of responses to the input data points.
+ arma::mat responses;
+
+ //! The number of separable functions (the number of predictor points).
+ size_t numFunctions;
+
+ //! Locally stored backward error.
+ arma::mat error;
+}; // class CNN
} // namespace ann
} // namespace mlpack
+// Include implementation.
+#include "cnn_impl.hpp"
+
#endif
diff --git a/src/mlpack/methods/ann/ffn_impl.hpp b/src/mlpack/methods/ann/cnn_impl.hpp
similarity index 79%
copy from src/mlpack/methods/ann/ffn_impl.hpp
copy to src/mlpack/methods/ann/cnn_impl.hpp
index bd7436a..3b568c4 100644
--- a/src/mlpack/methods/ann/ffn_impl.hpp
+++ b/src/mlpack/methods/ann/cnn_impl.hpp
@@ -1,14 +1,14 @@
/**
- * @file ffn_impl.hpp
+ * @file cnn_impl.hpp
* @author Marcus Edel
*
- * Definition of the FFN class, which implements feed forward neural networks.
+ * Definition of the CNN class, which implements convolutional neural networks.
*/
-#ifndef __MLPACK_METHODS_ANN_FFN_IMPL_HPP
-#define __MLPACK_METHODS_ANN_FFN_IMPL_HPP
+#ifndef __MLPACK_METHODS_ANN_CNN_IMPL_HPP
+#define __MLPACK_METHODS_ANN_CNN_IMPL_HPP
// In case it hasn't been included yet.
-#include "ffn.hpp"
+#include "cnn.hpp"
namespace mlpack {
namespace ann /** Artificial Neural Network. */ {
@@ -23,10 +23,10 @@ template<typename LayerType,
typename OutputType,
template<typename> class OptimizerType
>
-FFN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::FFN(LayerType &&network,
+CNN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
+>::CNN(LayerType &&network,
OutputType &&outputLayer,
- const arma::mat& predictors,
+ const arma::cube& predictors,
const arma::mat& responses,
OptimizerType<NetworkType>& optimizer,
InitializationRuleType initializeRule,
@@ -50,11 +50,11 @@ FFN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
NetworkWeights(parameter, network);
// Train the model.
- Timer::Start("ffn_optimization");
+ Timer::Start("cnn_optimization");
const double out = optimizer.Optimize(parameter);
- Timer::Stop("ffn_optimization");
+ Timer::Stop("cnn_optimization");
- Log::Info << "FFN::FFN(): final objective of trained model is " << out
+ Log::Info << "CNN::CNN(): final objective of trained model is " << out
<< "." << std::endl;
}
@@ -64,10 +64,10 @@ template<typename LayerTypes,
typename PerformanceFunction
>
template<typename LayerType, typename OutputType>
-FFN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::FFN(LayerType &&network,
+CNN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
+>::CNN(LayerType &&network,
OutputType &&outputLayer,
- const arma::mat& predictors,
+ const arma::cube& predictors,
const arma::mat& responses,
InitializationRuleType initializeRule,
PerformanceFunction performanceFunction) :
@@ -95,8 +95,8 @@ template<typename LayerTypes,
typename PerformanceFunction
>
template<typename LayerType, typename OutputType>
-FFN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::FFN(LayerType &&network,
+CNN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
+>::CNN(LayerType &&network,
OutputType &&outputLayer,
InitializationRuleType initializeRule,
PerformanceFunction performanceFunction) :
@@ -114,8 +114,6 @@ FFN<LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
initializeRule.Initialize(parameter, NetworkSize(network), 1);
NetworkWeights(parameter, network);
-
- Log::Debug << parameter << std::endl;
}
template<typename LayerTypes,
@@ -124,9 +122,9 @@ template<typename LayerTypes,
typename PerformanceFunction
>
template<template<typename> class OptimizerType>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::Train(const arma::mat& predictors, const arma::mat& responses)
+>::Train(const arma::cube& predictors, const arma::mat& responses)
{
numFunctions = predictors.n_cols;
this->predictors = predictors;
@@ -135,11 +133,11 @@ LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
OptimizerType<decltype(*this)> optimizer(*this);
// Train the model.
- Timer::Start("ffn_optimization");
+ Timer::Start("cnn_optimization");
const double out = optimizer.Optimize(parameter);
- Timer::Stop("ffn_optimization");
+ Timer::Stop("cnn_optimization");
- Log::Info << "FFN::FFN(): final objective of trained model is " << out
+ Log::Info << "CNN::CNN(): final objective of trained model is " << out
<< "." << std::endl;
}
@@ -149,9 +147,9 @@ template<typename LayerTypes,
typename PerformanceFunction
>
template<template<typename> class OptimizerType>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::Train(const arma::mat& predictors,
+>::Train(const arma::cube& predictors,
const arma::mat& responses,
OptimizerType<NetworkType>& optimizer)
{
@@ -160,11 +158,11 @@ LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
this->responses = responses;
// Train the model.
- Timer::Start("ffn_optimization");
+ Timer::Start("cnn_optimization");
const double out = optimizer.Optimize(parameter);
- Timer::Stop("ffn_optimization");
+ Timer::Stop("cnn_optimization");
- Log::Info << "FFN::FFN(): final objective of trained model is " << out
+ Log::Info << "CNN::CNN(): final objective of trained model is " << out
<< "." << std::endl;
}
@@ -176,16 +174,16 @@ template<typename LayerTypes,
template<
template<typename> class OptimizerType
>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Train(OptimizerType<NetworkType>& optimizer)
{
// Train the model.
- Timer::Start("ffn_optimization");
+ Timer::Start("cnn_optimization");
const double out = optimizer.Optimize(parameter);
- Timer::Stop("ffn_optimization");
+ Timer::Stop("cnn_optimization");
- Log::Info << "FFN::FFN(): final objective of trained model is " << out
+ Log::Info << "CNN::CNN(): final objective of trained model is " << out
<< "." << std::endl;
}
@@ -194,25 +192,23 @@ template<typename LayerTypes,
typename InitializationRuleType,
typename PerformanceFunction
>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
->::Predict(arma::mat& predictors, arma::mat& responses)
+>::Predict(arma::cube& predictors, arma::mat& responses)
{
deterministic = true;
arma::mat responsesTemp;
ResetParameter(network);
- Forward(arma::mat(predictors.colptr(0), predictors.n_rows, 1, false, true),
- network);
+ Forward(predictors.slices(0, 0), network);
OutputPrediction(responsesTemp, network);
- responses = arma::mat(responsesTemp.n_elem, predictors.n_cols);
+ responses = arma::mat(responsesTemp.n_elem, predictors.n_slices);
responses.col(0) = responsesTemp.col(0);
- for (size_t i = 1; i < predictors.n_cols; i++)
+ for (size_t i = 1; i < predictors.n_slices; i++)
{
- Forward(arma::mat(predictors.colptr(i), predictors.n_rows, 1, false, true),
- network);
+ Forward(predictors.slices(i, i), network);
responsesTemp = arma::mat(responses.colptr(i), responses.n_rows, 1, false,
true);
@@ -226,7 +222,7 @@ template<typename LayerTypes,
typename InitializationRuleType,
typename PerformanceFunction
>
-double FFN<
+double CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Evaluate(const arma::mat& /* unused */,
const size_t i,
@@ -235,9 +231,7 @@ LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
this->deterministic = deterministic;
ResetParameter(network);
-
- Forward(arma::mat(predictors.colptr(i), predictors.n_rows, 1, false, true),
- network);
+ Forward(predictors.slices(i, i), network);
return OutputError(arma::mat(responses.colptr(i), responses.n_rows, 1, false,
true), error, network);
@@ -248,7 +242,7 @@ template<typename LayerTypes,
typename InitializationRuleType,
typename PerformanceFunction
>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Gradient(const arma::mat& /* unused */,
const size_t /* unused */,
@@ -266,7 +260,7 @@ template<typename LayerTypes,
typename PerformanceFunction
>
template<typename Archive>
-void FFN<
+void CNN<
LayerTypes, OutputLayerType, InitializationRuleType, PerformanceFunction
>::Serialize(Archive& ar, const unsigned int /* version */)
{
More information about the mlpack-git
mailing list