Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create the header to determine the layer type #1987

Merged
merged 18 commits into from Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/mlpack/methods/ann/CMakeLists.txt
Expand Up @@ -7,6 +7,7 @@ set(SOURCES
rnn_impl.hpp
brnn.hpp
brnn_impl.hpp
layer_names.hpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you write a simple test for the visitor? Similar to https://github.com/mlpack/mlpack/blob/master/src/mlpack/tests/ann_visitor_test.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll get it done tomorrow, pushing the changes here for now

)

add_subdirectory(visitor)
Expand Down
322 changes: 322 additions & 0 deletions src/mlpack/methods/ann/layer_names.hpp
@@ -0,0 +1,322 @@
/**
* @file layer_names.hpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might make sense to split this into an _impl.hpp file also; up to you.

* @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 <mlpack/core.hpp>
#include <mlpack/methods/ann/layer/layer.hpp>
#include <mlpack/methods/ann/layer/layer_types.hpp>
#include <boost/variant/static_visitor.hpp>
#include <string>

using namespace mlpack::ann;

/**
* Implementation of a class that returns the string representation of the
* name of the given layer.
*/
class LayerNameVisitor : public boost::static_visitor<std::string>
rcurtin marked this conversation as resolved.
Show resolved Hide resolved
{
public:
//! Create the LayerNameVisitor object.
LayerNameVisitor()
rcurtin marked this conversation as resolved.
Show resolved Hide resolved
{
}

/*
* 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(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(AlphaDropout<>* /*layer*/) const
{
return "alphadropout";
}
rcurtin marked this conversation as resolved.
Show resolved Hide resolved

/*
* 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(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(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(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(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(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(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(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(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(LinearNoBias<>* /*layer*/) const
{
return "linearnobias";
}

/*
* 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(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(MeanPooling<>* /*layer*/) const
{
return "meanpooling";
}

/*
* 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(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(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(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(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(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(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(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(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(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(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(LogSoftMax<>* /*layer*/) const
{
return "logsoftmax";
}

/*
* 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<typename T>
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<typename LayerType>
std::string operator()(LayerType* layer) const
rcurtin marked this conversation as resolved.
Show resolved Hide resolved
{
return LayerString(layer);
}
};
1 change: 1 addition & 0 deletions src/mlpack/tests/CMakeLists.txt
Expand Up @@ -52,6 +52,7 @@ add_executable(mlpack_test
krann_search_test.cpp
ksinit_test.cpp
lars_test.cpp
layer_names_test.cpp
lin_alg_test.cpp
linear_regression_test.cpp
linear_svm_test.cpp
Expand Down