-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperimentRunner.h
118 lines (98 loc) · 2.87 KB
/
ExperimentRunner.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#pragma once
#include <iostream>
#include <chrono>
#include <torch/torch.h>
#include "Trainers/ITrainer.h"
#include "Evaluator.h"
class IExperimentRunner
{
public:
virtual void Run() = 0;
};
struct ScopedBlockLabel
{
ScopedBlockLabel(std::string msg) : _msg(msg)
{
using namespace std;
auto now = chrono::system_clock::to_time_t(chrono::system_clock::now());
cout << "Beginning " << msg << " at " << ctime(&now) << std::endl;
}
~ScopedBlockLabel()
{
using namespace std;
auto now = chrono::system_clock::to_time_t(chrono::system_clock::now());
std::cout << "Ending " << _msg << " at " << ctime(&now) << std::endl;
}
std::string _msg;
};
template <typename NetworkType, typename DatasetType>
class ExperimentRunner : public IExperimentRunner
{
using DataLoader_t = std::unique_ptr<torch::data::StatelessDataLoader<DatasetType, torch::data::samplers::RandomSampler>>;
public:
ExperimentRunner(
std::string experimentName,
DatasetType& dataset,
torch::nn::ModuleHolder<NetworkType> network,
std::shared_ptr<ITrainer> trainer,
int numberOfEpochs,
int batchSize,
c10::Device device) :
_experimentName(experimentName),
_network(network),
_trainer(trainer),
_batchSize(batchSize),
_numberOfEpochs(numberOfEpochs),
_pgdAttacker(std::make_shared<PGDAttacker<NetworkType>>(
/*epsilon*/ 6.0 / 255.0,
/*sigma*/ 3.0 / 255.0,
/*iterations*/ 20,
/*device*/ device)),
_device(device),
_dataset(dataset)
{}
void Run() override
{
ScopedBlockLabel startExperiment("Experiment " + _experimentName);
// Train
Evaluator<NetworkType> evaluator(_pgdAttacker, _device);
DataLoader_t dataloader = torch::data::make_data_loader(
_dataset,
torch::data::DataLoaderOptions().batch_size(_batchSize).workers(2));
for (int epoch = 0; epoch < _numberOfEpochs; ++epoch)
{
ScopedBlockLabel startExperiment("epoch " + std::to_string(epoch + 1));
// Training block
for (torch::data::Example<> batch : *dataloader)
_trainer->train_batch(batch);
print_accuracies(_trainer->get_accuracies());
if (epoch % 10)
{
this->evaluate(evaluator, dataloader);
}
}
this->evaluate(evaluator, dataloader);
}
private:
void evaluate(Evaluator<NetworkType>& evaluator, DataLoader_t& dataset)
{
_network->eval();
evaluator.reset();
for (torch::data::Example<> batch : *dataset)
evaluator.evaluate_single_batch(_network, batch);
print_accuracies(evaluator.get_accuracies());
_network->train();
}
void print_accuracies(std::pair<double, double> accuracies)
{
std::cout << "Clean accuracy " << accuracies.first << ", Adversarial accuracy " << accuracies.second << std::endl;
}
std::string _experimentName;
DatasetType _dataset;
torch::nn::ModuleHolder<NetworkType> _network;
std::shared_ptr<ITrainer> _trainer;
int _numberOfEpochs;
int _batchSize;
c10::Device _device;
std::shared_ptr<PGDAttacker<NetworkType>> _pgdAttacker;
};