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

Chain put #5149

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions src/shogun/base/SGObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,13 @@ class SGObject: public std::enable_shared_from_this<SGObject>
class X = typename std::enable_if_t<is_sg_base<T>::value>,
class Z = void>
#ifdef SWIG
void put(const std::string& name, std::shared_ptr<T> value)
std::shared_ptr<SGObject> put(const std::string& name, std::shared_ptr<T> value)
#else
void put(std::string_view name, std::shared_ptr<T> value)
std::shared_ptr<SGObject> put(std::string_view name, std::shared_ptr<T> value)
#endif
{
put(Tag<std::shared_ptr<T>>(name), value);
return shared_from_this();
}

/** Typed appender for an object class parameter of a Shogun base class
Expand Down Expand Up @@ -494,15 +495,17 @@ class SGObject: public std::enable_shared_from_this<SGObject>
!std::is_base_of_v<
SGObject, typename std::remove_pointer_t<T>>, T>>
#ifdef SWIG
void put(const std::string& name, T value)
std::shared_ptr<SGObject> put(const std::string& name, T value)
#else
void put(std::string_view name, T value)
std::shared_ptr<SGObject> put(std::string_view name, T value)
#endif
{
if constexpr (std::is_enum<T>::value)
put(Tag<machine_int_t>(name), static_cast<machine_int_t>(value));
else
put(Tag<T>(name), value);

return shared_from_this();
}

#ifndef SWIG
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -506,9 +506,7 @@ TEST_F(GaussianProcessClassificationTest, train)
std::make_shared<MultiLaplaceInferenceMethod>();
std::shared_ptr<Kernel> k = std::make_shared<GaussianKernel>();
std::shared_ptr<MeanFunction> mean = std::make_shared<ZeroMean>();
inf->put("kernel", k);
inf->put("mean_function", mean);
inf->put("likelihood_model", likelihood1);
inf->put("kernel", k)->put("mean_function", mean)->put("likelihood_model", likelihood1);
auto gpc1 = std::make_shared<GaussianProcessClassification>(inf);
gpc1->set_labels(multi_labels_train);
gpc1->train(features_train);
Expand All @@ -519,9 +517,7 @@ TEST_F(GaussianProcessClassificationTest, train)
std::make_shared<SoftMaxLikelihood>();
std::shared_ptr<Inference> inf2 =
std::make_shared<MultiLaplaceInferenceMethod>();
inf2->put("kernel", k);
inf2->put("mean_function", mean);
inf2->put("likelihood_model", likelihood2);
inf2->put("kernel", k)->put("mean_function", mean)->put("likelihood_model", likelihood2);
auto gpc2 = std::make_shared<GaussianProcessClassification>(inf2);
gpc2->set_labels(multi_labels_train);
gpc2->put("seed", 1);
Expand Down
58 changes: 29 additions & 29 deletions tests/unit/neuralnets/Autoencoder_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ TEST(Autoencoder, train)
auto decoding_layer = std::make_shared<NeuralLinearLayer>(num_features);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(num_features, hidden_layer, decoding_layer);
ae.put("seed", seed);
auto ae = std::make_shared<Autoencoder>(num_features, hidden_layer, decoding_layer);
ae->put("seed", seed);

auto features = std::make_shared<DenseFeatures<float64_t>>(data);

ae.train(features);
ae->train(features);

auto reconstructed = ae.reconstruct(features);
auto reconstructed = ae->reconstruct(features);
SGMatrix<float64_t> reconstructed_data = reconstructed->get_feature_matrix();

float64_t avg_diff = 0;
Expand All @@ -97,11 +97,11 @@ TEST(Autoencoder, contractive_linear)
auto decoding_layer = std::make_shared<NeuralLinearLayer>(10);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(10, hidden_layer, decoding_layer);
ae.put("seed", seed);
ae.set_contraction_coefficient(10.0);
auto ae = std::make_shared<Autoencoder>(10, hidden_layer, decoding_layer);
ae->put("seed", seed);
ae->set_contraction_coefficient(10.0);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -117,11 +117,11 @@ TEST(Autoencoder, contractive_rectified_linear)
auto decoding_layer = std::make_shared<NeuralLinearLayer>(10);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(10, hidden_layer, decoding_layer);
ae.put("seed", seed);
ae.set_contraction_coefficient(10.0);
auto ae = std::make_shared<Autoencoder>(10, hidden_layer, decoding_layer);
ae->put("seed", seed);
ae->set_contraction_coefficient(10.0);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -137,13 +137,13 @@ TEST(Autoencoder, contractive_logistic)
auto decoding_layer = std::make_shared<NeuralLinearLayer>(10);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(10, hidden_layer, decoding_layer);
ae.put("seed", seed);
ae.initialize_neural_network();
auto ae = std::make_shared<Autoencoder>(10, hidden_layer, decoding_layer);
ae->put("seed", seed);
ae->initialize_neural_network();

ae.set_contraction_coefficient(1.0);
ae->set_contraction_coefficient(1.0);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -161,10 +161,10 @@ TEST(Autoencoder, convolutional)
auto decoding_layer = std::make_shared<NeuralConvolutionalLayer>(CMAF_IDENTITY, 3, 1,1, 1,1, 1,1);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(w,h,3,hidden_layer,decoding_layer);
ae.put("seed", seed);
auto ae = std::make_shared<Autoencoder>(w, h, 3, hidden_layer, decoding_layer);
ae->put("seed", seed);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -182,10 +182,10 @@ TEST(Autoencoder, convolutional_with_pooling)
auto decoding_layer = std::make_shared<NeuralConvolutionalLayer>(CMAF_IDENTITY, 3, 1,1, 1,1, 1,1);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(w,h,3,hidden_layer,decoding_layer);
ae.put("seed", seed);
auto ae = std::make_shared<Autoencoder>(w, h, 3, hidden_layer, decoding_layer);
ae->put("seed", seed);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -202,10 +202,10 @@ TEST(Autoencoder, convolutional_with_stride)
auto decoding_layer = std::make_shared<NeuralConvolutionalLayer>(CMAF_IDENTITY, 3, 1,1, 1,1, 1,1);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(w,h,3,hidden_layer,decoding_layer);
ae.put("seed", seed);
auto ae = std::make_shared<Autoencoder>(w, h, 3, hidden_layer, decoding_layer);
ae->put("seed", seed);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}

/** Tests gradients computed using backpropagation against gradients computed
Expand All @@ -223,8 +223,8 @@ TEST(Autoencoder, convolutional_with_stride_and_pooling)
auto decoding_layer = std::make_shared<NeuralConvolutionalLayer>(CMAF_IDENTITY, 3, 1,1, 1,1, 1,1);
hidden_layer->put("seed", seed);
decoding_layer->put("seed", seed);
Autoencoder ae(w,h,3,hidden_layer,decoding_layer);
ae.put("seed", seed);
auto ae = std::make_shared<Autoencoder>(w, h, 3, hidden_layer, decoding_layer);
ae->put("seed", seed);

EXPECT_NEAR(ae.check_gradients(), 0.0, tolerance);
EXPECT_NEAR(ae->check_gradients(), 0.0, tolerance);
}
12 changes: 4 additions & 8 deletions tests/unit/neuralnets/NeuralNetwork_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ TEST(NeuralNetwork, binary_classification)
layers.push_back(std::make_shared<NeuralLogisticLayer>(1));

auto network = std::make_shared<NeuralNetwork>(layers);
network->put("seed", seed);
network->put("sigma", 0.1);
network->put("seed", seed)->put("sigma", 0.1);

network->set_epsilon(1e-8);

Expand Down Expand Up @@ -334,8 +333,7 @@ TEST(NeuralNetwork, multiclass_classification)
layers.push_back(std::make_shared<NeuralLogisticLayer>(2));

auto network = std::make_shared<NeuralNetwork>(layers);
network->put("seed", seed);
network->put("sigma", 0.1);
network->put("seed", seed)->put("sigma", 0.1);

network->set_epsilon(1e-8);

Expand Down Expand Up @@ -381,8 +379,7 @@ TEST(NeuralNetwork, regression)
layers.push_back(std::make_shared<NeuralLinearLayer>(1));

auto network = std::make_shared<NeuralNetwork>(layers);
network->put("seed", seed);
network->put("sigma", 1e-6);
network->put("seed", seed)->put("sigma", 1e-6);

network->set_epsilon(1e-6);

Expand Down Expand Up @@ -431,8 +428,7 @@ TEST(NeuralNetwork, gradient_descent)
layers.push_back(std::make_shared<NeuralLogisticLayer>(1));

auto network = std::make_shared<NeuralNetwork>(layers);
network->put("seed", seed);
network->put("sigma", 0.1);
network->put("seed", seed)->put("sigma", 0.1);

network->set_optimization_method(NNOM_GRADIENT_DESCENT);
network->set_gd_learning_rate(10.0);
Expand Down
94 changes: 47 additions & 47 deletions tests/unit/neuralnets/RBM_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,34 +47,34 @@ TEST(RBM, gibbs_sampling)
int32_t num_visible = 5;
int32_t num_hidden = 6;

RBM rbm(num_hidden, num_visible, RBMVUT_BINARY);
rbm.put("seed", seed);
rbm.initialize_neural_network();
rbm.set_batch_size(1);
auto rbm = std::make_shared<RBM>(num_hidden, num_visible, RBMVUT_BINARY);
rbm->put("seed", seed);
rbm->initialize_neural_network();
rbm->set_batch_size(1);

for (int32_t i=0; i<rbm.get_weights().num_rows*rbm.get_weights().num_cols; i++)
rbm.get_weights()[i] = i*1.0e-2;
for (int32_t i=0; i<rbm->get_weights().num_rows*rbm->get_weights().num_cols; i++)
rbm->get_weights()[i] = i*1.0e-2;

for (int32_t i=0; i<rbm.get_hidden_bias().vlen; i++)
rbm.get_hidden_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_hidden_bias().vlen; i++)
rbm->get_hidden_bias()[i] = i*1.0e-1;

for (int32_t i=0; i<rbm.get_visible_bias().vlen; i++)
rbm.get_visible_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_visible_bias().vlen; i++)
rbm->get_visible_bias()[i] = i*1.0e-1;

for (int32_t i=0; i<num_visible; i++)
rbm.visible_state[i] = i*1.0e-3;
rbm->visible_state[i] = i*1.0e-3;

SGVector<float64_t> probs(num_visible);
probs.zero();

rbm.sample(1000);
rbm->sample(1000);

for (int32_t i=0; i<1000; i++)
{
rbm.sample(100);
rbm->sample(100);

for (int32_t j=0; j<num_visible; j++)
probs[j] += rbm.visible_state[j]/1000;
probs[j] += rbm->visible_state[j]/1000;
}

// generated using scikit-learn
Expand All @@ -91,25 +91,25 @@ TEST(RBM, free_energy_binary)
int32_t num_hidden = 6;
int32_t batch_size = 3;

RBM rbm(num_hidden, num_visible, RBMVUT_BINARY);
rbm.put("seed", seed);
rbm.initialize_neural_network();
auto rbm = std::make_shared<RBM>(num_hidden, num_visible, RBMVUT_BINARY);
rbm->put("seed", seed);
rbm->initialize_neural_network();

for (int32_t i=0; i<rbm.get_weights().num_rows*rbm.get_weights().num_cols; i++)
rbm.get_weights()[i] = i*1.0e-2;
for (int32_t i=0; i<rbm->get_weights().num_rows*rbm->get_weights().num_cols; i++)
rbm->get_weights()[i] = i*1.0e-2;

for (int32_t i=0; i<rbm.get_hidden_bias().vlen; i++)
rbm.get_hidden_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_hidden_bias().vlen; i++)
rbm->get_hidden_bias()[i] = i*1.0e-1;

for (int32_t i=0; i<rbm.get_visible_bias().vlen; i++)
rbm.get_visible_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_visible_bias().vlen; i++)
rbm->get_visible_bias()[i] = i*1.0e-1;

SGMatrix<float64_t> V(num_visible, batch_size);
for (int32_t i=0; i<V.num_rows*V.num_cols; i++)
V[i] = i*1e-3;

// generated using scikit-learn
EXPECT_NEAR(-5.0044376228, rbm.free_energy(V), 1e-6);
EXPECT_NEAR(-5.0044376228, rbm->free_energy(V), 1e-6);
}

TEST(RBM, free_energy_gradients)
Expand All @@ -122,30 +122,30 @@ TEST(RBM, free_energy_gradients)
std::mt19937_64 prng(seed);
UniformRealDistribution<float64_t> uniform_real_dist(0.0, 1.0);

RBM rbm(num_hidden);
rbm.put("seed", seed);
rbm.add_visible_group(4, RBMVUT_BINARY);
rbm.add_visible_group(6, RBMVUT_GAUSSIAN);
rbm.add_visible_group(5, RBMVUT_BINARY);
rbm.initialize_neural_network();
auto rbm = std::make_shared<RBM>(num_hidden);
rbm->put("seed", seed);
rbm->add_visible_group(4, RBMVUT_BINARY);
rbm->add_visible_group(6, RBMVUT_GAUSSIAN);
rbm->add_visible_group(5, RBMVUT_BINARY);
rbm->initialize_neural_network();

SGMatrix<float64_t> V(num_visible, batch_size);
for (int32_t i=0; i<V.num_rows*V.num_cols; i++)
V[i] = uniform_real_dist(prng) < 0.7;

SGVector<float64_t> gradients(rbm.get_num_parameters());
rbm.free_energy_gradients(V, gradients);
SGVector<float64_t> gradients(rbm->get_num_parameters());
rbm->free_energy_gradients(V, gradients);

SGVector<float64_t> params = rbm.get_parameters();
SGVector<float64_t> gradients_numerical(rbm.get_num_parameters());
SGVector<float64_t> params = rbm->get_parameters();
SGVector<float64_t> gradients_numerical(rbm->get_num_parameters());
float64_t epsilon = 1e-9;
for (int32_t i=0; i<rbm.get_num_parameters(); i++)
for (int32_t i=0; i<rbm->get_num_parameters(); i++)
{
params[i] += epsilon;
float64_t energy_plus =rbm.free_energy(V);
float64_t energy_plus =rbm->free_energy(V);

params[i] -= 2*epsilon;
float64_t energy_minus =rbm.free_energy(V);
float64_t energy_minus =rbm->free_energy(V);

params[i] += epsilon;

Expand All @@ -163,26 +163,26 @@ TEST(RBM, pseudo_likelihood_binary)
int32_t num_hidden = 6;
int32_t batch_size = 1;

RBM rbm(num_hidden, num_visible, RBMVUT_BINARY);
rbm.put("seed", seed);
rbm.initialize_neural_network();
auto rbm = std::make_shared<RBM>(num_hidden, num_visible, RBMVUT_BINARY);
rbm->put("seed", seed);
rbm->initialize_neural_network();

for (int32_t i=0; i<rbm.get_weights().num_rows*rbm.get_weights().num_cols; i++)
rbm.get_weights()[i] = i*1.0e-2;
for (int32_t i=0; i<rbm->get_weights().num_rows*rbm->get_weights().num_cols; i++)
rbm->get_weights()[i] = i*1.0e-2;

for (int32_t i=0; i<rbm.get_hidden_bias().vlen; i++)
rbm.get_hidden_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_hidden_bias().vlen; i++)
rbm->get_hidden_bias()[i] = i*1.0e-1;

for (int32_t i=0; i<rbm.get_visible_bias().vlen; i++)
rbm.get_visible_bias()[i] = i*1.0e-1;
for (int32_t i=0; i<rbm->get_visible_bias().vlen; i++)
rbm->get_visible_bias()[i] = i*1.0e-1;

SGMatrix<float64_t> V(num_visible, batch_size);
for (int32_t i=0; i<V.num_rows*V.num_cols; i++)
V[i] = i > 2;

float64_t pl = 0;
for (int32_t i=0; i<10000; i++)
pl += rbm.pseudo_likelihood(V)/10000;
pl += rbm->pseudo_likelihood(V)/10000;

// generated using scikit-learn
EXPECT_NEAR(-3.3698, pl, 0.02);
Expand Down