Adding some changes in implementation
This commit is contained in:
@@ -82,8 +82,7 @@ template<typename InputDataType,
|
||||
class Linear;
|
||||
|
||||
template<typename InputDataType,
|
||||
typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
typename OutputDataType>
|
||||
class RBF;
|
||||
|
||||
template<typename InputDataType,
|
||||
@@ -216,7 +215,7 @@ using MoreTypes = boost::variant<
|
||||
Subview<arma::mat, arma::mat>*,
|
||||
VRClassReward<arma::mat, arma::mat>*,
|
||||
VirtualBatchNorm<arma::mat, arma::mat>*,
|
||||
RBF<arma::mat, arma::mat, NoRegularizer>*
|
||||
RBF<arma::mat, arma::mat>*
|
||||
>;
|
||||
|
||||
template <typename... CustomLayers>
|
||||
|
||||
@@ -37,8 +37,7 @@ namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template <
|
||||
typename InputDataType = arma::mat,
|
||||
typename OutputDataType = arma::mat,
|
||||
typename RegularizerType = NoRegularizer
|
||||
typename OutputDataType = arma::mat
|
||||
>
|
||||
class RBF
|
||||
{
|
||||
@@ -55,8 +54,7 @@ class RBF
|
||||
*/
|
||||
RBF(const size_t inSize,
|
||||
const size_t outSize,
|
||||
arma::mat& centres,
|
||||
RegularizerType regularizer = RegularizerType());
|
||||
arma::mat& centres);
|
||||
|
||||
/**
|
||||
* Reset the layer parameter.
|
||||
@@ -101,9 +99,6 @@ class RBF
|
||||
//! Modify the output parameter.
|
||||
OutputDataType& OutputParameter() { return outputParameter; }
|
||||
//! Get the parameters.
|
||||
OutputDataType const& Parameters() const { return weights; }
|
||||
//! Modify the parameters.
|
||||
OutputDataType& Parameters() { return weights; }
|
||||
|
||||
//! Get the input parameter.
|
||||
InputDataType const& InputParameter() const { return inputParameter; }
|
||||
@@ -121,9 +116,6 @@ class RBF
|
||||
//! Modify the delta.
|
||||
OutputDataType& Delta() { return delta; }
|
||||
|
||||
//! Modify the bias weights of the layer.
|
||||
arma::mat& Sigmas() { return sigmas; }
|
||||
|
||||
/**
|
||||
* Serialize the layer.
|
||||
*/
|
||||
@@ -149,9 +141,6 @@ class RBF
|
||||
//! Locally-stored the output distances of the shape.
|
||||
OutputDataType distances;
|
||||
|
||||
//! Locally-stored weight object.
|
||||
OutputDataType weights;
|
||||
|
||||
//! Locally-stored reset parameter used to initialize the layer once.
|
||||
bool reset;
|
||||
|
||||
@@ -161,8 +150,6 @@ class RBF
|
||||
//! Locally-stored number of output units.
|
||||
size_t outSize;
|
||||
|
||||
//! Locally-stored regularizer object.
|
||||
RegularizerType regularizer;
|
||||
}; // class RBF
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -17,9 +17,8 @@
|
||||
namespace mlpack {
|
||||
namespace ann /** Artificial Neural Network. */ {
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
RBF<InputDataType, OutputDataType, RegularizerType>::RBF() :
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
RBF<InputDataType, OutputDataType>::RBF() :
|
||||
inSize(0),
|
||||
outSize(0),
|
||||
reset(false)
|
||||
@@ -27,32 +26,26 @@ RBF<InputDataType, OutputDataType, RegularizerType>::RBF() :
|
||||
// Nothing to do here.
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
RBF<InputDataType, OutputDataType, RegularizerType>::RBF(
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
RBF<InputDataType, OutputDataType>::RBF(
|
||||
const size_t inSize,
|
||||
const size_t outSize,
|
||||
arma::mat& centres,
|
||||
RegularizerType regularizer) :
|
||||
arma::mat& centres) :
|
||||
inSize(inSize),
|
||||
outSize(outSize),
|
||||
centres(centres),
|
||||
regularizer(regularizer),
|
||||
reset(false)
|
||||
{
|
||||
weights.set_size(outSize * inSize + outSize, 1);
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
void RBF<InputDataType, OutputDataType, RegularizerType>::Reset()
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
void RBF<InputDataType, OutputDataType>::Reset()
|
||||
{
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void RBF<InputDataType, OutputDataType, RegularizerType>::Forward(
|
||||
void RBF<InputDataType, OutputDataType>::Forward(
|
||||
const arma::Mat<eT>& input,
|
||||
arma::Mat<eT>& output)
|
||||
{
|
||||
@@ -73,17 +66,12 @@ void RBF<InputDataType, OutputDataType, RegularizerType>::Forward(
|
||||
2), 0), 0.5).t();
|
||||
}
|
||||
|
||||
sigmas = arma::mean(distances, 1);
|
||||
arma::mat betas = 1 / 2 * arma::pow(sigmas, 2);
|
||||
distances = arma::pow(distances, 2);
|
||||
distances = distances.each_col() % betas;
|
||||
output = arma::exp(-1 * distances);
|
||||
output = distances;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename eT>
|
||||
void RBF<InputDataType, OutputDataType, RegularizerType>::Backward(
|
||||
void RBF<InputDataType, OutputDataType>::Backward(
|
||||
const arma::Mat<eT>& /* input */,
|
||||
const arma::Mat<eT>& gy,
|
||||
arma::Mat<eT>& g)
|
||||
@@ -91,21 +79,15 @@ void RBF<InputDataType, OutputDataType, RegularizerType>::Backward(
|
||||
g = centres.t() * gy;
|
||||
}
|
||||
|
||||
template<typename InputDataType, typename OutputDataType,
|
||||
typename RegularizerType>
|
||||
template<typename InputDataType, typename OutputDataType>
|
||||
template<typename Archive>
|
||||
void RBF<InputDataType, OutputDataType, RegularizerType>::serialize(
|
||||
void RBF<InputDataType, OutputDataType>::serialize(
|
||||
Archive& ar,
|
||||
const unsigned int /* version */)
|
||||
{
|
||||
ar & BOOST_SERIALIZATION_NVP(distances);
|
||||
ar & BOOST_SERIALIZATION_NVP(sigmas);
|
||||
ar & BOOST_SERIALIZATION_NVP(centres);
|
||||
|
||||
// This is inefficient, but we have to allocate this memory so that
|
||||
// WeightSetVisitor gets the right size.
|
||||
if (Archive::is_loading::value)
|
||||
weights.set_size(outSize * inSize + outSize, 1);
|
||||
}
|
||||
|
||||
} // namespace ann
|
||||
|
||||
@@ -687,7 +687,7 @@ BOOST_AUTO_TEST_CASE(RBFNetworkTest)
|
||||
model.Add<Linear<> >(10, 2);
|
||||
model1.Add<LogSoftMax<> >();
|
||||
// Vanilla neural net with logistic activation function.
|
||||
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
|
||||
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.3);
|
||||
}
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END();
|
||||
|
||||
Reference in New Issue
Block a user