Move some files that aren't yet adapted or are unneeded.
This commit is contained in:
@@ -1,159 +0,0 @@
|
||||
/**
|
||||
* @file core/cereal/pointer_variant_wrapper.hpp
|
||||
* @author Omar Shrit
|
||||
*
|
||||
* Implementation of a boost::variant wrapper to enable the serialization of
|
||||
* the pointers inside boost variant in cereal
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_CEREAL_POINTER_VARIANT_WRAPPER_HPP
|
||||
#define MLPACK_CORE_CEREAL_POINTER_VARIANT_WRAPPER_HPP
|
||||
|
||||
#include <cereal/archives/json.hpp>
|
||||
#include <cereal/archives/portable_binary.hpp>
|
||||
#include <cereal/archives/xml.hpp>
|
||||
#include <cereal/types/boost_variant.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
#include <boost/variant/variant_fwd.hpp>
|
||||
#include <boost/variant/static_visitor.hpp>
|
||||
|
||||
#include "pointer_wrapper.hpp"
|
||||
|
||||
namespace cereal {
|
||||
|
||||
// Forward declaration.
|
||||
template<typename... VariantTypes>
|
||||
class PointerVariantWrapper;
|
||||
|
||||
/**
|
||||
* Serialize a boost variant in which the variant it self is a raw pointer.
|
||||
* This wrapper will wrap each variant independently by encapsulating each variant
|
||||
* into the PoninterWrapper we have created already.
|
||||
*
|
||||
* @param t A reference to boost variant that holds raw pointer.
|
||||
*/
|
||||
template<typename... VariantTypes>
|
||||
inline PointerVariantWrapper<VariantTypes...>
|
||||
make_pointer_variant(boost::variant<VariantTypes...>& t)
|
||||
{
|
||||
return PointerVariantWrapper<VariantTypes...>(t);
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
struct save_visitor : public boost::static_visitor<void>
|
||||
{
|
||||
save_visitor(Archive& ar) : ar(ar) {}
|
||||
|
||||
template<class T>
|
||||
void operator()(const T* value) const
|
||||
{
|
||||
ar(CEREAL_POINTER(value));
|
||||
}
|
||||
|
||||
template<typename... Types>
|
||||
void operator()(boost::variant<Types*...>& value) const
|
||||
{
|
||||
ar(make_pointer_variant(value));
|
||||
}
|
||||
|
||||
Archive& ar;
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
struct load_visitor : public boost::static_visitor<void>
|
||||
{
|
||||
template<typename Archive, typename VariantType>
|
||||
static void load_impl(Archive& ar, VariantType& variant, std::true_type)
|
||||
{
|
||||
// Note that T will be a pointer type.
|
||||
T loadVariant;
|
||||
ar(CEREAL_POINTER(loadVariant));
|
||||
variant = loadVariant;
|
||||
}
|
||||
|
||||
template<typename Archive, typename VariantType>
|
||||
static void load_impl(Archive& ar, VariantType& value, std::false_type)
|
||||
{
|
||||
// This must be a nested boost::variant.
|
||||
T loadVariant;
|
||||
ar(make_pointer_variant(loadVariant));
|
||||
value = loadVariant;
|
||||
}
|
||||
|
||||
template<typename Archive, typename VariantType>
|
||||
static void load(Archive& ar, VariantType& variant)
|
||||
{
|
||||
// Delegate to the proper load_impl() overload depending on whether T is a
|
||||
// pointer type. If T is not a pointer type, then we expect it to be a
|
||||
// nested boost::variant.
|
||||
load_impl(ar, variant, typename std::is_pointer<T>::type());
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* The objective of this class is to create a wrapper for
|
||||
* boost::variant.
|
||||
* Cereal supports the serialization of boost::variant, but
|
||||
* we need to serialize it if it holds a raw pointers.
|
||||
* This class depeds on the PointerWrapper we have already created in which it is
|
||||
* used to serialize each variant independently
|
||||
*/
|
||||
template<typename... VariantTypes>
|
||||
class PointerVariantWrapper
|
||||
{
|
||||
public:
|
||||
PointerVariantWrapper(boost::variant<VariantTypes...>& pointerVar) :
|
||||
pointerVariant(pointerVar)
|
||||
{}
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive& ar) const
|
||||
{
|
||||
// which represents the index in std::variant.
|
||||
int which = pointerVariant.which();
|
||||
ar(CEREAL_NVP(which));
|
||||
save_visitor<Archive> s(ar);
|
||||
boost::apply_visitor(s, pointerVariant);
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive& ar)
|
||||
{
|
||||
// Load the size of the serialized type.
|
||||
int which;
|
||||
ar(CEREAL_NVP(which));
|
||||
|
||||
// Create function pointers to each overload of load_visitor<T>::load, for
|
||||
// all T in VariantTypes.
|
||||
using LoadFuncType = void(*)(Archive&, boost::variant<VariantTypes...>&);
|
||||
LoadFuncType loadFuncArray[] = { &load_visitor<VariantTypes>::load... };
|
||||
|
||||
if (which >= int(sizeof(loadFuncArray)/sizeof(loadFuncArray[0])))
|
||||
throw std::runtime_error("Invalid 'which' selector when"
|
||||
"deserializing boost::variant");
|
||||
|
||||
loadFuncArray[which](ar, pointerVariant);
|
||||
}
|
||||
|
||||
private:
|
||||
boost::variant<VariantTypes...>& pointerVariant;
|
||||
};
|
||||
|
||||
/**
|
||||
* Cereal does not support the serialization of raw pointer.
|
||||
* This macro enable developers to serialize boost::variant that holds raw
|
||||
* pointers by using the above PointerVariantWrapper class which replace the
|
||||
* internal raw pointers by smart pointer internally.
|
||||
*
|
||||
* @param T boost::variant that holds raw pointer to be serialized.
|
||||
*/
|
||||
#define CEREAL_VARIANT_POINTER(T) cereal::make_pointer_variant(T)
|
||||
|
||||
} // namespace cereal
|
||||
|
||||
#endif // CEREAL_POINTER_VARIANT_WRAPPER_HPP
|
||||
@@ -1,97 +0,0 @@
|
||||
/**
|
||||
* @file core/cereal/pointer_vector_variant_wrapper.hpp
|
||||
* @author Omar Shrit
|
||||
*
|
||||
* Implementation of a boost::variant wrapper to enable the serialization of
|
||||
* the pointers inside boost variant in cereal
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_CORE_CEREAL_POINTER_VECTOR_VARIANT_WRAPPER_HPP
|
||||
#define MLPACK_CORE_CEREAL_POINTER_VECTOR_VARIANT_WRAPPER_HPP
|
||||
|
||||
#include "pointer_wrapper.hpp"
|
||||
#include "pointer_variant_wrapper.hpp"
|
||||
#include "pointer_vector_wrapper.hpp"
|
||||
|
||||
namespace cereal {
|
||||
|
||||
// Forward declaration
|
||||
template<typename... VariantTypes>
|
||||
class PointerVectorVariantWrapper;
|
||||
|
||||
/**
|
||||
* Serialize a std::vector of boost variants in which the variant in each boost
|
||||
* variant is a raw pointer.
|
||||
* This wrapper will wrap each boost variant independently by encapsulating each
|
||||
* boost variant into the PoninterVariantWrapper we have created already.
|
||||
*
|
||||
* @param t A reference to a vector of boost variants that holds raw pointer.
|
||||
*/
|
||||
template<typename... VariantTypes>
|
||||
inline PointerVectorVariantWrapper<VariantTypes...>
|
||||
make_vector_pointer_variant(std::vector<boost::variant<VariantTypes...>>& t)
|
||||
{
|
||||
return PointerVectorVariantWrapper<VariantTypes...>(t);
|
||||
}
|
||||
|
||||
/**
|
||||
* The objective of this class is to create a wrapper for
|
||||
* a vector of boost::variant that holds pointer.
|
||||
* Cereal supports the serialization of boost::variant, but
|
||||
* we need to serialize it if it holds a vector of boost::variant that holds a
|
||||
* pointers.
|
||||
*/
|
||||
template<typename... VariantTypes>
|
||||
class PointerVectorVariantWrapper
|
||||
{
|
||||
public:
|
||||
PointerVectorVariantWrapper(
|
||||
std::vector<boost::variant<VariantTypes...>>& vecPointerVar)
|
||||
: vectorPointerVariant(vecPointerVar)
|
||||
{}
|
||||
|
||||
template<class Archive>
|
||||
void save(Archive& ar) const
|
||||
{
|
||||
size_t vecSize = vectorPointerVariant.size();
|
||||
ar(CEREAL_NVP(vecSize));
|
||||
for (size_t i = 0; i < vectorPointerVariant.size(); ++i)
|
||||
{
|
||||
ar(CEREAL_VARIANT_POINTER(vectorPointerVariant.at(i)));
|
||||
}
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void load(Archive& ar)
|
||||
{
|
||||
size_t vecSize = 0;
|
||||
ar(CEREAL_NVP(vecSize));
|
||||
vectorPointerVariant.resize(vecSize);
|
||||
for (size_t i = 0; i < vectorPointerVariant.size(); ++i)
|
||||
{
|
||||
ar(CEREAL_VARIANT_POINTER(vectorPointerVariant.at(i)));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<boost::variant<VariantTypes...>>& vectorPointerVariant;
|
||||
};
|
||||
|
||||
/**
|
||||
* Cereal does not support the serialization of raw pointer.
|
||||
* This macro enable developers to serialize a std vector that holds boost::variants
|
||||
* that holds raw pointers by using the above PointerVectorVariantWrapper class
|
||||
* which replace the internal raw pointers by smart pointer internally.
|
||||
*
|
||||
* @param T std::vector<boost::variant> that holds raw pointer to be serialized.
|
||||
*/
|
||||
#define CEREAL_VECTOR_VARIANT_POINTER(T) cereal::make_vector_pointer_variant(T)
|
||||
|
||||
} // namespace cereal
|
||||
|
||||
#endif // CEREAL_POINTER_VECTOR_VARIANT_WRAPPER_HPP
|
||||
|
||||
@@ -1,443 +0,0 @@
|
||||
/**
|
||||
* @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 <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>
|
||||
{
|
||||
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(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(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(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";
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 NoisyLinear as a string.
|
||||
*
|
||||
* @param * Given layer of type NoisyLinear.
|
||||
* @return The string representation of the layer.
|
||||
*/
|
||||
std::string LayerString(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(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 LpPooling as a string.
|
||||
*
|
||||
* @param * Given layer of type LpPooling.
|
||||
* @return The string representation of the layer.
|
||||
*/
|
||||
std::string LayerString(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(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 given layer of type LSTM as a string.
|
||||
*
|
||||
* @param * Given layer of type LSTM.
|
||||
* @return The string representation of the layer.
|
||||
*/
|
||||
std::string LayerString(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(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(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(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(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(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(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<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
|
||||
{
|
||||
return LayerString(layer);
|
||||
}
|
||||
};
|
||||
@@ -1,14 +0,0 @@
|
||||
# Define the files we need to compile
|
||||
# Anything not in this list will not be compiled into mlpack.
|
||||
set(SOURCES
|
||||
check_input_shape.hpp
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
|
||||
endforeach()
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# the parent scope).
|
||||
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
|
||||
@@ -1,53 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/util/check_input_shape.hpp
|
||||
* @author Khizir Siddiqui
|
||||
* @author Nippun Sharma
|
||||
*
|
||||
* Definition of the CheckInputShape() function that checks
|
||||
* whether the shape of input is consistent with the first layer
|
||||
* of the neural network.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_UTIL_CHECK_INPUT_SHAPE_HPP
|
||||
#define MLPACK_METHODS_ANN_UTIL_CHECK_INPUT_SHAPE_HPP
|
||||
|
||||
#include <mlpack/methods/ann/visitor/input_shape_visitor.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */{
|
||||
|
||||
template<typename T>
|
||||
void CheckInputShape(const T& network,
|
||||
const size_t inputShape,
|
||||
const std::string& functionName)
|
||||
{
|
||||
for (size_t l = 0; l < network.size(); ++l)
|
||||
{
|
||||
size_t layerInShape = boost::apply_visitor(InShapeVisitor(), network[l]);
|
||||
if (layerInShape == 0)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
else if (layerInShape == inputShape)
|
||||
{
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string estr = functionName + ": the first layer of the network " +
|
||||
"expects " + std::to_string(layerInShape) + " elements, but the " +
|
||||
"input has " + std::to_string(inputShape) + " dimensions!";
|
||||
throw std::logic_error(estr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,47 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/util/deterministic_update.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Definition of the DeterministicUpdate() function to update the layer and
|
||||
* sub-layer training/testing state.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_UTIL_DETERMINISTIC_UPDATE_HPP
|
||||
#define MLPACK_METHODS_ANN_UTIL_DETERMINISTIC_UPDATE_HPP
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */{
|
||||
|
||||
/**
|
||||
* Update the deterministic parameter for the given layer and all sub-layer
|
||||
* with the specified value.
|
||||
*
|
||||
* @note During training you should set the deterministic parameter for each
|
||||
* layer to false and during testing you should set deterministic to true.
|
||||
*
|
||||
* @tparam LayerType The type of the given layer e.g. Dropout, DropConnect.
|
||||
* @param layer The layer (including sub-layer) to be updated.
|
||||
* @param deterministic The training/testing state,
|
||||
* training = false, testing = true.
|
||||
*/
|
||||
template<typename LayerType>
|
||||
void DeterministicUpdate(const LayerType& layer, const bool deterministic)
|
||||
{
|
||||
layer->Deterministic() = deterministic;
|
||||
|
||||
if (layer->Model().size() > 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
DeterministicUpdate(layer->Model()[i], deterministic);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,55 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/util/gradient_update.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Definition of the GradientUpdate() function which assignes a portion of the
|
||||
* given gradient to the layer/sub-layer.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_UTIL_GRADIENT_UPDATE_HPP
|
||||
#define MLPACK_METHODS_ANN_UTIL_GRADIENT_UPDATE_HPP
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */{
|
||||
|
||||
/**
|
||||
* Assign a portion of the given gradient matrix to the given layer/sub-layer.
|
||||
*
|
||||
* @tparam LayerType The type of the layer that the gradient is assigned to
|
||||
* e.g. Linear, Convolution.
|
||||
* @tparam MathTest The type of the gradient matrix e.g. arma::Mat<double>,
|
||||
* arma::Mat<float>.
|
||||
* @param layer The layer that the gradient is assigned to.
|
||||
* @param offset The beginning of the gradient portion we assign to the layer.
|
||||
*/
|
||||
template<typename LayerType, typename MatType>
|
||||
size_t GradientUpdate(
|
||||
const LayerType& layer, MatType& gradient, const size_t offset)
|
||||
{
|
||||
size_t size = 0;
|
||||
|
||||
if (layer->Parameters().n_elem > 0)
|
||||
{
|
||||
layer->Gradient() = arma::mat(gradient.memptr() + offset,
|
||||
layer->Parameters().n_rows, layer->Parameters().n_cols, false, false);
|
||||
size += layer->Parameters().n_elem;
|
||||
}
|
||||
|
||||
if (layer->Model().size() > 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
size += GradientUpdate(layer->Model()[i], gradient, offset + size);
|
||||
}
|
||||
|
||||
return size;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,44 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/util/loss_update.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Definition of the LossUpdate() function which returns the layer/sub-layer
|
||||
* loss.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_UTIL_LOSS_UPDATE_HPP
|
||||
#define MLPACK_METHODS_ANN_UTIL_LOSS_UPDATE_HPP
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */{
|
||||
|
||||
/**
|
||||
* Get the los from the given layer/sub-layer.
|
||||
*
|
||||
* @tparam Layer The type of the given layer.
|
||||
* @param layer The layer to get the loss for.
|
||||
* @return The layer loss.
|
||||
*/
|
||||
template<typename LayerType>
|
||||
double LossUpdate(const LayerType& layer)
|
||||
{
|
||||
double loss = layer->Loss();
|
||||
|
||||
if (layer->Model().size() > 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
loss += LossUpdate(layer->Model()[i]);
|
||||
}
|
||||
|
||||
return loss;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,40 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/util/reset_update.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Definition of the ResetUpdate() function which resets the layer state.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef MLPACK_METHODS_ANN_UTIL_RESET_UPDATE_HPP
|
||||
#define MLPACK_METHODS_ANN_UTIL_RESET_UPDATE_HPP
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */{
|
||||
|
||||
/**
|
||||
* Call the Reset() function for the given layer/sub-layer.
|
||||
*
|
||||
* @tparam LayerType The type of the layer that the Reset() function is called.
|
||||
* @param layer The layer for which the Reset() function is called.
|
||||
*/
|
||||
template<typename LayerType>
|
||||
void ResetUpdate(const LayerType& layer)
|
||||
{
|
||||
layer->Reset();
|
||||
|
||||
if (layer->Model().size() > 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
ResetUpdate(layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,71 +0,0 @@
|
||||
# Define the files we need to compile
|
||||
# Anything not in this list will not be compiled into mlpack.
|
||||
set(SOURCES
|
||||
add_visitor.hpp
|
||||
add_visitor_impl.hpp
|
||||
backward_visitor.hpp
|
||||
backward_visitor_impl.hpp
|
||||
bias_set_visitor.hpp
|
||||
bias_set_visitor_impl.hpp
|
||||
copy_visitor.hpp
|
||||
copy_visitor_impl.hpp
|
||||
delete_visitor.hpp
|
||||
delete_visitor_impl.hpp
|
||||
delta_visitor.hpp
|
||||
delta_visitor_impl.hpp
|
||||
deterministic_set_visitor.hpp
|
||||
deterministic_set_visitor_impl.hpp
|
||||
forward_visitor.hpp
|
||||
forward_visitor_impl.hpp
|
||||
gradient_set_visitor.hpp
|
||||
gradient_set_visitor_impl.hpp
|
||||
gradient_update_visitor.hpp
|
||||
gradient_update_visitor_impl.hpp
|
||||
gradient_visitor.hpp
|
||||
gradient_visitor_impl.hpp
|
||||
gradient_zero_visitor.hpp
|
||||
gradient_zero_visitor_impl.hpp
|
||||
load_output_parameter_visitor.hpp
|
||||
load_output_parameter_visitor_impl.hpp
|
||||
loss_visitor.hpp
|
||||
loss_visitor_impl.hpp
|
||||
output_height_visitor.hpp
|
||||
output_height_visitor_impl.hpp
|
||||
output_parameter_visitor.hpp
|
||||
output_parameter_visitor_impl.hpp
|
||||
output_width_visitor.hpp
|
||||
output_width_visitor_impl.hpp
|
||||
parameters_set_visitor.hpp
|
||||
parameters_set_visitor_impl.hpp
|
||||
parameters_visitor.hpp
|
||||
parameters_visitor_impl.hpp
|
||||
reset_cell_visitor.hpp
|
||||
reset_cell_visitor_impl.hpp
|
||||
reset_visitor.hpp
|
||||
reset_visitor_impl.hpp
|
||||
reward_set_visitor.hpp
|
||||
reward_set_visitor_impl.hpp
|
||||
run_set_visitor.hpp
|
||||
run_set_visitor_impl.hpp
|
||||
save_output_parameter_visitor.hpp
|
||||
save_output_parameter_visitor_impl.hpp
|
||||
set_input_height_visitor.hpp
|
||||
set_input_height_visitor_impl.hpp
|
||||
set_input_width_visitor.hpp
|
||||
set_input_width_visitor_impl.hpp
|
||||
weight_set_visitor.hpp
|
||||
weight_set_visitor_impl.hpp
|
||||
weight_size_visitor.hpp
|
||||
weight_size_visitor_impl.hpp
|
||||
input_shape_visitor.hpp
|
||||
input_shape_visitor_impl.hpp
|
||||
)
|
||||
|
||||
# Add directory name to sources.
|
||||
set(DIR_SRCS)
|
||||
foreach(file ${SOURCES})
|
||||
set(DIR_SRCS ${DIR_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/${file})
|
||||
endforeach()
|
||||
# Append sources (with directory name) to list of all mlpack sources (used at
|
||||
# the parent scope).
|
||||
set(MLPACK_SRCS ${MLPACK_SRCS} ${DIR_SRCS} PARENT_SCOPE)
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/add_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Add() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_ADD_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_ADD_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* AddVisitor exposes the Add() method of the given module.
|
||||
*/
|
||||
template <typename... CustomLayers>
|
||||
class AddVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Exposes the Add() method of the given module.
|
||||
template<typename T>
|
||||
AddVisitor(T newLayer);
|
||||
|
||||
//! Exposes the Add() method.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The layer that should be added.
|
||||
LayerTypes<CustomLayers...> newLayer;
|
||||
|
||||
//! Only add the layer if the module implements the Add() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasAddCheck<T, void(T::*)(LayerTypes<CustomLayers...>)>::value, void>::type
|
||||
LayerAdd(T* layer) const;
|
||||
|
||||
//! Do not add the layer if the module doesn't implement the Add() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasAddCheck<T, void(T::*)(LayerTypes<CustomLayers...>)>::value, void>::type
|
||||
LayerAdd(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "add_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/add_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Add() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_ADD_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_ADD_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "add_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! AddVisitor visitor class.
|
||||
template<typename... CustomLayers>
|
||||
template<typename T>
|
||||
inline AddVisitor<CustomLayers...>::AddVisitor(T newLayer) :
|
||||
newLayer(std::move(newLayer))
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
template<typename LayerType>
|
||||
inline void AddVisitor<CustomLayers...>::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerAdd<LayerType>(layer);
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
inline void AddVisitor<CustomLayers...>::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasAddCheck<T, void(T::*)(LayerTypes<CustomLayers...>)>::value, void>::type
|
||||
AddVisitor<CustomLayers...>::LayerAdd(T* layer) const
|
||||
{
|
||||
layer->Add(newLayer);
|
||||
}
|
||||
|
||||
template<typename... CustomLayers>
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasAddCheck<T, void(T::*)(LayerTypes<CustomLayers...>)>::value, void>::type
|
||||
AddVisitor<CustomLayers...>::LayerAdd(T* /* layer */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,85 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/backward_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Backward() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_BACKWARD_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_BACKWARD_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* BackwardVisitor executes the Backward() function given the input, error and
|
||||
* delta parameter.
|
||||
*/
|
||||
class BackwardVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Execute the Backward() function given the input, error and delta
|
||||
//! parameter.
|
||||
BackwardVisitor(const arma::mat& input,
|
||||
const arma::mat& error,
|
||||
arma::mat& delta);
|
||||
|
||||
//! Execute the Backward() function for the layer with the specified index.
|
||||
BackwardVisitor(const arma::mat& input,
|
||||
const arma::mat& error,
|
||||
arma::mat& delta,
|
||||
const size_t index);
|
||||
|
||||
//! Execute the Backward() function.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input parameter set.
|
||||
const arma::mat& input;
|
||||
|
||||
//! The error parameter.
|
||||
const arma::mat& error;
|
||||
|
||||
//! The delta parameter.
|
||||
arma::mat& delta;
|
||||
|
||||
//! The index of the layer to run.
|
||||
size_t index;
|
||||
|
||||
//! Indicates whether to use index or not
|
||||
bool hasIndex;
|
||||
|
||||
//! Execute the Backward() function if the module does not have Run()
|
||||
//! check.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
LayerBackward(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Execute the Backward() function if the module is has Run() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
LayerBackward(T* layer, arma::mat& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "backward_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/backward_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Backward() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_BACKWARD_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_BACKWARD_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "backward_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! BackwardVisitor visitor class.
|
||||
inline BackwardVisitor::BackwardVisitor(const arma::mat& input,
|
||||
const arma::mat& error,
|
||||
arma::mat& delta) :
|
||||
input(input),
|
||||
error(error),
|
||||
delta(delta),
|
||||
index(0),
|
||||
hasIndex(false)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
inline BackwardVisitor::BackwardVisitor(const arma::mat& input,
|
||||
const arma::mat& error,
|
||||
arma::mat& delta,
|
||||
const size_t index) :
|
||||
input(input),
|
||||
error(error),
|
||||
delta(delta),
|
||||
index(index),
|
||||
hasIndex(true)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void BackwardVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerBackward(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void BackwardVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
BackwardVisitor::LayerBackward(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
layer->Backward(input, error, delta);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
BackwardVisitor::LayerBackward(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
if (!hasIndex)
|
||||
{
|
||||
layer->Backward(input, error, delta);
|
||||
}
|
||||
else
|
||||
{
|
||||
layer->Backward(input, error, delta, index);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/bias_set_visitor.hpp
|
||||
* @author Toshal Agrawal
|
||||
*
|
||||
* This file provides an abstraction for the Bias() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* BiasSetVisitor updates the module bias parameters given the parameters set.
|
||||
*/
|
||||
class BiasSetVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Update the bias parameters given the parameters' set and offset.
|
||||
BiasSetVisitor(arma::mat& weight, const size_t offset = 0);
|
||||
|
||||
//! Update the parameters' set.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameters' set.
|
||||
arma::mat& weight;
|
||||
|
||||
//! The parameters' offset.
|
||||
const size_t offset;
|
||||
|
||||
//! Do not update the bias parameters if the module doesn't implement the
|
||||
//! Bias() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Bias() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
|
||||
//! Update the bias parameters if the module implements the Model() and
|
||||
//! Bias() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "bias_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,101 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/bias_set_visitor_impl.hpp
|
||||
* @author Toshal Agrawal
|
||||
*
|
||||
* Implementation of the Bias() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_BIAS_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "bias_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! BiasSetVisitor visitor class.
|
||||
inline BiasSetVisitor::BiasSetVisitor(arma::mat& weight, const size_t offset) :
|
||||
weight(weight),
|
||||
offset(offset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline size_t BiasSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerSize(layer);
|
||||
}
|
||||
|
||||
inline size_t BiasSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
BiasSetVisitor::LayerSize(T* /* layer */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
BiasSetVisitor::LayerSize(T* layer) const
|
||||
{
|
||||
size_t modelOffset = 0;
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(BiasSetVisitor(
|
||||
weight, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
BiasSetVisitor::LayerSize(T* layer) const
|
||||
{
|
||||
layer->Bias() = arma::mat(weight.memptr() + offset,
|
||||
layer->Bias().n_rows, layer->Bias().n_cols, false, false);
|
||||
|
||||
return layer->Bias().n_elem;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasBiasCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
BiasSetVisitor::LayerSize(T* layer) const
|
||||
{
|
||||
layer->Bias() = arma::mat(weight.memptr() + offset,
|
||||
layer->Bias().n_rows, layer->Bias().n_cols, false, false);
|
||||
|
||||
size_t modelOffset = layer->Bias().n_elem;
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(BiasSetVisitor(
|
||||
weight, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,41 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/copy_visitor.hpp
|
||||
* @author Shangtong Zhang
|
||||
*
|
||||
* This file provides an abstraction for copy between layers.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_COPY_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_COPY_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* This visitor is to support copy constructor for neural network module.
|
||||
* We want a layer-wise copy rather than simple duplicate the pointer.
|
||||
*/
|
||||
template <typename... CustomLayers>
|
||||
class CopyVisitor : public boost::static_visitor<LayerTypes<CustomLayers...> >
|
||||
{
|
||||
public:
|
||||
template <typename LayerType>
|
||||
LayerTypes<CustomLayers...> operator()(LayerType*) const;
|
||||
|
||||
LayerTypes<CustomLayers...> operator()(MoreTypes) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation
|
||||
#include "copy_visitor_impl.hpp"
|
||||
#endif
|
||||
|
||||
@@ -1,39 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/copy_visitor_impl.hpp
|
||||
* @author Shangtong Zhang
|
||||
*
|
||||
* This file provides an implementation for copy between layers
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_COPY_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_COPY_VISITOR_IMPL_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
template <typename... CustomLayers>
|
||||
template <typename LayerType>
|
||||
inline LayerTypes<CustomLayers...>
|
||||
CopyVisitor<CustomLayers...>::operator()(LayerType* layer) const
|
||||
{
|
||||
return new LayerType(*layer);
|
||||
}
|
||||
|
||||
template <typename... CustomLayers>
|
||||
inline LayerTypes<CustomLayers...>
|
||||
CopyVisitor<CustomLayers...>::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,51 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/delete_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Delete() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DELETE_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DELETE_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* DeleteVisitor executes the destructor of the instantiated object.
|
||||
*/
|
||||
class DeleteVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Execute the destructor if the layer does not hold layers internally.
|
||||
template<typename LayerType>
|
||||
typename std::enable_if<
|
||||
!HasModelCheck<LayerType>::value, void>::type
|
||||
operator()(LayerType* layer) const;
|
||||
|
||||
//! Execute the destructor if the layer does hold layers internally.
|
||||
template<typename LayerType>
|
||||
typename std::enable_if<
|
||||
HasModelCheck<LayerType>::value, void>::type
|
||||
operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "delete_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,53 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/delete_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Delete() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DELETE_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DELETE_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "delete_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! DeleteVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline typename std::enable_if<
|
||||
!HasModelCheck<LayerType>::value, void>::type
|
||||
DeleteVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
if (layer)
|
||||
delete layer;
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline typename std::enable_if<
|
||||
HasModelCheck<LayerType>::value, void>::type
|
||||
DeleteVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
if (layer)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
boost::apply_visitor(DeleteVisitor(), layer->Model()[i]);
|
||||
|
||||
delete layer;
|
||||
}
|
||||
}
|
||||
|
||||
inline void DeleteVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/delta_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Delta() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DELTA_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DELTA_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* DeltaVisitor exposes the delta parameter of the given module.
|
||||
*/
|
||||
class DeltaVisitor : public boost::static_visitor<arma::mat&>
|
||||
{
|
||||
public:
|
||||
//! Return the delta parameter.
|
||||
template<typename LayerType>
|
||||
arma::mat& operator()(LayerType* layer) const;
|
||||
|
||||
arma::mat& operator()(MoreTypes layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "delta_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/delta_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Delta() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DELTA_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DELTA_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "delta_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! DeltaVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline arma::mat& DeltaVisitor::operator()(LayerType *layer) const
|
||||
{
|
||||
return layer->Delta();
|
||||
}
|
||||
|
||||
inline arma::mat& DeltaVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,83 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/deterministic_set_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Deterministic() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DETERMINISTIC_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DETERMINISTIC_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* DeterministicSetVisitor set the deterministic parameter given the
|
||||
* deterministic value.
|
||||
*/
|
||||
class DeterministicSetVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Set the deterministic parameter given the current deterministic value.
|
||||
DeterministicSetVisitor(const bool deterministic = true);
|
||||
|
||||
//! Set the deterministic parameter.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The deterministic parameter.
|
||||
const bool deterministic;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Deterministic() and Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerDeterministic(T* layer) const;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerDeterministic(T* layer) const;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Deterministic() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerDeterministic(T* layer) const;
|
||||
|
||||
//! Do not set the deterministic parameter if the module doesn't implement the
|
||||
//! Deterministic() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerDeterministic(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "deterministic_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/deterministic_set_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Deterministic() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_DETERMINISTIC_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_DETERMINISTIC_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "deterministic_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! DeterministicSetVisitor visitor class.
|
||||
inline DeterministicSetVisitor::DeterministicSetVisitor(
|
||||
const bool deterministic) : deterministic(deterministic)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void DeterministicSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerDeterministic(layer);
|
||||
}
|
||||
|
||||
inline void DeterministicSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
DeterministicSetVisitor::LayerDeterministic(T* layer) const
|
||||
{
|
||||
layer->Deterministic() = deterministic;
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(DeterministicSetVisitor(deterministic),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
DeterministicSetVisitor::LayerDeterministic(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(DeterministicSetVisitor(deterministic),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
DeterministicSetVisitor::LayerDeterministic(T* layer) const
|
||||
{
|
||||
layer->Deterministic() = deterministic;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasDeterministicCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
DeterministicSetVisitor::LayerDeterministic(T* /* input */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,54 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/forward_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Forward() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_FORWARD_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_FORWARD_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* ForwardVisitor executes the Forward() function given the input and output
|
||||
* parameter.
|
||||
*/
|
||||
class ForwardVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Execute the Forward() function given the input and output parameter.
|
||||
ForwardVisitor(const arma::mat& input, arma::mat& output);
|
||||
|
||||
//! Execute the Forward() function.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input parameter set.
|
||||
const arma::mat& input;
|
||||
|
||||
//! The output parameter set.
|
||||
arma::mat& output;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "forward_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/forward_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Forward() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_FORWARD_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_FORWARD_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "forward_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! ForwardVisitor visitor class.
|
||||
inline ForwardVisitor::ForwardVisitor(const arma::mat& input, arma::mat& output) :
|
||||
input(input),
|
||||
output(output)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void ForwardVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
layer->Forward(input, output);
|
||||
}
|
||||
|
||||
inline void ForwardVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_set_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Gradient() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* GradientSetVisitor update the gradient parameter given the gradient set.
|
||||
*/
|
||||
class GradientSetVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Update the gradient parameter given the gradient set.
|
||||
GradientSetVisitor(arma::mat& gradient, size_t offset = 0);
|
||||
|
||||
//! Update the gradient parameter.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The gradient set.
|
||||
arma::mat& gradient;
|
||||
|
||||
//! The gradient offset.
|
||||
size_t offset;
|
||||
|
||||
//! Update the gradient if the module implements the Gradient() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Update the gradient if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Update the gradient if the module implements the Gradient() and Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Do not update the gradient parameter if the module doesn't implement the
|
||||
//! Gradient() or Model() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, P& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "gradient_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,100 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_set_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Gradient() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "gradient_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! GradientSetVisitor visitor class.
|
||||
inline GradientSetVisitor::GradientSetVisitor(arma::mat& gradient,
|
||||
size_t offset) :
|
||||
gradient(gradient),
|
||||
offset(offset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline size_t GradientSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerGradients(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline size_t GradientSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
GradientSetVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
layer->Gradient() = arma::mat(gradient.memptr() + offset,
|
||||
layer->Parameters().n_rows, layer->Parameters().n_cols, false, false);
|
||||
|
||||
return layer->Parameters().n_elem;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
GradientSetVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
size_t modelOffset = 0;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(GradientSetVisitor(
|
||||
gradient, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
GradientSetVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
layer->Gradient() = arma::mat(gradient.memptr() + offset,
|
||||
layer->Parameters().n_rows, layer->Parameters().n_cols, false, false);
|
||||
|
||||
size_t modelOffset = layer->Parameters().n_elem;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(GradientSetVisitor(
|
||||
gradient, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
GradientSetVisitor::LayerGradients(T* /* layer */, P& /* input */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_update_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Gradient() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_UPDATE_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_UPDATE_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* GradientUpdateVisitor update the gradient parameter given the gradient set.
|
||||
*/
|
||||
class GradientUpdateVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Update the gradient parameter given the gradient set.
|
||||
GradientUpdateVisitor(arma::mat& gradient, size_t offset = 0);
|
||||
|
||||
//! Update the gradient parameter.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The gradient set.
|
||||
arma::mat& gradient;
|
||||
|
||||
//! The gradient offset.
|
||||
size_t offset;
|
||||
|
||||
//! Update the gradient if the module implements the Gradient() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Update the gradient if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Update the gradient if the module implements the Gradient() and Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Do not update the gradient parameter if the module doesn't implement the
|
||||
//! Gradient() or Model() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerGradients(T* layer, P& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "gradient_update_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,106 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_update_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Gradient() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_UPDATE_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_UPDATE_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "gradient_update_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! GradientUpdateVisitor visitor class.
|
||||
inline GradientUpdateVisitor::GradientUpdateVisitor(arma::mat& gradient,
|
||||
size_t offset) :
|
||||
gradient(gradient),
|
||||
offset(offset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline size_t GradientUpdateVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerGradients(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline size_t GradientUpdateVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
GradientUpdateVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
if (layer->Parameters().n_elem != 0)
|
||||
{
|
||||
layer->Gradient() = gradient.submat(offset, 0,
|
||||
offset + layer->Parameters().n_elem - 1, 0);;
|
||||
}
|
||||
|
||||
return layer->Parameters().n_elem;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
GradientUpdateVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
size_t modelOffset = 0;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(GradientUpdateVisitor(
|
||||
gradient, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
GradientUpdateVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
if (layer->Parameters().n_elem != 0)
|
||||
{
|
||||
layer->Gradient() = gradient.submat(offset, 0,
|
||||
offset + layer->Parameters().n_elem - 1, 0);;
|
||||
}
|
||||
|
||||
size_t modelOffset = layer->Parameters().n_elem;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(GradientUpdateVisitor(
|
||||
gradient, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
GradientUpdateVisitor::LayerGradients(T* /* layer */, P& /* input */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,89 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Gradient() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* SearchModeVisitor executes the Gradient() method of the given module using
|
||||
* the input and delta parameter.
|
||||
*/
|
||||
class GradientVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Executes the Gradient() method of the given module using the input and
|
||||
//! delta parameter.
|
||||
GradientVisitor(const arma::mat& input, const arma::mat& delta);
|
||||
|
||||
//! Executes the Gradient() method for the layer with the specified index.
|
||||
GradientVisitor(const arma::mat& input,
|
||||
const arma::mat& delta,
|
||||
const size_t index);
|
||||
|
||||
//! Executes the Gradient() method.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input set.
|
||||
const arma::mat& input;
|
||||
|
||||
//! The delta parameter.
|
||||
const arma::mat& delta;
|
||||
|
||||
//! Index of the layer to run.
|
||||
size_t index;
|
||||
|
||||
//! Indicates whether to use index or not
|
||||
bool hasIndex;
|
||||
|
||||
//! Execute the Gradient() function if the module implements the Gradient()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Execute the Gradient() function if the module implements the Gradient()
|
||||
//! and has a Run() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Do not execute the Gradient() function if the module doesn't implement
|
||||
//! the Gradient() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerGradients(T* layer, P& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "gradient_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,90 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Gradient() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "gradient_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! GradientVisitor visitor class.
|
||||
inline GradientVisitor::GradientVisitor(const arma::mat& input,
|
||||
const arma::mat& delta) :
|
||||
input(input),
|
||||
delta(delta),
|
||||
index(0),
|
||||
hasIndex(false)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
inline GradientVisitor::GradientVisitor(const arma::mat& input,
|
||||
const arma::mat& delta,
|
||||
const size_t index) :
|
||||
input(input),
|
||||
delta(delta),
|
||||
index(index),
|
||||
hasIndex(true)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void GradientVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerGradients(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void GradientVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
GradientVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
layer->Gradient(input, delta, layer->Gradient());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value &&
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value, void>::type
|
||||
GradientVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
if (!hasIndex)
|
||||
{
|
||||
layer->Gradient(input, delta, layer->Gradient());
|
||||
}
|
||||
else
|
||||
{
|
||||
layer->Gradient(input, delta, layer->Gradient(), index);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value, void>::type
|
||||
GradientVisitor::LayerGradients(T* /* layer */, P& /* input */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,60 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_zero_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Gradient() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_ZERO_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_ZERO_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/*
|
||||
* GradientZeroVisitor set the gradient to zero for the given module.
|
||||
*/
|
||||
class GradientZeroVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Set the gradient to zero for the given module.
|
||||
GradientZeroVisitor();
|
||||
|
||||
//! Set the gradient to zero.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! Set the gradient to zero if the module implements the Gradient() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value, void>::type
|
||||
LayerGradients(T* layer, arma::mat& input) const;
|
||||
|
||||
//! Do not set the gradient to zero if the module doesn't implement the
|
||||
//! Gradient() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerGradients(T* layer, P& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "gradient_zero_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,57 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/gradient_zero_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Gradient() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_GRADIENT_ZERO_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_GRADIENT_ZERO_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "gradient_zero_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! GradientZeroVisitor visitor class.
|
||||
inline GradientZeroVisitor::GradientZeroVisitor()
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void GradientZeroVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerGradients(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void GradientZeroVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasGradientCheck<T, arma::mat&(T::*)()>::value, void>::type
|
||||
GradientZeroVisitor::LayerGradients(T* layer, arma::mat& /* input */) const
|
||||
{
|
||||
layer->Gradient().zeros();
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasGradientCheck<T, P&(T::*)()>::value, void>::type
|
||||
GradientZeroVisitor::LayerGradients(T* /* layer */, P& /* input */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/input_shape_visitor.hpp
|
||||
* @author Khizir Siddiqui
|
||||
* @author Nippun Sharma
|
||||
*
|
||||
* This file provides an abstraction for the InputShape() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_INPUT_SHAPE_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_INPUT_SHAPE_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* InShapeVisitor returns the input shape a Layer expects.
|
||||
*/
|
||||
class InShapeVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Return the input shape of layer.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! If the module doesn't implement the InputShape() function return 0.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputShapeCheck<T>::value, size_t>::type
|
||||
LayerInputShape(T* layer) const;
|
||||
|
||||
//! If the module implements the InputShape() function returns the input shape.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputShapeCheck<T>::value, size_t>::type
|
||||
LayerInputShape(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "input_shape_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,53 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/input_shape_visitor_impl.hpp
|
||||
* @author Khizir Siddiqui
|
||||
* @author Nippun Sharma
|
||||
*
|
||||
* Implementation of the InputShape() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_INPUT_SHAPE_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_INPUT_SHAPE_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "input_shape_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! InShapeVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline std::size_t InShapeVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerInputShape(layer);
|
||||
}
|
||||
|
||||
inline std::size_t InShapeVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputShapeCheck<T>::value, std::size_t>::type
|
||||
InShapeVisitor::LayerInputShape(T* /* layer */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputShapeCheck<T>::value, std::size_t>::type
|
||||
InShapeVisitor::LayerInputShape(T* layer) const
|
||||
{
|
||||
return layer->InputShape();
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,65 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/load_output_parameter_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the OutputParameter() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_LOAD_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_LOAD_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* LoadOutputParameterVisitor restores the output parameter using the given
|
||||
* parameter set.
|
||||
*/
|
||||
class LoadOutputParameterVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Restore the output parameter given a parameter set.
|
||||
LoadOutputParameterVisitor(std::vector<arma::mat>& parameter);
|
||||
|
||||
//! Restore the output parameter.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameter set.
|
||||
std::vector<arma::mat>& parameter;
|
||||
|
||||
//! Restore the output parameter for a module which doesn't implement the
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
OutputParameter(T* layer) const;
|
||||
|
||||
//! Restore the output parameter for a module which implements the Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasModelCheck<T>::value, void>::type
|
||||
OutputParameter(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "load_output_parameter_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,66 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/load_output_parameter_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the OutputParameter() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_LOAD_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_LOAD_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "load_output_parameter_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! LoadOutputParameterVisitor visitor class.
|
||||
inline LoadOutputParameterVisitor::LoadOutputParameterVisitor(
|
||||
std::vector<arma::mat>& parameter) : parameter(parameter)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void LoadOutputParameterVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
OutputParameter(layer);
|
||||
}
|
||||
|
||||
inline void LoadOutputParameterVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LoadOutputParameterVisitor::OutputParameter(T* layer) const
|
||||
{
|
||||
layer->OutputParameter() = parameter.back();
|
||||
parameter.pop_back();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LoadOutputParameterVisitor::OutputParameter(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(LoadOutputParameterVisitor(parameter),
|
||||
layer->Model()[layer->Model().size() - i - 1]);
|
||||
}
|
||||
|
||||
layer->OutputParameter() = parameter.back();
|
||||
parameter.pop_back();
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,71 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/loss_visitor.hpp
|
||||
* @author Atharva Khandait
|
||||
*
|
||||
* This file provides an abstraction for the Loss() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_LOSS_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_LOSS_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* LossVisitor exposes the Loss() method of the given module.
|
||||
*/
|
||||
class LossVisitor : public boost::static_visitor<double>
|
||||
{
|
||||
public:
|
||||
//! Return the Loss.
|
||||
template<typename LayerType>
|
||||
double operator()(LayerType* layer) const;
|
||||
|
||||
double operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! Return 0 if the module doesn't implement the Loss() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasLoss<T, double(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, double>::type
|
||||
LayerLoss(T* layer) const;
|
||||
|
||||
//! Return the output height if the module implements the Loss() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasLoss<T, double(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, double>::type
|
||||
LayerLoss(T* layer) const;
|
||||
|
||||
//! Return the loss if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasLoss<T, double(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, double>::type
|
||||
LayerLoss(T* layer) const;
|
||||
|
||||
//! Return the loss if the module implements the Model() or loss() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasLoss<T, double(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, double>::type
|
||||
LayerLoss(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "loss_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,99 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/loss_visitor_impl.hpp
|
||||
* @author Atharva Khandait
|
||||
*
|
||||
* Implementation of the Loss() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_LOSS_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_LOSS_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "loss_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! LossVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline double LossVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerLoss(layer);
|
||||
}
|
||||
|
||||
inline double LossVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasLoss<T, double(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, double>::type
|
||||
LossVisitor::LayerLoss(T* /* layer */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasLoss<T, double(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, double>::type
|
||||
LossVisitor::LayerLoss(T* layer) const
|
||||
{
|
||||
return layer->Loss();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasLoss<T, double(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, double>::type
|
||||
LossVisitor::LayerLoss(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
double loss = boost::apply_visitor(LossVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (loss != 0)
|
||||
{
|
||||
return loss;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasLoss<T, double(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, double>::type
|
||||
LossVisitor::LayerLoss(T* layer) const
|
||||
{
|
||||
double loss = layer->Loss();
|
||||
|
||||
if (loss == 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
loss = boost::apply_visitor(LossVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (loss != 0)
|
||||
{
|
||||
return loss;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return loss;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_height_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the OutputHeight() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_HEIGHT_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_HEIGHT_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* OutputHeightVisitor exposes the OutputHeight() method of the given module.
|
||||
*/
|
||||
class OutputHeightVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Return the output height.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! Return 0 if the module doesn't implement the InputHeight() or Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputHeight(T* layer) const;
|
||||
|
||||
//! Return the output height if the module implements the InputHeight()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputHeight(T* layer) const;
|
||||
|
||||
//! Return the output height if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputHeight(T* layer) const;
|
||||
|
||||
//! Return the output height if the module implements the Model() or
|
||||
//! InputHeight() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputHeight(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "output_height_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,99 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_height_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the OutputHeight() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_HEIGHT_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_HEIGHT_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "output_height_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! OutputHeightVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline size_t OutputHeightVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerOutputHeight(layer);
|
||||
}
|
||||
|
||||
inline size_t OutputHeightVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
OutputHeightVisitor::LayerOutputHeight(T* /* layer */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
OutputHeightVisitor::LayerOutputHeight(T* layer) const
|
||||
{
|
||||
return layer->OutputHeight();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
OutputHeightVisitor::LayerOutputHeight(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
size_t outputHeight = boost::apply_visitor(OutputHeightVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (outputHeight != 0)
|
||||
{
|
||||
return outputHeight;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
OutputHeightVisitor::LayerOutputHeight(T* layer) const
|
||||
{
|
||||
size_t outputHeight = layer->OutputHeight();
|
||||
|
||||
if (outputHeight == 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
outputHeight = boost::apply_visitor(OutputHeightVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (outputHeight != 0)
|
||||
{
|
||||
return outputHeight;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return outputHeight;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,43 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_parameter_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the OutputParameter() function for
|
||||
* different layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* OutputParameterVisitor exposes the output parameter of the given module.
|
||||
*/
|
||||
class OutputParameterVisitor : public boost::static_visitor<arma::mat&>
|
||||
{
|
||||
public:
|
||||
//! Return the output parameter set.
|
||||
template<typename LayerType>
|
||||
arma::mat& operator()(LayerType* layer) const;
|
||||
|
||||
arma::mat& operator()(MoreTypes layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "output_parameter_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,36 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_parameter_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the OutputParameter() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "output_parameter_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! OutputParameterVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline arma::mat& OutputParameterVisitor::operator()(LayerType *layer) const
|
||||
{
|
||||
return layer->OutputParameter();
|
||||
}
|
||||
|
||||
inline arma::mat& OutputParameterVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_width_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the OutputWidth() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_WIDTH_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_WIDTH_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* OutputWidthVisitor exposes the OutputWidth() method of the given module.
|
||||
*/
|
||||
class OutputWidthVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Return the output width.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! Return 0 if the module doesn't implement the InputWidth() or Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputWidth(T* layer) const;
|
||||
|
||||
//! Return the output width if the module implements the InputWidth()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputWidth(T* layer) const;
|
||||
|
||||
//! Return the output width if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputWidth(T* layer) const;
|
||||
|
||||
//! Return the output width if the module implements the Model() or
|
||||
//! InputWidth() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerOutputWidth(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "output_width_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,99 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/output_width_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the OutputWidth() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_OUTPUT_WIDTH_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_OUTPUT_WIDTH_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "output_width_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! OutputWidthVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline size_t OutputWidthVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerOutputWidth(layer);
|
||||
}
|
||||
|
||||
inline size_t OutputWidthVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
OutputWidthVisitor::LayerOutputWidth(T* /* layer */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
OutputWidthVisitor::LayerOutputWidth(T* layer) const
|
||||
{
|
||||
return layer->OutputWidth();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
OutputWidthVisitor::LayerOutputWidth(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
size_t outputWidth = boost::apply_visitor(OutputWidthVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (outputWidth != 0)
|
||||
{
|
||||
return outputWidth;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
OutputWidthVisitor::LayerOutputWidth(T* layer) const
|
||||
{
|
||||
size_t outputWidth = layer->OutputWidth();
|
||||
|
||||
if (outputWidth == 0)
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
outputWidth = boost::apply_visitor(OutputWidthVisitor(),
|
||||
layer->Model()[layer->Model().size() - 1 - i]);
|
||||
|
||||
if (outputWidth != 0)
|
||||
{
|
||||
return outputWidth;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return outputWidth;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/parameters_set_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Parameters() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_PARAMETERS_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_PARAMETERS_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* ParametersSetVisitor update the parameters set using the given matrix.
|
||||
*/
|
||||
class ParametersSetVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Update the parameters set given the parameters matrix.
|
||||
ParametersSetVisitor(arma::mat& parameters);
|
||||
|
||||
//! Update the parameters set.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType *layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameters set.
|
||||
arma::mat& parameters;
|
||||
|
||||
//! Do not update the parameters set if the module doesn't implement the
|
||||
//! Parameters() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerParameters(T* layer, P& output) const;
|
||||
|
||||
//! Update the parameters set if the module implements the Parameters()
|
||||
//! function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerParameters(T* layer, P& output) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "parameters_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/parameters_set_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Parameters() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_PARAMETERS_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_PARAMETERS_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "parameters_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! ParametersSetVisitor visitor class.
|
||||
inline ParametersSetVisitor::ParametersSetVisitor(arma::mat& parameters) :
|
||||
parameters(parameters)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void ParametersSetVisitor::operator()(LayerType *layer) const
|
||||
{
|
||||
LayerParameters(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void ParametersSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
ParametersSetVisitor::LayerParameters(T* /* layer */, P& /* output */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
ParametersSetVisitor::LayerParameters(T* layer, P& /* output */) const
|
||||
{
|
||||
layer->Parameters() = parameters;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/parameters_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Parameters() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_PARAMETERS_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_PARAMETERS_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* ParametersVisitor exposes the parameters set of the given module and stores
|
||||
* the parameters set into the given matrix.
|
||||
*/
|
||||
class ParametersVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Store the parameters set into the given parameters matrix.
|
||||
ParametersVisitor(arma::mat& parameters);
|
||||
|
||||
//! Set the parameters set.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameters set.
|
||||
arma::mat& parameters;
|
||||
|
||||
//! Do not set the parameters set if the module doesn't implement the
|
||||
//! Parameters() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerParameters(T* layer, P& output) const;
|
||||
|
||||
//! Set the parameters set if the module implements the Parameters() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
LayerParameters(T* layer, P& output) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "parameters_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/parameters_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Parameters() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_PARAMETERS_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_PARAMETERS_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "parameters_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! ParametersVisitor visitor class.
|
||||
inline ParametersVisitor::ParametersVisitor(arma::mat& parameters) :
|
||||
parameters(parameters)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void ParametersVisitor::operator()(LayerType *layer) const
|
||||
{
|
||||
LayerParameters(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline void ParametersVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
ParametersVisitor::LayerParameters(T* /* layer */, P& /* output */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value, void>::type
|
||||
ParametersVisitor::LayerParameters(T* layer, P& /* output */) const
|
||||
{
|
||||
parameters = layer->Parameters();
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,62 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reset_cell_visitor.hpp
|
||||
* @author Sumedh Ghaisas
|
||||
*
|
||||
* Boost static visitor abstraction for calling ResetCell function on RNN cells.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RESET_CELL_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RESET_CELL_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
#include <mlpack/methods/ann/layer/layer_types.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* ResetCellVisitor executes the ResetCell() function.
|
||||
*/
|
||||
class ResetCellVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Reset the cell using the given size.
|
||||
ResetCellVisitor(const size_t size);
|
||||
|
||||
//! Execute the ResetCell() function.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
size_t size;
|
||||
|
||||
//! Execute the ResetCell() function for a module which implements
|
||||
//! the ResetCell() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasResetCellCheck<T, void(T::*)(const size_t)>::value, void>::type
|
||||
ResetCell(T* layer) const;
|
||||
|
||||
//! Do not execute the Reset() function for a module which doesn't implement
|
||||
// the Reset() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasResetCellCheck<T, void(T::*)(const size_t)>::value, void>::type
|
||||
ResetCell(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "reset_cell_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,58 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reset_cell_visitor_impl.hpp
|
||||
* @author Sumedh Ghaisas
|
||||
*
|
||||
* Implementation of the ResetCell() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RESET_CELL_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RESET_CELL_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "reset_cell_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! ResetVisitor visitor class.
|
||||
inline ResetCellVisitor::ResetCellVisitor(const size_t size) : size(size)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
//! ResetVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline void ResetCellVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
ResetCell(layer);
|
||||
}
|
||||
|
||||
inline void ResetCellVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasResetCellCheck<T, void(T::*)(const size_t)>::value, void>::type
|
||||
ResetCellVisitor::ResetCell(T* layer) const
|
||||
{
|
||||
layer->ResetCell(size);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasResetCellCheck<T, void(T::*)(const size_t)>::value, void>::type
|
||||
ResetCellVisitor::ResetCell(T* /* layer */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,75 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reset_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Reset() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RESET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RESET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* ResetVisitor executes the Reset() function.
|
||||
*/
|
||||
class ResetVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Execute the Reset() function.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! Execute the Reset() function for a module which implements the Reset()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasResetCheck<T, void(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
ResetParameter(T* layer) const;
|
||||
|
||||
//! Execute the Reset() function for a module which implements the Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasResetCheck<T, void(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
ResetParameter(T* layer) const;
|
||||
|
||||
//! Execute the Reset() function for a module which implements the Reset()
|
||||
//! and Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasResetCheck<T, void(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
ResetParameter(T* layer) const;
|
||||
|
||||
//! Do not execute the Reset() function for a module which doesn't implement
|
||||
// the Reset() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasResetCheck<T, void(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
ResetParameter(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "reset_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,80 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reset_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Reset() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RESET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RESET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "reset_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! ResetVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline void ResetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
ResetParameter(layer);
|
||||
}
|
||||
|
||||
inline void ResetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasResetCheck<T, void(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
ResetVisitor::ResetParameter(T* layer) const
|
||||
{
|
||||
layer->Reset();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasResetCheck<T, void(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
ResetVisitor::ResetParameter(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(ResetVisitor(), layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasResetCheck<T, void(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
ResetVisitor::ResetParameter(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(ResetVisitor(), layer->Model()[i]);
|
||||
}
|
||||
|
||||
layer->Reset();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasResetCheck<T, void(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
ResetVisitor::ResetParameter(T* /* layer */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,81 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reward_set_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Reward() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_REWARD_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_REWARD_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* RewardSetVisitor set the reward parameter given the reward value.
|
||||
*/
|
||||
class RewardSetVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Set the reward parameter given the reward value.
|
||||
RewardSetVisitor(const double reward);
|
||||
|
||||
//! Set the reward parameter.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The reward value.
|
||||
const double reward;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Deterministic() and Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerReward(T* layer) const;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerReward(T* layer) const;
|
||||
|
||||
//! Set the deterministic parameter if the module implements the
|
||||
//! Deterministic() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerReward(T* layer) const;
|
||||
|
||||
//! Do not set the deterministic parameter if the module doesn't implement the
|
||||
//! Deterministic() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerReward(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "reward_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,87 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/reward_set_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Reward() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_REWARD_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_REWARD_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "reward_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! RewardSetVisitor visitor class.
|
||||
inline RewardSetVisitor::RewardSetVisitor(const double reward) : reward(reward)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void RewardSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerReward(layer);
|
||||
}
|
||||
|
||||
inline void RewardSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
RewardSetVisitor::LayerReward(T* layer) const
|
||||
{
|
||||
layer->Reward() = reward;
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(RewardSetVisitor(reward),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
RewardSetVisitor::LayerReward(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(RewardSetVisitor(reward),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
RewardSetVisitor::LayerReward(T* layer) const
|
||||
{
|
||||
layer->Reward() = reward;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRewardCheck<T, double&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
RewardSetVisitor::LayerReward(T* /* input */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,83 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/run_set_visitor.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* This file provides an abstraction for the Run() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RUN_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RUN_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* RunSetVisitor set the run parameter given the
|
||||
* run value.
|
||||
*/
|
||||
class RunSetVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Set the run parameter given the current run value.
|
||||
RunSetVisitor(const bool run = true);
|
||||
|
||||
//! Set the run parameter.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The run parameter.
|
||||
const bool run;
|
||||
|
||||
//! Set the run parameter if the module implements the
|
||||
//! Run() and Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerRun(T* layer) const;
|
||||
|
||||
//! Set the run parameter if the module implements the
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
LayerRun(T* layer) const;
|
||||
|
||||
//! Set the run parameter if the module implements the
|
||||
//! Run() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerRun(T* layer) const;
|
||||
|
||||
//! Do not set the run parameter if the module doesn't implement the
|
||||
//! Run() or Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
LayerRun(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "run_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,88 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/run_set_visitor_impl.hpp
|
||||
* @author Saksham Bansal
|
||||
*
|
||||
* Implementation of the Run() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_RUN_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_RUN_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "run_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! RunSetVisitor visitor class.
|
||||
inline RunSetVisitor::RunSetVisitor(
|
||||
const bool run) : run(run)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void RunSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
LayerRun(layer);
|
||||
}
|
||||
|
||||
inline void RunSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
RunSetVisitor::LayerRun(T* layer) const
|
||||
{
|
||||
layer->Run() = run;
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(RunSetVisitor(run),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
HasModelCheck<T>::value, void>::type
|
||||
RunSetVisitor::LayerRun(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(RunSetVisitor(run),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
RunSetVisitor::LayerRun(T* layer) const
|
||||
{
|
||||
layer->Run() = run;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasRunCheck<T, bool&(T::*)(void)>::value &&
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
RunSetVisitor::LayerRun(T* /* input */) const
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/save_output_parameter_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the OutputParameter() function for
|
||||
* different layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SAVE_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SAVE_OUTPUT_PARAMETER_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* SaveOutputParameterVisitor saves the output parameter into the given
|
||||
* parameter set.
|
||||
*/
|
||||
class SaveOutputParameterVisitor : public boost::static_visitor<void>
|
||||
{
|
||||
public:
|
||||
//! Save the output parameter into the given parameter set.
|
||||
SaveOutputParameterVisitor(std::vector<arma::mat>& parameter);
|
||||
|
||||
//! Save the output parameter.
|
||||
template<typename LayerType>
|
||||
void operator()(LayerType* layer) const;
|
||||
|
||||
void operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameter set.
|
||||
std::vector<arma::mat>& parameter;
|
||||
|
||||
//! Save the output parameter for a module which doesn't implement the
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
OutputParameter(T* layer) const;
|
||||
|
||||
//! Save the output parameter for a module which implements the Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasModelCheck<T>::value, void>::type
|
||||
OutputParameter(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "save_output_parameter_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,64 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/save_output_parameter_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the OutputParameter() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SAVE_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SAVE_OUTPUT_PARAMETER_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "load_output_parameter_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! SaveOutputParameterVisitor visitor class.
|
||||
inline SaveOutputParameterVisitor::SaveOutputParameterVisitor(
|
||||
std::vector<arma::mat>& parameter) : parameter(parameter)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline void SaveOutputParameterVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
OutputParameter(layer);
|
||||
}
|
||||
|
||||
inline void SaveOutputParameterVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasModelCheck<T>::value, void>::type
|
||||
SaveOutputParameterVisitor::OutputParameter(T* layer) const
|
||||
{
|
||||
parameter.push_back(layer->OutputParameter());
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasModelCheck<T>::value, void>::type
|
||||
SaveOutputParameterVisitor::OutputParameter(T* layer) const
|
||||
{
|
||||
parameter.push_back(layer->OutputParameter());
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(SaveOutputParameterVisitor(parameter),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/set_input_height_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the InputHeight() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SET_INPUT_HEIGHT_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SET_INPUT_HEIGHT_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* SetInputHeightVisitor updates the input height parameter with the given input
|
||||
* height.
|
||||
*/
|
||||
class SetInputHeightVisitor : public boost::static_visitor<bool>
|
||||
{
|
||||
public:
|
||||
//! Update the input height parameter with the given input height.
|
||||
SetInputHeightVisitor(const size_t inputHeight = 0, const bool reset = false);
|
||||
|
||||
//! Update the input height parameter.
|
||||
template<typename LayerType>
|
||||
bool operator()(LayerType* layer) const;
|
||||
|
||||
bool operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input height parameter.
|
||||
size_t inputHeight;
|
||||
|
||||
//! If set reset the height parameter if already set.
|
||||
bool reset;
|
||||
|
||||
//! Do nothing if the module doesn't implement the InputHeight() or Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
LayerInputHeight(T* layer) const;
|
||||
|
||||
//! Update the input height if the module implements the InputHeight()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
LayerInputHeight(T* layer) const;
|
||||
|
||||
//! Update the input height if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
LayerInputHeight(T* layer) const;
|
||||
|
||||
//! Update the input height if the module implements the InputHeight() or
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
LayerInputHeight(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "set_input_height_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,102 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/set_input_height_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the InputHeight() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SET_INPUT_HEIGHT_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SET_INPUT_HEIGHT_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "set_input_height_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! SetInputHeightVisitor visitor class.
|
||||
inline SetInputHeightVisitor::SetInputHeightVisitor(const size_t inputHeight,
|
||||
const bool reset) :
|
||||
inputHeight(inputHeight),
|
||||
reset(reset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline bool SetInputHeightVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerInputHeight(layer);
|
||||
}
|
||||
|
||||
inline bool SetInputHeightVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
SetInputHeightVisitor::LayerInputHeight(T* /* layer */) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
SetInputHeightVisitor::LayerInputHeight(T* layer) const
|
||||
{
|
||||
if (layer->InputHeight() == 0 || reset)
|
||||
{
|
||||
layer->InputHeight() = inputHeight;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
SetInputHeightVisitor::LayerInputHeight(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(SetInputHeightVisitor(inputHeight, reset),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputHeight<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
SetInputHeightVisitor::LayerInputHeight(T* layer) const
|
||||
{
|
||||
if (layer->InputHeight() == 0 || reset)
|
||||
{
|
||||
layer->InputHeight() = inputHeight;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(SetInputHeightVisitor(inputHeight, reset),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,83 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/set_input_width_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the InputWidth() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SET_INPUT_WIDTH_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SET_INPUT_WIDTH_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* SetInputWidthVisitor updates the input width parameter with the given input
|
||||
* width.
|
||||
*/
|
||||
class SetInputWidthVisitor : public boost::static_visitor<bool>
|
||||
{
|
||||
public:
|
||||
//! Update the input width parameter with the given input width.
|
||||
SetInputWidthVisitor(const size_t inputWidth = 0, const bool reset = false);
|
||||
|
||||
//! Update the input width parameter.
|
||||
template<typename LayerType>
|
||||
bool operator()(LayerType* layer) const;
|
||||
|
||||
bool operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The input width parameter.
|
||||
size_t inputWidth;
|
||||
|
||||
//! If set reset the height parameter if already set.
|
||||
bool reset;
|
||||
|
||||
//! Do nothing if the module doesn't implement the InputWidth() or Model()
|
||||
//! function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
LayerInputWidth(T* layer) const;
|
||||
|
||||
//! Update the input width if the module implements the InputWidth() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
LayerInputWidth(T* layer) const;
|
||||
|
||||
//! Update the input width if the module implements the Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
LayerInputWidth(T* layer) const;
|
||||
|
||||
//! Update the input width if the module implements the InputWidth() or
|
||||
//! Model() function.
|
||||
template<typename T>
|
||||
typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
LayerInputWidth(T* layer) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "set_input_width_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,102 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/set_input_width_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the InputWidth() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_SET_INPUT_WIDTH_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_SET_INPUT_WIDTH_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "set_input_width_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! SetInputWidthVisitor visitor class.
|
||||
inline SetInputWidthVisitor::SetInputWidthVisitor(const size_t inputWidth,
|
||||
const bool reset) :
|
||||
inputWidth(inputWidth),
|
||||
reset(reset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline bool SetInputWidthVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerInputWidth(layer);
|
||||
}
|
||||
|
||||
inline bool SetInputWidthVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
SetInputWidthVisitor::LayerInputWidth(T* /* layer */) const
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, bool>::type
|
||||
SetInputWidthVisitor::LayerInputWidth(T* layer) const
|
||||
{
|
||||
if (layer->InputWidth() == 0 || reset)
|
||||
{
|
||||
layer->InputWidth() = inputWidth;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
!HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
SetInputWidthVisitor::LayerInputWidth(T* layer) const
|
||||
{
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(SetInputWidthVisitor(inputWidth, reset),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
inline typename std::enable_if<
|
||||
HasInputWidth<T, size_t&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, bool>::type
|
||||
SetInputWidthVisitor::LayerInputWidth(T* layer) const
|
||||
{
|
||||
if (layer->InputWidth() == 0 || reset)
|
||||
{
|
||||
layer->InputWidth() = inputWidth;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
boost::apply_visitor(SetInputWidthVisitor(inputWidth, reset),
|
||||
layer->Model()[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,82 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/weight_set_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the Weight() function for different
|
||||
* layers and automatically directs any parameter to the right layer type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_WEIGHT_SET_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_WEIGHT_SET_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* WeightSetVisitor update the module parameters given the parameters set.
|
||||
*/
|
||||
class WeightSetVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Update the parameters given the parameters set and offset.
|
||||
WeightSetVisitor(arma::mat& weight, const size_t offset = 0);
|
||||
|
||||
//! Update the parameters set.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! The parameters set.
|
||||
arma::mat& weight;
|
||||
|
||||
//! The parameters offset.
|
||||
const size_t offset;
|
||||
|
||||
//! Do not update the parameters if the module doesn't implement the
|
||||
//! Parameters() or Model() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P&& input) const;
|
||||
|
||||
//! Update the parameters if the module implements the Model() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P&& input) const;
|
||||
|
||||
//! Update the parameters if the module implements the Parameters() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P&& input) const;
|
||||
|
||||
//! Update the parameters if the module implements the Model() and
|
||||
//! Parameters() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P&& input) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "weight_set_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,100 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/weight_set_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the Weight() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_WEIGHT_SET_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_WEIGHT_SET_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "weight_set_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! WeightSetVisitor visitor class.
|
||||
inline WeightSetVisitor::WeightSetVisitor(arma::mat& weight,
|
||||
const size_t offset) :
|
||||
weight(weight),
|
||||
offset(offset)
|
||||
{
|
||||
/* Nothing to do here. */
|
||||
}
|
||||
|
||||
template<typename LayerType>
|
||||
inline size_t WeightSetVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerSize(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline size_t WeightSetVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
WeightSetVisitor::LayerSize(T* /* layer */, P&& /*output */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
WeightSetVisitor::LayerSize(T* layer, P&& /*output */) const
|
||||
{
|
||||
size_t modelOffset = 0;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(WeightSetVisitor(
|
||||
weight, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
WeightSetVisitor::LayerSize(T* layer, P&& /* output */) const
|
||||
{
|
||||
layer->Parameters() = arma::mat(weight.memptr() + offset,
|
||||
layer->Parameters().n_rows, layer->Parameters().n_cols, false, false);
|
||||
|
||||
return layer->Parameters().n_elem;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
WeightSetVisitor::LayerSize(T* layer, P&& /* output */) const
|
||||
{
|
||||
layer->Parameters() = arma::mat(weight.memptr() + offset,
|
||||
layer->Parameters().n_rows, layer->Parameters().n_cols, false, false);
|
||||
|
||||
size_t modelOffset = layer->Parameters().n_elem;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
modelOffset += boost::apply_visitor(WeightSetVisitor(
|
||||
weight, modelOffset + offset), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return modelOffset;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -1,76 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/weight_size_visitor.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* This file provides an abstraction for the WeightSize() function for
|
||||
* different layers and automatically directs any parameter to the right layer
|
||||
* type.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_WEIGHT_SIZE_VISITOR_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_WEIGHT_SIZE_VISITOR_HPP
|
||||
|
||||
#include <mlpack/methods/ann/layer/layer_traits.hpp>
|
||||
|
||||
#include <boost/variant.hpp>
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
/**
|
||||
* WeightSizeVisitor returns the number of weights of the given module.
|
||||
*/
|
||||
class WeightSizeVisitor : public boost::static_visitor<size_t>
|
||||
{
|
||||
public:
|
||||
//! Return the number of weights.
|
||||
template<typename LayerType>
|
||||
size_t operator()(LayerType* layer) const;
|
||||
|
||||
size_t operator()(MoreTypes layer) const;
|
||||
|
||||
private:
|
||||
//! If the module doesn't implement the Parameters() or Model() function
|
||||
//! return 0.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P& output) const;
|
||||
|
||||
//! Return the number of parameters if the module implements the Model()
|
||||
//! function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P& output) const;
|
||||
|
||||
//! Return the number of parameters if the module implements the Parameters()
|
||||
//! function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P& output) const;
|
||||
|
||||
//! Return the accumulated number of parameters if the module implements the
|
||||
//! Parameters() and Model() function.
|
||||
template<typename T, typename P>
|
||||
typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
LayerSize(T* layer, P& output) const;
|
||||
};
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
// Include implementation.
|
||||
#include "weight_size_visitor_impl.hpp"
|
||||
|
||||
#endif
|
||||
@@ -1,84 +0,0 @@
|
||||
/**
|
||||
* @file methods/ann/visitor/weight_size_visitor_impl.hpp
|
||||
* @author Marcus Edel
|
||||
*
|
||||
* Implementation of the WeightSize() function layer abstraction.
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
#ifndef MLPACK_METHODS_ANN_VISITOR_WEIGHT_SIZE_VISITOR_IMPL_HPP
|
||||
#define MLPACK_METHODS_ANN_VISITOR_WEIGHT_SIZE_VISITOR_IMPL_HPP
|
||||
|
||||
// In case it hasn't been included yet.
|
||||
#include "weight_size_visitor.hpp"
|
||||
|
||||
namespace mlpack {
|
||||
namespace ann {
|
||||
|
||||
//! WeightSizeVisitor visitor class.
|
||||
template<typename LayerType>
|
||||
inline size_t WeightSizeVisitor::operator()(LayerType* layer) const
|
||||
{
|
||||
return LayerSize(layer, layer->OutputParameter());
|
||||
}
|
||||
|
||||
inline size_t WeightSizeVisitor::operator()(MoreTypes layer) const
|
||||
{
|
||||
return layer.apply_visitor(*this);
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
WeightSizeVisitor::LayerSize(T* /* layer */, P& /* output */) const
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
!HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
WeightSizeVisitor::LayerSize(T* layer, P& /* output */) const
|
||||
{
|
||||
size_t weights = 0;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
weights += boost::apply_visitor(WeightSizeVisitor(), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return weights;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
!HasModelCheck<T>::value, size_t>::type
|
||||
WeightSizeVisitor::LayerSize(T* layer, P& /* output */) const
|
||||
{
|
||||
return layer->Parameters().n_elem;
|
||||
}
|
||||
|
||||
template<typename T, typename P>
|
||||
inline typename std::enable_if<
|
||||
HasParametersCheck<T, P&(T::*)()>::value &&
|
||||
HasModelCheck<T>::value, size_t>::type
|
||||
WeightSizeVisitor::LayerSize(T* layer, P& /* output */) const
|
||||
{
|
||||
size_t weights = layer->Parameters().n_elem;
|
||||
for (size_t i = 0; i < layer->Model().size(); ++i)
|
||||
{
|
||||
weights += boost::apply_visitor(WeightSizeVisitor(), layer->Model()[i]);
|
||||
}
|
||||
|
||||
return weights;
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
} // namespace mlpack
|
||||
|
||||
#endif
|
||||
@@ -3,7 +3,7 @@ include(CTest)
|
||||
# mlpack test executable.
|
||||
add_executable(mlpack_test
|
||||
EXCLUDE_FROM_ALL
|
||||
# activation_functions_test.cpp
|
||||
# activation_functions_test.cpp
|
||||
adaboost_test.cpp
|
||||
akfn_test.cpp
|
||||
aknn_test.cpp
|
||||
@@ -59,7 +59,6 @@ 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
|
||||
lmnn_test.cpp
|
||||
@@ -95,7 +94,7 @@ add_executable(mlpack_test
|
||||
random_test.cpp
|
||||
randomized_svd_test.cpp
|
||||
range_search_test.cpp
|
||||
rbm_network_test.cpp
|
||||
# rbm_network_test.cpp
|
||||
rectangle_tree_test.cpp
|
||||
recurrent_network_test.cpp
|
||||
# rnn_reber_test.cpp
|
||||
|
||||
@@ -1,235 +0,0 @@
|
||||
/**
|
||||
* @file tests/ann_visitor_test.cpp
|
||||
*
|
||||
* Tests for testing visitors in ANN's of mlpack.
|
||||
*
|
||||
* 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 <mlpack/methods/ann/visitor/bias_set_visitor.hpp>
|
||||
#include <mlpack/methods/ann/visitor/weight_set_visitor.hpp>
|
||||
#include <mlpack/methods/ann/visitor/reset_visitor.hpp>
|
||||
|
||||
#include "catch.hpp"
|
||||
#include "test_catch_tools.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace mlpack::ann;
|
||||
|
||||
/**
|
||||
* Test that the BiasSetVisitor works properly.
|
||||
*/
|
||||
TEST_CASE("BiasSetVisitorTest", "[ANNVisitorTest]")
|
||||
{
|
||||
LayerTypes<> linear = new Linear<>(10, 10);
|
||||
|
||||
arma::mat layerWeights(110, 1);
|
||||
layerWeights.zeros();
|
||||
|
||||
ResetVisitor resetVisitor;
|
||||
|
||||
boost::apply_visitor(WeightSetVisitor(layerWeights, 0), linear);
|
||||
|
||||
boost::apply_visitor(resetVisitor, linear);
|
||||
|
||||
arma::mat weight = {"1 2 3 4 5 6 7 8 9 10"};
|
||||
|
||||
size_t biasSize = boost::apply_visitor(BiasSetVisitor(weight, 0), linear);
|
||||
|
||||
REQUIRE(biasSize == 10);
|
||||
|
||||
arma::mat input(10, 1), output;
|
||||
input.randu();
|
||||
|
||||
boost::apply_visitor(ForwardVisitor(input, output), linear);
|
||||
|
||||
REQUIRE(arma::accu(output) == 55);
|
||||
|
||||
boost::apply_visitor(DeleteVisitor(), linear);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check correctness of WeightSize() for a layer.
|
||||
*/
|
||||
void CheckCorrectnessOfWeightSize(LayerTypes<>& layer)
|
||||
{
|
||||
size_t weightSize = boost::apply_visitor(WeightSizeVisitor(),
|
||||
layer);
|
||||
|
||||
arma::mat parameters;
|
||||
boost::apply_visitor(ParametersVisitor(parameters), layer);
|
||||
|
||||
REQUIRE(weightSize == parameters.n_elem);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSetVisitor works properly.
|
||||
*/
|
||||
TEST_CASE("WeightSetVisitorTest", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> linear = new Linear<>(randomSize, randomSize);
|
||||
|
||||
arma::mat layerWeights(randomSize * randomSize + randomSize, 1);
|
||||
layerWeights.zeros();
|
||||
|
||||
size_t setWeights = boost::apply_visitor(WeightSetVisitor(layerWeights, 0),
|
||||
linear);
|
||||
|
||||
REQUIRE(setWeights == randomSize * randomSize + randomSize);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for linear layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForLinearLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> linearLayer = new Linear<>(randomInSize, randomOutSize);
|
||||
|
||||
CheckCorrectnessOfWeightSize(linearLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for concat layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForConcatLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
LayerTypes<> concatLayer = new Concat<>();
|
||||
|
||||
CheckCorrectnessOfWeightSize(concatLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for fast lstm layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForFastLSTMLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> fastLSTMLayer = new FastLSTM<>(randomInSize, randomOutSize);
|
||||
|
||||
CheckCorrectnessOfWeightSize(fastLSTMLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for Add layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForAddLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> addLayer = new Add<>(randomOutSize);
|
||||
|
||||
CheckCorrectnessOfWeightSize(addLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for Atrous Convolution Layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForAtrousConvolutionLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelWidth = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelHeight = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> atrousConvLayer = new AtrousConvolution<>(randomInSize,
|
||||
randomOutSize, randomKernelWidth, randomKernelHeight);
|
||||
|
||||
CheckCorrectnessOfWeightSize(atrousConvLayer);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for Convolution layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForConvLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelWidth = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelHeight = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> convLayer = new Convolution<>(randomInSize, randomOutSize,
|
||||
randomKernelWidth, randomKernelHeight);
|
||||
CheckCorrectnessOfWeightSize(convLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for BatchNorm layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForBatchNormLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> batchNorm = new BatchNorm<>(randomSize);
|
||||
CheckCorrectnessOfWeightSize(batchNorm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for LSTM layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForLSTMLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> lstm = new LSTM<>(randomInSize, randomOutSize);
|
||||
CheckCorrectnessOfWeightSize(lstm);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for Transposed Convolution layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForTransposedConvLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelWidth = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomKernelHeight = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> transposedConvLayer = new TransposedConvolution<>(randomInSize,
|
||||
randomOutSize, randomKernelWidth, randomKernelHeight);
|
||||
|
||||
CheckCorrectnessOfWeightSize(transposedConvLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for noisy linear layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForNoisyLinearLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomInSize = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomOutSize = arma::randi(arma::distr_param(1, 100));
|
||||
|
||||
LayerTypes<> noisyLinearLayer = new NoisyLinear<>(randomInSize,
|
||||
randomOutSize);
|
||||
|
||||
CheckCorrectnessOfWeightSize(noisyLinearLayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that WeightSizeVisitor works properly for Multihead Attention layer.
|
||||
*/
|
||||
TEST_CASE("WeightSizeVisitorTestForMultiheadAttentionLayer", "[ANNVisitorTest]")
|
||||
{
|
||||
size_t randomtgtSeqLen = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomsrcSeqLen = arma::randi(arma::distr_param(1, 100));
|
||||
size_t randomembedDim = 768;
|
||||
size_t randomnumHeads = 12;
|
||||
|
||||
LayerTypes<> MultiheadAttentionLayer = new MultiheadAttention<>(
|
||||
randomtgtSeqLen, randomsrcSeqLen, randomembedDim, randomnumHeads);
|
||||
|
||||
CheckCorrectnessOfWeightSize(MultiheadAttentionLayer);
|
||||
}
|
||||
@@ -1,160 +0,0 @@
|
||||
/**
|
||||
* @file tests/layer_names_test.cpp
|
||||
* @author Sreenik Seal
|
||||
*
|
||||
* Tests for testing the string representation of
|
||||
* layers in mlpack's ANN module.
|
||||
*
|
||||
* 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 <mlpack/methods/ann/layer_names.hpp>
|
||||
|
||||
#include "catch.hpp"
|
||||
|
||||
using namespace mlpack;
|
||||
using namespace ann;
|
||||
|
||||
/**
|
||||
* Test if the LayerNameVisitor works properly.
|
||||
*/
|
||||
TEST_CASE("LayerNameVisitorTest", "[LayerNamesTest]")
|
||||
{
|
||||
LayerTypes<> atrousConvolution = new AtrousConvolution<>();
|
||||
LayerTypes<> alphaDropout = new AlphaDropout<>();
|
||||
LayerTypes<> batchNorm = new BatchNorm<>();
|
||||
LayerTypes<> constant = new Constant<>();
|
||||
LayerTypes<> convolution = new Convolution<>();
|
||||
LayerTypes<> dropConnect = new DropConnect<>();
|
||||
LayerTypes<> dropout = new Dropout<>();
|
||||
LayerTypes<> flexibleReLU = new FlexibleReLU<>();
|
||||
LayerTypes<> layerNorm = new LayerNorm<>();
|
||||
LayerTypes<> linear = new Linear<>();
|
||||
LayerTypes<> linearNoBias = new LinearNoBias<>();
|
||||
LayerTypes<> maxPooling = new MaxPooling<>();
|
||||
LayerTypes<> meanPooling = new MeanPooling<>();
|
||||
LayerTypes<> multiplyConstant = new MultiplyConstant<>();
|
||||
LayerTypes<> reLULayer = new ReLULayer<>();
|
||||
LayerTypes<> transposedConvolution = new TransposedConvolution<>();
|
||||
LayerTypes<> identityLayer = new IdentityLayer<>();
|
||||
LayerTypes<> tanHLayer = new TanHLayer<>();
|
||||
LayerTypes<> eLU = new ELU<>();
|
||||
LayerTypes<> hardTanH = new HardTanH<>();
|
||||
LayerTypes<> leakyReLU = new LeakyReLU<>();
|
||||
LayerTypes<> pReLU = new PReLU<>();
|
||||
LayerTypes<> sigmoidLayer = new SigmoidLayer<>();
|
||||
LayerTypes<> logSoftMax = new LogSoftMax<>();
|
||||
LayerTypes<> lstmLayer = new LSTM<>(100, 10);
|
||||
LayerTypes<> creluLayer = new CReLU<>();
|
||||
LayerTypes<> highwayLayer = new Highway<>();
|
||||
LayerTypes<> gruLayer = new GRU<>();
|
||||
LayerTypes<> glimpseLayer = new Glimpse<>();
|
||||
LayerTypes<> fastlstmLayer = new FastLSTM<>();
|
||||
LayerTypes<> weightnormLayer = new WeightNorm<>(new IdentityLayer<>());
|
||||
|
||||
// Bilinear interpolation is not yet supported by the string converter.
|
||||
LayerTypes<> unsupportedLayer = new BilinearInterpolation<>();
|
||||
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
atrousConvolution) == "atrousconvolution");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
alphaDropout) == "alphadropout");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
batchNorm) == "batchnorm");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
constant) == "constant");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
convolution) == "convolution");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
dropConnect) == "dropconnect");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
dropout) == "dropout");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
flexibleReLU) == "flexiblerelu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
layerNorm) == "layernorm");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
linear) == "linear");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
linearNoBias) == "linearnobias");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
maxPooling) == "maxpooling");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
meanPooling) == "meanpooling");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
multiplyConstant) == "multiplyconstant");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
reLULayer) == "relu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
transposedConvolution) == "transposedconvolution");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
identityLayer) == "identity");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
tanHLayer) == "tanh");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
eLU) == "elu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
hardTanH) == "hardtanh");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
leakyReLU) == "leakyrelu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
pReLU) == "prelu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
sigmoidLayer) == "sigmoid");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
logSoftMax) == "logsoftmax");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
unsupportedLayer) == "unsupported");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
lstmLayer) == "lstm");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
creluLayer) == "crelu");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
highwayLayer) == "highway");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
gruLayer) == "gru");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
glimpseLayer) == "glimpse");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
fastlstmLayer) == "fastlstm");
|
||||
REQUIRE(boost::apply_visitor(LayerNameVisitor(),
|
||||
weightnormLayer) == "weightnorm");
|
||||
// Delete all instances.
|
||||
boost::apply_visitor(DeleteVisitor(), atrousConvolution);
|
||||
boost::apply_visitor(DeleteVisitor(), alphaDropout);
|
||||
boost::apply_visitor(DeleteVisitor(), batchNorm);
|
||||
boost::apply_visitor(DeleteVisitor(), constant);
|
||||
boost::apply_visitor(DeleteVisitor(), convolution);
|
||||
boost::apply_visitor(DeleteVisitor(), dropConnect);
|
||||
boost::apply_visitor(DeleteVisitor(), dropout);
|
||||
boost::apply_visitor(DeleteVisitor(), flexibleReLU);
|
||||
boost::apply_visitor(DeleteVisitor(), layerNorm);
|
||||
boost::apply_visitor(DeleteVisitor(), linear);
|
||||
boost::apply_visitor(DeleteVisitor(), linearNoBias);
|
||||
boost::apply_visitor(DeleteVisitor(), maxPooling);
|
||||
boost::apply_visitor(DeleteVisitor(), meanPooling);
|
||||
boost::apply_visitor(DeleteVisitor(), multiplyConstant);
|
||||
boost::apply_visitor(DeleteVisitor(), reLULayer);
|
||||
boost::apply_visitor(DeleteVisitor(), transposedConvolution);
|
||||
boost::apply_visitor(DeleteVisitor(), identityLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), tanHLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), eLU);
|
||||
boost::apply_visitor(DeleteVisitor(), hardTanH);
|
||||
boost::apply_visitor(DeleteVisitor(), leakyReLU);
|
||||
boost::apply_visitor(DeleteVisitor(), pReLU);
|
||||
boost::apply_visitor(DeleteVisitor(), sigmoidLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), logSoftMax);
|
||||
boost::apply_visitor(DeleteVisitor(), unsupportedLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), lstmLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), creluLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), highwayLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), gruLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), glimpseLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), fastlstmLayer);
|
||||
boost::apply_visitor(DeleteVisitor(), weightnormLayer);
|
||||
}
|
||||
Reference in New Issue
Block a user