NeuralNet 1.0
Loading...
Searching...
No Matches
Gauge.hpp
1#pragma once
2
3#include <ftxui/dom/elements.hpp> // for text, gauge, operator|, flex, hbox, Element
4#include <ftxui/screen/screen.hpp> // for Screen
5#include <iostream> // for cout, endl, ostream
6#include <string> // for allocator, char_traits, operator+, operator<<, string, to_string, basic_string
7#include <thread> // for sleep_for
8
9using namespace ftxui;
10
11namespace NeuralNet {
17class Gauge {
18 public:
19 Gauge(std::string preText, int totalIndexes, int currIndex = 0) {
20 this->totalIndexes = totalIndexes;
21 this->currIndex = currIndex;
22 this->preText = preText;
23 };
24
25 Gauge &operator++() {
26 this->currIndex++;
27 printGauge();
28 return *this;
29 };
30
31 protected:
32 int totalIndexes;
33 int currIndex;
34 std::string preText;
35 // string to be printed in order to reset the cursor position
36 std::string resetPos;
37
38 private:
39 void printGauge() {
40 std::string ratioStr = std::to_string(this->currIndex) + "/" +
41 std::to_string(this->totalIndexes);
42 float ratio = static_cast<float>(this->currIndex) / this->totalIndexes;
43 Element document = hbox({
44 text(this->preText),
45 gauge(ratio) | flex,
46 text(" " + ratioStr),
47 });
48
49 auto screen =
50 Screen::Create(Dimension::Fixed(100), Dimension::Fit(document));
51 Render(screen, document);
52 std::cout << this->resetPos;
53 screen.Print();
54 this->resetPos = screen.ResetPosition();
55 };
56};
57
58// todo: Refactor this code to not have duplicates
59class TrainingGauge : public Gauge {
60 public:
61 TrainingGauge(int totalIndexes, int currIndex = 0, int totalEpochs = 10,
62 int currEpoch = 0)
63 : Gauge("Training : ", totalIndexes, currIndex) {
64 this->totalEpochs = totalEpochs;
65 this->currEpoch = currEpoch;
66 }
67
68 void printWithLoss(double l) {
69 ++this->currIndex;
70 std::string ratioStr = std::to_string(this->currIndex) + "/" +
71 std::to_string(this->totalIndexes);
72 std::string epochStr = "Epoch : " + std::to_string(this->currEpoch) + "/" +
73 std::to_string(this->totalEpochs);
74 std::string errorStr = "Loss : " + std::to_string(static_cast<float>(l));
75 float ratio = static_cast<float>(this->currIndex) / this->totalIndexes;
76
77 auto screen = gaugeBuilder({hbox({
78 text(epochStr + " "),
79 gauge(ratio),
80 text(" " + ratioStr),
81 }) | flex |
82 border,
83 text(errorStr) | border});
84
85 std::cout << this->resetPos;
86 screen.Print();
87 this->resetPos = screen.ResetPosition();
88 }
89
93 void printWithLAndA(double l, double a) {
94 ++this->currIndex;
95 std::string ratioStr = std::to_string(this->currIndex) + "/" +
96 std::to_string(this->totalIndexes);
97 std::string epochStr = "Epoch : " + std::to_string(this->currEpoch) + "/" +
98 std::to_string(this->totalEpochs);
99 std::string errorStr = "Loss : " + std::to_string(static_cast<float>(l));
100 std::string accStr = "Accuracy : " + std::to_string(static_cast<float>(a));
101
102 float ratio = static_cast<float>(this->currIndex) / this->totalIndexes;
103
104 auto screen =
105 gaugeBuilder({hbox({
106 text(epochStr + " "),
107 gauge(ratio),
108 text(" " + ratioStr),
109 }) | flex |
110 border,
111 text(errorStr) | border, text(accStr) | border});
112
113 std::cout << this->resetPos;
114 screen.Print();
115 this->resetPos = screen.ResetPosition();
116 };
117
125 Screen gaugeBuilder(Elements elements) {
126 Element document = hbox(elements);
127
128 Screen screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
129
130 Render(screen, document);
131
132 return screen;
133 }
134
135 private:
136 std::string resetPos;
137 int totalEpochs;
138 int currEpoch;
139};
140} // namespace NeuralNet
Definition Gauge.hpp:17
Definition Gauge.hpp:59
Screen gaugeBuilder(Elements elements)
Create a horizontal document with the given elements.
Definition Gauge.hpp:125
void printWithLAndA(double l, double a)
Definition Gauge.hpp:93