Blame 无法作为单个页面加载。
Newer
Older
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
import os
import sys
import time
from dataclasses import dataclass
from datetime import datetime
from enum import IntFlag
from logging import getLogger
from typing import *
import numpy as np
from .checkpoint import BaseCheckpoint, CheckpointManager
from .errors import NaNMetricError
from .formatting import MetricsFormatter, format_duration, format_as_asctime
from .logging_ import print_with_time
from .metrics import ScalarMetricsLogger, ScalarMetricCollector
from .mlstorage import ExperimentDoc
from .stateful import StatefulObjectGroup, StatefulObject
from .utils import NOT_SET
__all__ = [
'CallbackData', 'Callback', 'CallbackList',
'LoggerMode', 'LoggerCallback', 'StopOnNaN',
'BaseTrainCallback', 'BaseCheckpointCallback',
'AutoCheckpoint', 'EarlyStopping',
]
@dataclass
class CallbackData(object):
"""
Data carried by a cycle begin/end event from :class:`Callback`.
"""
__slots__ = ('stage', 'index', 'size', 'start_timestamp',
'end_timestamp', 'exc_time', 'metrics')
stage: 'Stage'
"""The stage that calls the callback."""
index: Optional[int]
"""Index of the epoch or batch, start from 1."""
size: Optional[int]
"""The size of the batch."""
start_timestamp: float
"""Start timestamp of the stage/epoch/batch."""
end_timestamp: Optional[float]
"""End timestamp of the stage/epoch/batch, available at the cycle end."""
exc_time: Optional[float]
"""Execution time of the stage/epoch/batch, available at the cycle end."""
metrics: Optional[Dict[str, Any]]
"""Metrics dict, available at the cycle end."""
class Callback(object):
"""Base class of a callback for a machine learning stage."""
priority: int = 0
"""
The priority of the callback. Smaller priority indicates the callback
should be called earlier than other callbacks with larger priorities.
"""
###########
# metrics #
###########
def on_metrics(self, data: CallbackData):
pass # pragma: no cover
##################
# general events #
##################
def on_stage_begin(self, data: CallbackData):
pass # pragma: no cover
def on_stage_end(self, data: CallbackData):
pass # pragma: no cover
def on_epoch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_epoch_end(self, data: CallbackData):
pass # pragma: no cover
def on_batch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_batch_end(self, data: CallbackData):
pass # pragma: no cover
################
# train events #
################
def on_train_begin(self, data: CallbackData):
pass # pragma: no cover
def on_train_end(self, data: CallbackData):
pass # pragma: no cover
def on_train_epoch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_train_epoch_end(self, data: CallbackData):
pass # pragma: no cover
def on_train_batch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_train_batch_end(self, data: CallbackData):
pass # pragma: no cover
#####################
# validation events #
#####################
def on_validation_begin(self, data: CallbackData):
pass # pragma: no cover
def on_validation_end(self, data: CallbackData):
pass # pragma: no cover
def on_validation_batch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_validation_batch_end(self, data: CallbackData):
pass # pragma: no cover
###############
# test events #
###############
def on_test_begin(self, data: CallbackData):
pass # pragma: no cover
def on_test_end(self, data: CallbackData):
pass # pragma: no cover
def on_test_batch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_test_batch_end(self, data: CallbackData):
pass # pragma: no cover
##################
# predict events #
##################
def on_predict_begin(self, data: CallbackData):
pass # pragma: no cover
def on_predict_end(self, data: CallbackData):
pass # pragma: no cover
def on_predict_batch_begin(self, data: CallbackData):
pass # pragma: no cover
def on_predict_batch_end(self, data: CallbackData):
pass # pragma: no cover
class CallbackList(Sequence[Callback]):
"""
A callback list, which maintains the orders of callbacks according to
their priority.
"""
_SORTED = object()
def __init__(self,
callbacks: Optional[Iterator[Callback]] = None,
*,
_sorted=None):
if callbacks is not None:
if _sorted is not self._SORTED:
callbacks = sorted(callbacks, key=lambda cb: cb.priority)
else:
callbacks = list(callbacks)
self._callbacks = callbacks
def __len__(self) -> int:
return len(self._callbacks)
def __iter__(self):
return iter(self._callbacks)
def __eq__(self, other):
return isinstance(other, CallbackList) and \
self._callbacks == other._callbacks
def __getitem__(self, item):
return self._callbacks[item]
def __delitem__(self, item):
del self._callbacks[item]
def __copy__(self):
return self.clone()