Skip to content

Commit

Permalink
Adding changes in implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
geekypathak21 committed May 14, 2020
1 parent 0a8e983 commit c6a8f87
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 53 deletions.
1 change: 0 additions & 1 deletion src/mlpack/methods/ann/layer/layer_types.hpp
Expand Up @@ -252,7 +252,6 @@ using LayerTypes = boost::variant<
LeakyReLU<arma::mat, arma::mat>*,
CReLU<arma::mat, arma::mat>*,
Linear<arma::mat, arma::mat, NoRegularizer>*,
RBF<arma::mat, arma::mat>*,
LinearNoBias<arma::mat, arma::mat, NoRegularizer>*,
LogSoftMax<arma::mat, arma::mat>*,
Lookup<arma::mat, arma::mat>*,
Expand Down
22 changes: 15 additions & 7 deletions src/mlpack/methods/ann/layer/radial_basis_function_impl.hpp
Expand Up @@ -41,19 +41,27 @@ void RBF<InputDataType, OutputDataType>::Forward(
const arma::Mat<eT>& input,
arma::Mat<eT>& output)
{
centres = arma::mat(outSize, input.n_rows, arma::fill::randu);
centres = arma::mat(input.n_rows, outSize, arma::fill::randu);
centres = arma::normcdf(centres, 0, 1);
sigmas = arma::ones(1, outSize);
sigmas = arma::ones(outSize, 1);
arma::cube x = arma::cube(input.n_rows, outSize, input.n_cols);

distances = arma::mat(outSize, input.n_cols);
for (size_t i = 0; i < input.n_cols; i++)
{
x.slice(i).each_col() = input.col(i);
}

arma::cube c = arma::cube(input.n_rows, outSize, input.n_cols);
c.each_slice() = centres;

distances = arma::mat(input.n_rows, input.n_cols);

for (size_t i = 0; i < outSize; i++)
for (size_t i = 0; i < input.n_cols; i++)
{
arma::mat temp = centres.each_col(i)
distances.col(i) = arma::pow(arma::sum(
arma::pow((
temp),
2), 1), 0.5).t() * sigmas(i);
x.slice(i) - c.slice(i)),
2), 1), 0.5);
}

output = distances;
Expand Down
90 changes: 45 additions & 45 deletions src/mlpack/tests/feedforward_network_test.cpp
Expand Up @@ -624,20 +624,20 @@ BOOST_AUTO_TEST_CASE(OptimizerTest)
/**
* Train the RBF network on a larger dataset.
*/
//BOOST_AUTO_TEST_CASE(RBFNetworkTest)
//{
// // Load the dataset.
// arma::mat trainData;
// data::Load("thyroid_train.csv", trainData, true);
//
// arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
// trainData.shed_row(trainData.n_rows - 1);
//
// arma::mat testData;
// data::Load("thyroid_test.csv", testData, true);
//
// arma::mat testLabels = testData.row(testData.n_rows - 1);
// testData.shed_row(testData.n_rows - 1);
BOOST_AUTO_TEST_CASE(RBFNetworkTest)
{
// Load the dataset.
arma::mat trainData;
data::Load("thyroid_train.csv", trainData, true);

arma::mat trainLabels = trainData.row(trainData.n_rows - 1);
trainData.shed_row(trainData.n_rows - 1);

arma::mat testData;
data::Load("thyroid_test.csv", testData, true);

arma::mat testLabels = testData.row(testData.n_rows - 1);
testData.shed_row(testData.n_rows - 1);

/*
* Construct a feed forward network with trainData.n_rows input nodes,
Expand All @@ -652,36 +652,36 @@ BOOST_AUTO_TEST_CASE(OptimizerTest)
* | | | | | | | |
* +-----+ +--+--+ +-----+ +-----+
*/
//
// FFN<NegativeLogLikelihood<> > model;
// model.Add<RBF<> >(trainData.n_cols, 8);
// model.Add<GaussianFunctionLayer<> >();
// model.Add<Linear<> >(trainData.n_rows, 8);
// model.Add<Linear<> >(8, 3);
// model.Add<LogSoftMax<> >();
//
// TestNetwork<>(model, trainData, trainLabels, testData, testLabels, 10, 0.1);
// arma::mat dataset;
// dataset.load("mnist_first250_training_4s_and_9s.arm");
//
// // Normalize each point since these are images.
// for (size_t i = 0; i < dataset.n_cols; ++i)
// {
// dataset.col(i) /= norm(dataset.col(i), 2);
// }
//
// arma::mat labels = arma::zeros(1, dataset.n_cols);
// labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
// labels += 1;
//
// FFN<NegativeLogLikelihood<> > model1;
// model1.Add<RBF<> >(dataset.n_cols, 10);
// model1.Add<GaussianFunctionLayer<> >();
// model1.Add<Linear<> >(dataset.n_rows, 10);
// model.Add<Linear<> >(10, 2);
// model1.Add<LogSoftMax<> >();
// // Vanilla neural net with logistic activation function.
// TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
//}

FFN<NegativeLogLikelihood<> > model;
model.Add<RBF<> >(trainData.n_cols, 8);
model.Add<GaussianFunctionLayer<> >();
model.Add<Linear<> >(trainData.n_rows, 8);
model.Add<Linear<> >(8, 3);
model.Add<LogSoftMax<> >();

TestNetwork<>(model, trainData, trainLabels, testData, testLabels, 10, 0.1);
arma::mat dataset;
dataset.load("mnist_first250_training_4s_and_9s.arm");

// Normalize each point since these are images.
for (size_t i = 0; i < dataset.n_cols; ++i)
{
dataset.col(i) /= norm(dataset.col(i), 2);
}

arma::mat labels = arma::zeros(1, dataset.n_cols);
labels.submat(0, labels.n_cols / 2, 0, labels.n_cols - 1).fill(1);
labels += 1;

FFN<NegativeLogLikelihood<> > model1;
model1.Add<RBF<> >(dataset.n_cols, 10);
model1.Add<GaussianFunctionLayer<> >();
model1.Add<Linear<> >(dataset.n_rows, 10);
model.Add<Linear<> >(10, 2);
model1.Add<LogSoftMax<> >();
// Vanilla neural net with logistic activation function.
TestNetwork<>(model1, dataset, labels, dataset, labels, 10, 0.2);
}

BOOST_AUTO_TEST_SUITE_END();

0 comments on commit c6a8f87

Please sign in to comment.