forked from fastai/fastai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
callbacks.csv_logger.html
351 lines (266 loc) · 14.1 KB
/
callbacks.csv_logger.html
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
---
title: callbacks.csv_logger
keywords: fastai
sidebar: home_sidebar
summary: "Callbacks that saves the tracked metrics during training"
---
<div class="container" id="notebook-container">
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h2 id="CSV-Logger">CSV Logger<a class="anchor-link" href="#CSV-Logger">¶</a></h2>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h2 id="CSVLogger" class="doc_header"><code>class</code> <code>CSVLogger</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/csv_logger.py#L10" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CSVLogger-pytest" style="float:right; padding-right:10px">[test]</a></h2><blockquote><p><code>CSVLogger</code>(<strong><code>learn</code></strong>:<a href="/basic_train.html#Learner"><code>Learner</code></a>, <strong><code>filename</code></strong>:<code>str</code>=<strong><em><code>'history'</code></em></strong>) :: <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a></p>
</blockquote>
<div class="collapse" id="CSVLogger-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CSVLogger-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>CSVLogger</code>:</p><ul><li><code>pytest -sv tests/test_callbacks_csv_logger.py::test_logger</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_callbacks_csv_logger.py#L37" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>A <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a> that saves history of metrics while training <code>learn</code> into CSV <code>filename</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>First let's show an example of use, with a training on the usual MNIST dataset.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">path</span> <span class="o">=</span> <span class="n">untar_data</span><span class="p">(</span><span class="n">URLs</span><span class="o">.</span><span class="n">MNIST_TINY</span><span class="p">)</span>
<span class="n">data</span> <span class="o">=</span> <span class="n">ImageDataBunch</span><span class="o">.</span><span class="n">from_folder</span><span class="p">(</span><span class="n">path</span><span class="p">)</span>
<span class="n">learn</span> <span class="o">=</span> <span class="n">Learner</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">simple_cnn</span><span class="p">((</span><span class="mi">3</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">2</span><span class="p">)),</span> <span class="n">metrics</span><span class="o">=</span><span class="p">[</span><span class="n">accuracy</span><span class="p">,</span> <span class="n">error_rate</span><span class="p">],</span> <span class="n">callback_fns</span><span class="o">=</span><span class="p">[</span><span class="n">CSVLogger</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">learn</span><span class="o">.</span><span class="n">fit</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea ">
Total time: 00:01 <p><table style='width:375px; margin-bottom:10px'>
<tr>
<th>epoch</th>
<th>train_loss</th>
<th>valid_loss</th>
<th>accuracy</th>
<th>error_rate</th>
</tr>
<tr>
<th>1</th>
<th>0.586522</th>
<th>0.433411</th>
<th>0.935622</th>
<th>0.064378</th>
</tr>
<tr>
<th>2</th>
<th>0.448884</th>
<th>0.187622</th>
<th>0.949928</th>
<th>0.050072</th>
</tr>
<tr>
<th>3</th>
<th>0.345840</th>
<th>0.130823</th>
<th>0.957082</th>
<th>0.042918</th>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Training details have been saved in 'history.csv'.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">learn</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">ls</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_text output_subarea output_execute_result">
<pre>[PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/labels.csv'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/export.pkl'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/test'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/train'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/history.csv'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/models'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/cleaned.csv'),
PosixPath('/home/ubuntu/.fastai/data/mnist_tiny/valid')]</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>Note that, as with all <a href="/basic_train.html#LearnerCallback"><code>LearnerCallback</code></a>, you can access the object as an attribute of <code>learn</code> after it has been created. Here it's <code>learn.csv_logger</code>.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CSVLogger.read_logged_file" class="doc_header"><code>read_logged_file</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/csv_logger.py#L16" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CSVLogger-read_logged_file-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>read_logged_file</code>()</p>
</blockquote>
<div class="collapse" id="CSVLogger-read_logged_file-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CSVLogger-read_logged_file-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>Tests found for <code>read_logged_file</code>:</p><p>Some other tests where <code>read_logged_file</code> is used:</p><ul><li><code>pytest -sv tests/test_callbacks_csv_logger.py::test_logger</code> <a href="https://github.com/fastai/fastai/blob/master/tests/test_callbacks_csv_logger.py#L37" class="source_link" style="float:right">[source]</a></li></ul><p>To run tests please refer to this <a href="/dev/test.html#quick-guide">guide</a>.</p></div></div><p>Read the content of saved file</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">learn</span><span class="o">.</span><span class="n">csv_logger</span><span class="o">.</span><span class="n">read_logged_file</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_html rendered_html output_subarea output_execute_result">
<div>
<style scoped>
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th>epoch</th>
<th>train_loss</th>
<th>valid_loss</th>
<th>accuracy</th>
<th>error_rate</th>
</tr>
</thead>
<tbody>
<tr>
<th>0</th>
<td>1</td>
<td>0.586522</td>
<td>0.433411</td>
<td>0.935622</td>
<td>0.064378</td>
</tr>
<tr>
<th>1</th>
<td>2</td>
<td>0.448884</td>
<td>0.187622</td>
<td>0.949928</td>
<td>0.050072</td>
</tr>
<tr>
<th>2</th>
<td>3</td>
<td>0.345840</td>
<td>0.130823</td>
<td>0.957082</td>
<td>0.042918</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Calback-methods">Calback methods<a class="anchor-link" href="#Calback-methods">¶</a></h3>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>You don't call these yourself - they're called by fastai's <a href="/callback.html#Callback"><code>Callback</code></a> system automatically to enable the class's functionality.</p>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CSVLogger.on_train_begin" class="doc_header"><code>on_train_begin</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/csv_logger.py#L20" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CSVLogger-on_train_begin-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_begin</code>(<strong>**<code>kwargs</code></strong>:<code>Any</code>)</p>
</blockquote>
<div class="collapse" id="CSVLogger-on_train_begin-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CSVLogger-on_train_begin-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_begin</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Prepare file with metric names.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CSVLogger.on_epoch_end" class="doc_header"><code>on_epoch_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/csv_logger.py#L26" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CSVLogger-on_epoch_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_epoch_end</code>(<strong><code>epoch</code></strong>:<code>int</code>, <strong><code>smooth_loss</code></strong>:<code>Tensor</code>, <strong><code>last_metrics</code></strong>:<code>MetricsList</code>, <strong>**<code>kwargs</code></strong>:<code>Any</code>) → <code>bool</code></p>
</blockquote>
<div class="collapse" id="CSVLogger-on_epoch_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CSVLogger-on_epoch_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_epoch_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Add a line with <code>epoch</code> number, <code>smooth_loss</code> and <code>last_metrics</code>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="output_markdown rendered_html output_subarea ">
<h4 id="CSVLogger.on_train_end" class="doc_header"><code>on_train_end</code><a href="https://github.com/fastai/fastai/blob/master/fastai/callbacks/csv_logger.py#L34" class="source_link" style="float:right">[source]</a><a class="source_link" data-toggle="collapse" data-target="#CSVLogger-on_train_end-pytest" style="float:right; padding-right:10px">[test]</a></h4><blockquote><p><code>on_train_end</code>(<strong>**<code>kwargs</code></strong>:<code>Any</code>)</p>
</blockquote>
<div class="collapse" id="CSVLogger-on_train_end-pytest"><div class="card card-body pytest_card"><a type="button" data-toggle="collapse" data-target="#CSVLogger-on_train_end-pytest" class="close" aria-label="Close"><span aria-hidden="true">×</span></a><p>No tests found for <code>on_train_end</code>. To contribute a test please refer to <a href="/dev/test.html">this guide</a> and <a href="https://forums.fast.ai/t/improving-expanding-functional-tests/32929">this discussion</a>.</p></div></div><p>Close the file.</p>
</div>
</div>
</div>
</div>
</div>
</div>