Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Adapt C_ReLU, ReLU6, FlexibleReLU #3445

Merged
merged 26 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b7d45df
Update fastmks.md
AdarshSantoria Feb 6, 2023
0101186
Update fastmks.md
AdarshSantoria Feb 6, 2023
6197b40
Merge branch 'mlpack:master' into master
AdarshSantoria Feb 7, 2023
592164b
add c_relu, relu6, f_relu
AdarshSantoria Mar 10, 2023
5246adc
Merge branch 'mlpack:master' into master
AdarshSantoria Apr 3, 2023
d8771bf
Merge branch 'mlpack:master' into master
AdarshSantoria Apr 7, 2023
fcdd90f
Merge branch 'mlpack:master' into master
AdarshSantoria May 29, 2023
c556ce9
Update src/mlpack/methods/ann/layer/c_relu_impl.hpp
AdarshSantoria May 29, 2023
8c30a86
Update relu6_impl.hpp
AdarshSantoria May 30, 2023
bad7b7f
Update relu6_impl.hpp
AdarshSantoria May 30, 2023
7df3bbc
Add ComputeOutputDimensions() to CReLU and fix test setup.
rcurtin May 30, 2023
c957ba6
Shrink sizes of arrays in tests.
rcurtin May 30, 2023
2b82352
Use arma::clamp() instead of min() and max().
rcurtin May 30, 2023
572f2a1
Some other minor fixes to tests.
rcurtin May 30, 2023
abd59c4
Merge pull request #1 from rcurtin/notadapted
AdarshSantoria Jun 3, 2023
a8d3b21
Merge branch 'mlpack:master' into notadapted
AdarshSantoria Jun 3, 2023
88221ed
Merge branch 'master' into notadapted
AdarshSantoria Jun 3, 2023
30cda61
Update HISTORY.md
AdarshSantoria Jun 3, 2023
178f795
fix style
AdarshSantoria Jun 4, 2023
4756cc7
fix style
AdarshSantoria Jun 4, 2023
3c10d01
fix style
AdarshSantoria Jun 4, 2023
db3da8d
fix style
AdarshSantoria Jun 4, 2023
27727e6
Merge branch 'master' into notadapted
AdarshSantoria Jun 9, 2023
80274e4
Update HISTORY.md
AdarshSantoria Jun 9, 2023
9149729
Update HISTORY.md
AdarshSantoria Jun 9, 2023
0846c15
Update HISTORY.md
rcurtin Jun 12, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ namespace mlpack {
* }
* @endcode
*
* @tparam InputType The type of the layer's inputs. The layer automatically
* cast inputs to this type (Default: arma::mat).
* @tparam OutputType The type of the computation which also causes the output
* to also be in this type. The type also allows the computation and weight
* type to differ from the input type (Default: arma::mat).
* @tparam MatType Matrix representation to accept as input and allows the
* computation and weight type to differ from the input type
* (Default: arma::mat).
*/
template<typename InputType = arma::mat, typename OutputType = arma::mat>
class CReLUType : public Layer<InputType, OutputType>
template<typename MatType = arma::mat>
class CReLUType : public Layer<MatType>
{
public:
//! Create the CReLU object.
Expand All @@ -55,6 +53,18 @@ class CReLUType : public Layer<InputType, OutputType>
//! Clone the CReLUType object. This handles polymorphism correctly.
CReLUType* Clone() const { return new CReLUType(*this); }

// Virtual destructor.
virtual ~CReLUType() { }

//! Copy the given CReLUType.
CReLUType(const CReLUType& other);
//! Take ownership of the given CReLUType.
CReLUType(CReLUType&& other);
//! Copy the given CReLUType.
CReLUType& operator=(const CReLUType& other);
//! Take ownership of the given CReLUType.
CReLUType& operator=(CReLUType&& other);

/**
* Ordinary feed forward pass of a neural network, evaluating the function
* f(x) by propagating the activity forward through f.
Expand All @@ -63,7 +73,7 @@ class CReLUType : public Layer<InputType, OutputType>
* @param input Input data used for evaluating the specified function.
* @param output Resulting output activation.
*/
void Forward(const InputType& input, OutputType& output);
void Forward(const MatType& input, MatType& output);

/**
* Ordinary feed backward pass of a neural network, calculating the function
Expand All @@ -74,7 +84,7 @@ class CReLUType : public Layer<InputType, OutputType>
* @param gy The backpropagated error.
* @param g The calculated gradient.
*/
void Backward(const InputType& input, const OutputType& gy, OutputType& g);
void Backward(const MatType& input, const MatType& gy, MatType& g);

//! Serialize the layer.
template<typename Archive>
Expand All @@ -84,7 +94,7 @@ class CReLUType : public Layer<InputType, OutputType>
// Convenience typedefs.

rcurtin marked this conversation as resolved.
Show resolved Hide resolved
// Standard CReLU layer.
typedef CReLUType<arma::mat, arma::mat> CReLU;
typedef CReLUType<arma::mat> CReLU;

} // namespace mlpack

Expand Down
95 changes: 95 additions & 0 deletions src/mlpack/methods/ann/layer/c_relu_impl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* @file methods/ann/layer/c_relu_impl.hpp
* @author Jeffin Sam
*
* Implementation of CReLU 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_LAYER_C_RELU_IMPL_HPP
#define MLPACK_METHODS_ANN_LAYER_C_RELU_IMPL_HPP

// In case it hasn't yet been included.
#include "c_relu.hpp"

namespace mlpack {

template<typename MatType>
CReLUType<MatType>::CReLUType() :
Layer<MatType>()
{
// Nothing to do here.
}

template<typename MatType>
CReLUType<MatType>::CReLUType(
const CReLUType& other) :
Layer<MatType>(other)
{
// Nothing to do here.
}

template<typename MatType>
CReLUType<MatType>::CReLUType(
CReLUType&& other) :
Layer<MatType>(std::move(other))
{
// Nothing to do here.
}

template<typename MatType>
CReLUType<MatType>&
CReLUType<MatType>::operator=(const CReLUType& other)
{
if (&other != this)
{
Layer<MatType>::operator=(other);
}

return *this;
}

template<typename MatType>
CReLUType<MatType>&
CReLUType<MatType>::operator=(CReLUType&& other)
{
if (&other != this)
{
Layer<MatType>::operator=(std::move(other));
}

return *this;
}

template<typename MatType>
void CReLUType<MatType>::Forward(
const MatType& input, MatType& output)
{
output = arma::join_cols(arma::max(input, 0.0 * input), arma::max(
(-1 * input), 0.0 * input));
AdarshSantoria marked this conversation as resolved.
Show resolved Hide resolved
}

template<typename MatType>
void CReLUType<MatType>::Backward(
const MatType& input, const MatType& gy, MatType& g)
{
MatType temp = gy % (input >= 0.0);
g = temp.rows(0, (input.n_rows / 2 - 1)) - temp.rows(input.n_rows / 2,
(input.n_rows - 1));
}

template<typename MatType>
template<typename Archive>
void CReLUType<MatType>::serialize(
Archive& ar,
const uint32_t /* version */)
{
ar(cereal::base_class<Layer<MatType>>(this));
}

} // namespace mlpack

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
* 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_LAYER_FLEXIBLERELU_HPP
#define MLPACK_METHODS_ANN_LAYER_FLEXIBLERELU_HPP
#ifndef MLPACK_METHODS_ANN_LAYER_FLEXIBLE_RELU_HPP
#define MLPACK_METHODS_ANN_LAYER_FLEXIBLE_RELU_HPP

#include <mlpack/prereqs.hpp>

Expand Down Expand Up @@ -47,14 +47,12 @@ namespace mlpack {
* }
* @endcode
*
* @tparam InputType The type of the layer's inputs. The layer automatically
* cast inputs to this type (Default: arma::mat).
* @tparam OutputType The type of the computation which also causes the output
* to also be in this type. The type also allows the computation and weight
* type to differ from the input type (Default: arma::mat).
* @tparam MatType Matrix representation to accept as input and allows the
* computation and weight type to differ from the input type
* (Default: arma::mat).
*/
template<typename InputType = arma::mat, typename OutputType = arma::mat>
class FlexibleReLUType : public Layer<InputType, OutputType>
template<typename MatType = arma::mat>
class FlexibleReLUType : public Layer<MatType>
{
public:
/**
Expand All @@ -69,11 +67,33 @@ class FlexibleReLUType : public Layer<InputType, OutputType>
//! Clone the FlexibleReLUType object. This handles polymorphism correctly.
FlexibleReLUType* Clone() const { return new FlexibleReLUType(*this); }

// Virtual destructor.
virtual ~FlexibleReLUType() { }

//! Copy the given FlexibleReLUType.
FlexibleReLUType(const FlexibleReLUType& other);
//! Take ownership of the given FlexibleReLUType.
FlexibleReLUType(FlexibleReLUType&& other);
//! Copy the given FlexibleReLUType.
FlexibleReLUType& operator=(const FlexibleReLUType& other);
//! Take ownership of the given FlexibleReLUType.
FlexibleReLUType& operator=(FlexibleReLUType&& other);

/**
* Reset the layer parameter (alpha). The method is called to
* assign the allocated memory to the learnable layer parameter.
*/
void SetWeights(typename OutputType::elem_type* weightsPtr);
void SetWeights(typename MatType::elem_type* weightsPtr);

/**
* Initialize the weight matrix of the layer.
*
* @param W Weight matrix to initialize.
* @param elements Number of elements.
*/
void CustomInitialize(
MatType& W,
const size_t elements);

/**
* Ordinary feed forward pass of a neural network, evaluating the function
Expand All @@ -82,7 +102,7 @@ class FlexibleReLUType : public Layer<InputType, OutputType>
* @param input Input data used for evaluating the specified function.
* @param output Resulting output activation.
*/
void Forward(const InputType& input, OutputType& output);
void Forward(const MatType& input, MatType& output);

/**
* Ordinary feed backward pass of a neural network, calculating the function
Expand All @@ -93,7 +113,7 @@ class FlexibleReLUType : public Layer<InputType, OutputType>
* @param gy The backpropagated error.
* @param g The calculated gradient.
*/
void Backward(const InputType& input, const OutputType& gy, OutputType& g);
void Backward(const MatType& input, const MatType& gy, MatType& g);

/**
* Calculate the gradient using the output delta and the input activation.
Expand All @@ -102,21 +122,22 @@ class FlexibleReLUType : public Layer<InputType, OutputType>
* @param error The calculated error.
* @param gradient The calculated gradient.
*/
void Gradient(const InputType& input,
const OutputType& error,
OutputType& gradient);
void Gradient(const MatType& input,
const MatType& error,
MatType& gradient);

//! Get the parameters.
OutputType const& Parameters() const { return alpha; }
MatType const& Parameters() const { return alpha; }
//! Modify the parameters.
OutputType& Parameters() { return alpha; }
MatType& Parameters() { return alpha; }

//! Get the parameter controlling the range of the ReLU function.
const double& Alpha() const { return alpha; }
//! Modify the parameter controlling the range of the ReLU function.
double& Alpha() { return alpha; }

const size_t WeightSize() const { return 1; }
//! Get size of weights.
size_t WeightSize() const { return 1; }

/**
* Serialize the layer.
Expand All @@ -126,19 +147,16 @@ class FlexibleReLUType : public Layer<InputType, OutputType>

private:
//! Parameter object.
OutputType alpha;
MatType alpha;

//! Parameter controlling the range of the ReLU function.
double userAlpha;

//! Whether or not a forward pass has ever been performed.
bool initialized;
}; // class FlexibleReLUType

// Convenience typedefs.

// Standard flexible ReLU layer.
typedef FlexibleReLUType<arma::mat, arma::mat> FlexibleReLU;
typedef FlexibleReLUType<arma::mat> FlexibleReLU;

} // namespace mlpack

Expand Down