From 407b563330cd1f81020f85198f14d7a3a4e5cccd Mon Sep 17 00:00:00 2001 From: Omar Shrit Date: Thu, 2 Dec 2021 22:54:06 +0000 Subject: [PATCH] Clean all using namespaces from the headers. Signed-off-by: Omar Shrit --- src/mlpack/methods/ann/layer_names.hpp | 441 ++++++++++++++++++ .../q_networks/categorical_dqn.hpp | 30 +- .../q_networks/dueling_dqn.hpp | 74 ++- .../q_networks/simple_dqn.hpp | 26 +- 4 files changed, 503 insertions(+), 68 deletions(-) create mode 100644 src/mlpack/methods/ann/layer_names.hpp diff --git a/src/mlpack/methods/ann/layer_names.hpp b/src/mlpack/methods/ann/layer_names.hpp new file mode 100644 index 0000000000..b55deebbed --- /dev/null +++ b/src/mlpack/methods/ann/layer_names.hpp @@ -0,0 +1,441 @@ +/** + * @file methods/ann/layer_names.hpp + * @author Sreenik Seal + * + * Implementation of a class that converts a given ann layer to string format. + * + * mlpack is free software; you may redistribute it and/or modify it under the + * terms of the 3-clause BSD license. You should have received a copy of the + * 3-clause BSD license along with mlpack. If not, see + * http://www.opensource.org/licenses/BSD-3-Clause for more information. + */ + +#include +#include +#include +#include +#include + +/** + * Implementation of a class that returns the string representation of the + * name of the given layer. + */ +class LayerNameVisitor : public boost::static_visitor +{ + public: + //! Create the LayerNameVisitor object. + LayerNameVisitor() + { + } + + /** + * Return the name of the given layer of type AdaptiveMaxPooling as string. + * + * @param * Given layer of type AdaptiveMaxPooling. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::AdaptiveMaxPooling<> * /*layer*/) const + { + return "adaptivemaxpooling"; + } + + /** + * Return the name of the given layer of type AdaptiveMeanPooling as string. + * + * @param * Given layer of type AdaptiveMeanPooling. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::AdaptiveMeanPooling<> * /*layer*/) const + { + return "adaptivemeanpooling"; + } + + /** + * Return the name of the given layer of type AtrousConvolution as a string. + * + * @param * Given layer of type AtrousConvolution. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::AtrousConvolution<>* /*layer*/) const + { + return "atrousconvolution"; + } + + /** + * Return the name of the given layer of type AlphaDropout as a string. + * + * @param * Given layer of type AlphaDropout. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::AlphaDropout<>* /*layer*/) const + { + return "alphadropout"; + } + + /** + * Return the name of the given layer of type BatchNorm as a string. + * + * @param * Given layer of type BatchNorm. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::BatchNorm<>* /*layer*/) const + { + return "batchnorm"; + } + + /** + * Return the name of the given layer of type Constant as a string. + * + * @param * Given layer of type Constant. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Constant<>* /*layer*/) const + { + return "constant"; + } + + /** + * Return the name of the given layer of type Convolution as a string. + * + * @param * Given layer of type Convolution. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Convolution<>* /*layer*/) const + { + return "convolution"; + } + + /** + * Return the name of the given layer of type DropConnect as a string. + * + * @param * Given layer of type DropConnect. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::DropConnect<>* /*layer*/) const + { + return "dropconnect"; + } + + /** + * Return the name of the given layer of type Dropout as a string. + * + * @param * Given layer of type Dropout. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Dropout<>* /*layer*/) const + { + return "dropout"; + } + + /** + * Return the name of the given layer of type FlexibleReLU as a string. + * + * @param * Given layer of type FlexibleReLU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::FlexibleReLU<>* /*layer*/) const + { + return "flexiblerelu"; + } + + /** + * Return the name of the given layer of type LayerNorm as a string. + * + * @param * Given layer of type LayerNorm. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LayerNorm<>* /*layer*/) const + { + return "layernorm"; + } + + /** + * Return the name of the given layer of type Linear as a string. + * + * @param * Given layer of type Linear. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Linear<>* /*layer*/) const + { + return "linear"; + } + + /** + * Return the name of the given layer of type LinearNoBias as a string. + * + * @param * Given layer of type LinearNoBias. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LinearNoBias<>* /*layer*/) const + { + return "linearnobias"; + } + + /** + * Return the name of the given layer of type NoisyLinear as a string. + * + * @param * Given layer of type NoisyLinear. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::NoisyLinear<>* /*layer*/) const + { + return "noisylinear"; + } + + /** + * Return the name of the given layer of type MaxPooling as a string. + * + * @param * Given layer of type MaxPooling. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::MaxPooling<>* /*layer*/) const + { + return "maxpooling"; + } + + /** + * Return the name of the given layer of type MeanPooling as a string. + * + * @param * Given layer of type MeanPooling. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::MeanPooling<>* /*layer*/) const + { + return "meanpooling"; + } + + /** + * Return the name of the given layer of type LpPooling as a string. + * + * @param * Given layer of type LpPooling. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LpPooling<>* /*layer*/) const + { + return "lppooling"; + } + + /** + * Return the name of the given layer of type MultiplyConstant as a string. + * + * @param * Given layer of type MultiplyConstant. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::MultiplyConstant<>* /*layer*/) const + { + return "multiplyconstant"; + } + + /** + * Return the name of the given layer of type ReLULayer as a string. + * + * @param * Given layer of type ReLULayer. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::ReLULayer<>* /*layer*/) const + { + return "relu"; + } + + /** + * Return the name of the given layer of type TransposedConvolution as a + * string. + * + * @param * Given layer of type TransposedConvolution. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::TransposedConvolution<>* /*layer*/) const + { + return "transposedconvolution"; + } + + /** + * Return the name of the given layer of type IdentityLayer as a string. + * + * @param * Given layer of type IdentityLayer. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::IdentityLayer<>* /*layer*/) const + { + return "identity"; + } + + /** + * Return the name of the given layer of type TanHLayer as a string. + * + * @param * Given layer of type TanHLayer. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::TanHLayer<>* /*layer*/) const + { + return "tanh"; + } + + /** + * Return the name of the given layer of type ELU as a string. + * + * @param * Given layer of type ELU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::ELU<>* /*layer*/) const + { + return "elu"; + } + + /** + * Return the name of the given layer of type HardTanH as a string. + * + * @param * Given layer of type HardTanH. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::HardTanH<>* /*layer*/) const + { + return "hardtanh"; + } + + /** + * Return the name of the given layer of type LeakyReLU as a string. + * + * @param * Given layer of type LeakyReLU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LeakyReLU<>* /*layer*/) const + { + return "leakyrelu"; + } + + /** + * Return the name of the given layer of type PReLU as a string. + * + * @param * Given layer of type PReLU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::PReLU<>* /*layer*/) const + { + return "prelu"; + } + + /** + * Return the name of the given layer of type SigmoidLayer as a string. + * + * @param * Given layer of type SigmoidLayer. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::SigmoidLayer<>* /*layer*/) const + { + return "sigmoid"; + } + + /** + * Return the name of the given layer of type LogSoftMax as a string. + * + * @param * Given layer of type LogSoftMax. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LogSoftMax<>* /*layer*/) const + { + return "logsoftmax"; + } + + /* + * Return the name of the given layer of type LSTM as a string. + * + * @param * Given layer of type LSTM. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::LSTM<>* /*layer*/) const + { + return "lstm"; + } + + /** + * Return the name of the given layer of type CReLU as a string. + * + * @param * Given layer of type CReLU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::CReLU<>* /*layer*/) const + { + return "crelu"; + } + + /** + * Return the name of the given layer of type Highway as a string. + * + * @param * Given layer of type Highway. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Highway<>* /*layer*/) const + { + return "highway"; + } + + /** + * Return the name of the given layer of type GRU as a string. + * + * @param * Given layer of type GRU. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::GRU<>* /*layer*/) const + { + return "gru"; + } + + /** + * Return the name of the given layer of type Glimpse as a string. + * + * @param * Given layer of type Glimpse. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::Glimpse<>* /*layer*/) const + { + return "glimpse"; + } + + /** + * Return the name of the given layer of type FastLSTM as a string. + * + * @param * Given layer of type FastLSTM. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::FastLSTM<>* /*layer*/) const + { + return "fastlstm"; + } + + /** + * Return the name of the given layer of type WeightNorm as a string. + * + * @param * Given layer of type WeightNorm. + * @return The string representation of the layer. + */ + std::string LayerString(mlpack::ann::WeightNorm<>* /*layer*/) const + { + return "weightnorm"; + } + + /** + * Return the name of the layer of specified type as a string. + * + * @param * Given layer of any type. + * @return A string declaring that the layer is unsupported. + */ + template + std::string LayerString(T* /*layer*/) const + { + return "unsupported"; + } + + //! Overload function call. + std::string operator()(MoreTypes layer) const + { + return layer.apply_visitor(*this); + } + + //! Overload function call. + template + std::string operator()(LayerType* layer) const + { + return LayerString(layer); + } +}; diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp index aab2c4bc1b..d6f5374096 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/categorical_dqn.hpp @@ -23,8 +23,6 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * Implementation of the Categorical Deep Q-Learning network. * For more information, see the following. @@ -43,9 +41,9 @@ using namespace mlpack::ann; * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = EmptyLoss<>, - typename InitType = GaussianInitialization, - typename NetworkType = FFN + typename OutputLayerType = mlpack::ann::EmptyLoss<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename NetworkType = mlpack::ann::FFN > class CategoricalDQN { @@ -83,21 +81,21 @@ class CategoricalDQN vMax(config.VMax()), isNoisy(isNoisy) { - network.Add(new Linear<>(inputDim, h1)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(inputDim, h1)); + network.Add(new mlpack::ann::ReLULayer<>()); if (isNoisy) { noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h1, h2)); - network.Add(new ReLULayer<>()); + network.Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); noisyLayerIndex.push_back(network.Model().size()); - network.Add(new NoisyLinear<>(h2, outputDim * atomSize)); + network.Add(new mlpack::ann::NoisyLinear<>(h2, outputDim * atomSize)); } else { - network.Add(new Linear<>(h1, h2)); - network.Add(new ReLULayer<>()); - network.Add(new Linear<>(h2, outputDim * atomSize)); + network.Add(new mlpack::ann::Linear<>(h1, h2)); + network.Add(new mlpack::ann::ReLULayer<>()); + network.Add(new mlpack::ann::Linear<>(h2, outputDim * atomSize)); } } @@ -181,9 +179,9 @@ class CategoricalDQN */ void ResetNoise() { - for (size_t i = 0; i < noisyLayerIndex.size(); i++) + for (size_t i = 0; i < noisyLayerIndex.size(); ++i) { - boost::get*> + boost::get*> (network.Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -236,7 +234,7 @@ class CategoricalDQN std::vector noisyLayerIndex; //! Locally-stored softmax activation function. - Softmax<> softMax; + mlpack::ann::Softmax<> softMax; //! Locally-stored activations from softMax. arma::mat activations; diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp index 5272466ccc..925da63ee2 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/dueling_dqn.hpp @@ -22,8 +22,6 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * Implementation of the Dueling Deep Q-Learning network. * For more information, see the following. @@ -46,12 +44,12 @@ using namespace mlpack::ann; * @tparam ValueNetworkType The type of network used for value network. */ template < - typename OutputLayerType = EmptyLoss<>, - typename InitType = GaussianInitialization, - typename CompleteNetworkType = FFN, - typename FeatureNetworkType = Sequential<>, - typename AdvantageNetworkType = Sequential<>, - typename ValueNetworkType = Sequential<> + typename OutputLayerType = mlpack::ann::EmptyLoss<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename CompleteNetworkType = mlpack::ann::FFN, + typename FeatureNetworkType = mlpack::ann::Sequential<>, + typename AdvantageNetworkType = mlpack::ann::Sequential<>, + typename ValueNetworkType = mlpack::ann::Sequential<> > class DuelingDQN { @@ -59,14 +57,14 @@ class DuelingDQN //! Default constructor. DuelingDQN() : isNoisy(false) { - featureNetwork = new Sequential<>(); - valueNetwork = new Sequential<>(); - advantageNetwork = new Sequential<>(); - concat = new Concat<>(true); + featureNetwork = new mlpack::ann::Sequential<>(); + valueNetwork = new mlpack::ann::Sequential<>(); + advantageNetwork = new mlpack::ann::Sequential<>(); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); } @@ -92,42 +90,42 @@ class DuelingDQN completeNetwork(outputLayer, init), isNoisy(isNoisy) { - featureNetwork = new Sequential<>(); - featureNetwork->Add(new Linear<>(inputDim, h1)); - featureNetwork->Add(new ReLULayer<>()); + featureNetwork = new mlpack::ann::Sequential<>(); + featureNetwork->Add(new mlpack::ann::Linear<>(inputDim, h1)); + featureNetwork->Add(new mlpack::ann::ReLULayer<>()); - valueNetwork = new Sequential<>(); - advantageNetwork = new Sequential<>(); + valueNetwork = new mlpack::ann::Sequential<>(); + advantageNetwork = new mlpack::ann::Sequential<>(); if (isNoisy) { noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new NoisyLinear<>(h1, h2)); - advantageNetwork->Add(new NoisyLinear<>(h1, h2)); + valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); + advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h1, h2)); - valueNetwork->Add(new ReLULayer<>()); - advantageNetwork->Add(new ReLULayer<>()); + valueNetwork->Add(new mlpack::ann::ReLULayer<>()); + advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); noisyLayerIndex.push_back(valueNetwork->Model().size()); - valueNetwork->Add(new NoisyLinear<>(h2, 1)); - advantageNetwork->Add(new NoisyLinear<>(h2, outputDim)); + valueNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, 1)); + advantageNetwork->Add(new mlpack::ann::NoisyLinear<>(h2, outputDim)); } else { - valueNetwork->Add(new Linear<>(h1, h2)); - valueNetwork->Add(new ReLULayer<>()); - valueNetwork->Add(new Linear<>(h2, 1)); + valueNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); + valueNetwork->Add(new mlpack::ann::ReLULayer<>()); + valueNetwork->Add(new mlpack::ann::Linear<>(h2, 1)); - advantageNetwork->Add(new Linear<>(h1, h2)); - advantageNetwork->Add(new ReLULayer<>()); - advantageNetwork->Add(new Linear<>(h2, outputDim)); + advantageNetwork->Add(new mlpack::ann::Linear<>(h1, h2)); + advantageNetwork->Add(new mlpack::ann::ReLULayer<>()); + advantageNetwork->Add(new mlpack::ann::Linear<>(h2, outputDim)); } - concat = new Concat<>(true); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -150,10 +148,10 @@ class DuelingDQN valueNetwork(valueNetwork), isNoisy(isNoisy) { - concat = new Concat<>(true); + concat = new mlpack::ann::Concat<>(true); concat->Add(valueNetwork); concat->Add(advantageNetwork); - completeNetwork.Add(new IdentityLayer<>()); + completeNetwork.Add(new mlpack::ann::IdentityLayer<>()); completeNetwork.Add(featureNetwork); completeNetwork.Add(concat); this->ResetParameters(); @@ -245,9 +243,9 @@ class DuelingDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - boost::get*> + boost::get*> (valueNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); - boost::get*> + boost::get*> (advantageNetwork->Model()[noisyLayerIndex[i]])->ResetNoise(); } } @@ -262,7 +260,7 @@ class DuelingDQN CompleteNetworkType completeNetwork; //! Locally-stored concat network. - Concat<>* concat; + mlpack::ann::Concat<>* concat; //! Locally-stored feature network. FeatureNetworkType* featureNetwork; @@ -283,7 +281,7 @@ class DuelingDQN arma::mat actionValues; //! Locally-stored loss function. - MeanSquaredError<> lossFunction; + mlpack::ann::MeanSquaredError<> lossFunction; }; } // namespace rl diff --git a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp index 7d71e75df7..9d8adadd37 100644 --- a/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp +++ b/src/mlpack/methods/reinforcement_learning/q_networks/simple_dqn.hpp @@ -21,17 +21,15 @@ namespace mlpack { namespace rl { -using namespace mlpack::ann; - /** * @tparam OutputLayerType The output layer type of the network. * @tparam InitType The initialization type used for the network. * @tparam NetworkType The type of network used for simple dqn. */ template< - typename OutputLayerType = MeanSquaredError<>, - typename InitType = GaussianInitialization, - typename NetworkType = FFN + typename OutputLayerType = mlpack::ann::MeanSquaredError<>, + typename InitType = mlpack::ann::GaussianInitialization, + typename NetworkType = mlpack::ann::FFN > class SimpleDQN { @@ -61,21 +59,21 @@ class SimpleDQN network(outputLayer, init), isNoisy(isNoisy) { - network.Add(new Linear(h1)); - network.Add(new ReLU()); + network.Add(new mlpack::ann::Linear(h1)); + network.Add(new mlpack::ann::ReLU()); if (isNoisy) { noisyLayerIndex.push_back(network.Network().size()); - network.Add(new NoisyLinear(h2)); - network.Add(new ReLU()); + network.Add(new mlpack::ann::NoisyLinear(h2)); + network.Add(new mlpack::ann::ReLU()); noisyLayerIndex.push_back(network.Network().size()); - network.Add(new NoisyLinear(outputDim)); + network.Add(new mlpack::ann::NoisyLinear(outputDim)); } else { - network.Add(new Linear(h2)); - network.Add(new ReLU()); - network.Add(new Linear(outputDim)); + network.Add(new mlpack::ann::Linear(h2)); + network.Add(new mlpack::ann::ReLU()); + network.Add(new mlpack::ann::Linear(outputDim)); } } @@ -132,7 +130,7 @@ class SimpleDQN { for (size_t i = 0; i < noisyLayerIndex.size(); i++) { - dynamic_cast( + dynamic_cast( network.Network()[noisyLayerIndex[i]])->ResetNoise(); } }