add bias_set_visitor

This commit is contained in:
walragatver
2019-08-14 18:47:38 +05:30
parent 1174e84eeb
commit 3d2d71ced5
8 changed files with 192 additions and 0 deletions
@@ -165,6 +165,9 @@ class AtrousConvolution
//! Modify the output height.
size_t& OutputHeight() { return outputHeight; }
//! Modify the bias weights of the layer.
arma::mat& Bias() { return bias; }
/**
* Serialize the layer
*/
@@ -161,6 +161,9 @@ class Convolution
//! Modify the output height.
size_t& OutputHeight() { return outputHeight; }
//! Modify the bias weights of the layer.
arma::mat& Bias() { return bias; }
/**
* Serialize the layer
*/
@@ -112,6 +112,10 @@ HAS_MEM_FUNC(Loss, HasLoss);
// can use with SFINAE to catch when a type has a Run() function.
HAS_MEM_FUNC(Run, HasRunCheck);
// This gives us a HasBiasCheck<T, U> type (where U is a function pointer) we
// can use with SFINAE to catch when a type has a Bias() function.
HAS_MEM_FUNC(Bias, HasBiasCheck);
} // namespace ann
} // namespace mlpack
+3
View File
@@ -117,6 +117,9 @@ class Linear
//! Modify the gradient.
OutputDataType& Gradient() { return gradient; }
//! Modify the bias weights of the layer.
arma::mat& Bias() { return bias; }
/**
* Serialize the layer
*/
@@ -162,6 +162,9 @@ class TransposedConvolution
//! Modify the output height.
size_t& OutputHeight() { return outputHeight; }
//! Modify the bias weights of the layer.
arma::mat& Bias() { return bias; }
/**
* Serialize the layer
*/
@@ -5,6 +5,8 @@ set(SOURCES
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
@@ -0,0 +1,80 @@
/**
* @file 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 update 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;
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
@@ -0,0 +1,94 @@
/**
* @file 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(std::move(weight)),
offset(offset)
{
/* Nothing to do here. */
}
template<typename LayerType>
inline size_t BiasSetVisitor::operator()(LayerType* layer) const
{
return LayerSize(layer);
}
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(
std::move(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(
std::move(weight), modelOffset + offset), layer->Model()[i]);
}
return modelOffset;
}
} // namespace ann
} // namespace mlpack
#endif