Skip to content
Snippets Groups Projects
Commit 3fa17637 authored by Erik Strand's avatar Erik Strand
Browse files

Use appropriate log in gradient descent app

parent 0ca1aa19
No related branches found
No related tags found
No related merge requests found
add_executable(compare_convergence
compare_convergence.cpp
)
target_link_libraries(compare_convergence optimization_lib cma-es)
target_link_libraries(compare_convergence optimization_lib)
......@@ -4,7 +4,7 @@ target_link_libraries(optimization_lib INTERFACE shared_settings Eigen3::Eigen)
if (VISUALIZE)
target_compile_definitions(optimization_lib INTERFACE VISUALIZE)
target_link_libraries(optimization_lib INTERFACE json)
endif()
target_link_libraries(optimization_lib INTERFACE cma-es json)
add_subdirectory(optimizers)
......@@ -3,7 +3,5 @@ add_executable(gradient_descent
)
target_link_libraries(gradient_descent optimization_lib clara)
if (VISUALIZE)
make_plot_target(gradient_descent 2d ARGS -d 2 -l 0.0015 -n 10000)
make_plot_target(gradient_descent 10d ARGS -d 10 -l 0.0005 -n 10000)
endif()
#ifndef OPTIMIZATION_GRADIENT_DESCENT_LOGS_EVERYTHING_H
#define OPTIMIZATION_GRADIENT_DESCENT_LOGS_EVERYTHING_H
#include "objectives/samples.h"
#include "objectives/samples_vis.h"
#include "utils/eigen_json.h"
#include "utils/vector.h"
#include "utils/vis_only.h"
#include "objectives/samples.h"
#ifdef VISUALIZE
#include <vector>
#include "json.hpp"
#include "objectives/samples_vis.h"
#endif
namespace optimization {
......@@ -19,39 +16,20 @@ namespace optimization {
template <int32_t N>
struct GradientDescentLogEverything {
template <typename Objective>
void initialize(Objective const&) VIS_ONLY_METHOD;
void initialize(Objective const&) { objective_name = Objective::name; }
void push_back(
VectorNs<N> const& point,
Scalar value,
VectorNs<N> const& gradient
) VIS_ONLY_METHOD;
void clear() VIS_ONLY_METHOD;
) {
samples.emplace_back(point, value, gradient);
}
void clear();
#ifdef VISUALIZE
std::string objective_name;
std::vector<GradientSample<VectorNs<N>>> samples;
#endif
};
#ifdef VISUALIZE
//..................................................................................................
template <int32_t N>
template <typename Objective>
void GradientDescentLogEverything<N>::initialize(Objective const&) {
objective_name = Objective::name;
}
//..................................................................................................
template <int32_t N>
void GradientDescentLogEverything<N>::push_back(
VectorNs<N> const& point,
Scalar value,
VectorNs<N> const& gradient
) {
samples.emplace_back(point, value, gradient);
}
//--------------------------------------------------------------------------------------------------
template <int32_t N>
void to_json(nlohmann::json& j, GradientDescentLogEverything<N> const& log) {
......@@ -62,8 +40,6 @@ void to_json(nlohmann::json& j, GradientDescentLogEverything<N> const& log) {
};
}
#endif
}
#endif
......@@ -11,7 +11,7 @@ struct GradientDescentLogNothing {
void initialize(Objective const&) {}
template <int32_t N>
void push_back(VectorNs<N> const& point, Scalar value, VectorNs<N> const& gradient) {}
void push_back(VectorNs<N> const&, Scalar, VectorNs<N> const&) {}
};
}
......
#include "clara.hpp"
#include "gradient_descent.h"
#include "objectives/paraboloid.h"
#include "objectives/rosenbrock.h"
#include "logs/everything.h"
#include <iostream>
#ifdef VISUALIZE
#include "logs/everything_vis.h"
#include "objectives/paraboloid.h"
#include "objectives/rosenbrock.h"
#include "utils/eigen_json.h"
#include <fstream>
#include <iostream>
using json = nlohmann::json;
#endif
using namespace optimization;
......@@ -55,13 +52,20 @@ int main(int const argc, char const** argv) {
Objective objective;
objective.dim() = dim;
if (log_file_path.empty() && vis_file_path.empty()) {
// If we're not saving data, use a lean optimizer.
// TODO: Find a way to deduplicate code between these branches.
GradientDescent<-1> optimizer(learning_rate, max_evaluations, gradient_threshold);
VectorXs minimum = optimizer.optimize(objective, initial_point);
std::cout << "n evaluations: " << optimizer.n_evaluations() << '\n';
std::cout << "final point: " << minimum << '\n';
} else {
using Log = GradientDescentLogEverything<-1>;
GradientDescent<-1, Log> optimizer(learning_rate, max_evaluations, gradient_threshold);
VectorXs minimum = optimizer.optimize(objective, initial_point);
std::cout << "n evaluations: " << optimizer.n_evaluations() << '\n';
std::cout << "final point: " << minimum << '\n';
#ifdef VISUALIZE
if (!log_file_path.empty()) {
json data = optimizer;
std::ofstream log_file(log_file_path);
......@@ -73,7 +77,7 @@ int main(int const argc, char const** argv) {
std::ofstream vis_file(vis_file_path);
vis_file << data.dump(4) << '\n';
}
#endif
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment