m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/experiment.py
blob: f8a219aab9bac3eeeef66f148bbaec51ce575de8 (plain)
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
import subprocess
import os
import time

import pandas as pd
import matplotlib.pyplot as plt
import torch

from runner import Runner

class Experiment:
    def __init__(self, file):
        self.make_dir()
        self.copy_config(file)
        self.metrics = ExperimentMetrics()
        self.runner = Runner(file, self.metrics)

    def run(self):
        self.runner.run()

    def save_results(self):
        data = self.metrics.get_dataframe()
        data.to_csv(self.dir_path('metrics.csv'))

        plt.plot(data['train_losses'], label='train loss')
        plt.plot(data['test_losses'], label='test loss')
        plt.xlabel('Epoch')
        plt.ylabel('Loss')
        plt.legend()
        plt.savefig(self.dir_path('losses.png'))
        plt.clf()

        plt.plot(data['test_accuracies'], label='test accuracy')
        plt.xlabel('Epoch')
        plt.ylabel('% correct')
        plt.legend()
        plt.savefig(self.dir_path('accuracies.png'))

        torch.save(self.runner.net.state_dict(), self.dir_path('net.pt'))

    def dir_path(self, file):
        return '{}/{}'.format(self.dirname, file)

    def make_dir(self):
        time_string = time.strftime('%Y%m%d%H%M%S')
        dirname = 'outputs/{}'.format(time_string)
        self.dirname = dirname
        os.mkdir(dirname)

    def copy_config(self, file):
        subprocess.run(['cp', file, '{}/config.yaml'.format(self.dirname)])

class ExperimentMetrics:
    def __init__(self):
        self.train_losses = []
        self.test_losses = []
        self.test_accuracies = []

    def add_train_loss(self, loss):
        self.train_losses.append(round(loss.tolist(), 3))

    def add_test_metrics(self, loss, accuracy):
        self.test_losses.append(round(loss.tolist(), 3))
        self.test_accuracies.append(accuracy)

    def get_dataframe(self):
        return pd.DataFrame({
            'train_losses': self.train_losses,
            'test_losses': self.test_losses,
            'test_accuracies': self.test_accuracies,
            })