bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Logging to Tensorboard

An experiment is easier to follow when its numbers are plotted as it runs. TensorboardServer sends values to a small server that writes them for Tensorboard, with PyTorch's SummaryWriter. This example reads the settings of an experiment from config.json and logs some of them.

What you need

  • a Tensorboard server that TensorboardServer can reach at http://0.0.0.0:8002, with PyTorch installed on its machine;
  • config.json, which the build copies next to the executable. Set log_path in it to where the server should write the logs.

What it logs

TensorboardServer has add_scalar, add_scalars and add_text. The example creates the directory of the experiment from experiment_dict and experiment_id, logs the learning rate, the seed and the number of epochs as scalars, and the optimizer as text.

Running the example

./tensorboard
tensorboard --logdir <log_path>

The code

#include <filesystem>
#include <iostream>
#include <random>
#include <string>
namespace tensorboard
{
using namespace bitsim;
namespace fs = std::filesystem;
const std::string CONFIG = "config.json";
} // namespace tensorboard
int main()
{
using namespace tensorboard;
try
{
// load the json configuration
JSONFileReader json_reader(CONFIG);
json_reader.open();
auto experiment_dict = json_reader.template get_value<std::string>("experiment_dict");
auto experiment_id = json_reader.template get_value<std::string>("experiment_id");
std::cout << "Experiment directory: " << experiment_dict << std::endl;
std::cout << "Experiment id: " << experiment_id << std::endl << std::endl;
const fs::path EXPERIMENT_DIR_PATH = experiment_dict + experiment_id;
// the first thing we want to do when monitoring experiments
// is to create a directory where all data will reside
std::filesystem::create_directories(experiment_dict + experiment_id);
const auto input_size = json_reader.template get_value<uint_t>("input_size");
const auto output_size = json_reader.template get_value<uint_t>("output_size");
const auto num_epochs = json_reader.template get_value<uint_t>("num_epochs");
const auto learning_rate = json_reader.template get_value<real_t>("lr");
auto log_path = json_reader.template get_value<std::string>("log_path");
// log the hyperparameters
std::cout << "Input size: " << input_size << std::endl;
std::cout << "Output size: " << output_size << std::endl;
std::cout << "Max epochs: " << num_epochs << std::endl;
std::cout << "Learning rate: " << learning_rate << std::endl;
TensorboardServer logger("http://0.0.0.0:8001");
std::cout << "Logging results at " << logger.get_log_dir_path() << std::endl;
logger.init(std::filesystem::absolute(log_path).string());
logger.add_scalar("lr", learning_rate);
logger.add_scalar("seed", 42);
logger.add_scalar("num_epochs", num_epochs);
logger.add_text("optimizer", "torch::optim::SGD");
logger.close();
}
catch (std::exception &e)
{
std::cout << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unknown exception occured" << std::endl;
}
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
class TensorboardServer
Definition tensorboard_server.h:21
Definition json_file_reader.h:16
int main()
Definition discounted_returns.cpp:19
Definition dc_motor_backend_base.h:9
Definition tensorboard.cpp:30
const std::string CONFIG
Definition tensorboard.cpp:36