torch.monitor¶
警告
此模块为原型发布版,其接口和功能可能会在未来版本的 PyTorch 中未经警告即发生变化。
torch.monitor 提供了一个用于记录来自 PyTorch 的事件和计数器的接口。
统计接口的设计目的是用于跟踪定期记录的高级别指标,以便监控系统性能。由于统计信息会以特定窗口大小进行聚合,因此你可以从关键循环中记录这些指标,而对性能影响极小。
对于较为罕见的事件或值(例如损失、准确率、使用情况)的跟踪,可以直接使用事件接口。
可以注册事件处理程序来处理事件并将它们传递给外部事件接收器。
API 参考¶
-
class
torch.monitor.Aggregation¶ These are types of aggregations that can be used to accumulate stats.
Members:
- VALUE :
VALUE returns the last value to be added.
- MEAN :
MEAN computes the arithmetic mean of all the added values.
- COUNT :
COUNT returns the total number of added values.
- SUM :
SUM returns the sum of the added values.
- MAX :
MAX returns the max of the added values.
- MIN :
MIN returns the min of the added values.
-
property
name¶
-
class
torch.monitor.Stat¶ Stat 用于以高性能方式在固定间隔内计算汇总统计信息。Stat 每隔
window_size时间间隔记录一次统计数据。当窗口关闭时,统计信息会通过事件处理程序作为torch.monitor.Stat事件进行记录。window_size应设置为相对较高的值,以避免记录大量事件。例如:60秒。Stat 使用毫秒精度。如果
max_samples被设置,统计将会通过丢弃 add 次调用来限制每个窗口中的样本数量,一旦发生了max_samples次添加。如果没有设置,窗口期间的所有add次调用都将被包含。这是一个可选字段,用于在样本数量可能变化时,使不同窗口之间的聚合更具可比性。当 Stat 被销毁时,它会记录任何剩余的数据,即使窗口尚未结束。
-
__init__(self: torch._C._monitor.Stat, name: str, aggregations: List[torch._C._monitor.Aggregation], window_size: datetime.timedelta, max_samples: int = 9223372036854775807) → None¶ 构建
Stat。
-
property
count¶ 已收集的数据点数量。一旦事件被记录,重置。
-
get(self: torch._C._monitor.Stat) → Dict[torch._C._monitor.Aggregation, float]¶ 返回当前的统计值,主要用于测试目的。如果该统计已记录且未添加其他值,此值将为零。
-
property
name¶ 在创建时设置的统计量名称。
-
-
class
torch.monitor.data_value_t¶ data_value_t 是其中之一
str,float,int,bool。
-
class
torch.monitor.Event¶ 事件表示要记录的特定类型事件。这可以代表高层次的数据点,例如每个epoch的损失或准确率,或者是通过该库提供的Stats进行的更底层的聚合。
所有相同类型的事件都应该具有相同的名称,以便下游处理器能够正确处理它们。
-
__init__(self: torch._C._monitor.Event, name: str, timestamp: datetime.datetime, data: Dict[str, data_value_t]) → None¶ 构建
Event。
-
property
data¶ 包含在
Event中的结构化数据。
-
property
name¶ 名称为
Event。
-
property
timestamp¶ 事件
Event发生的 timestamp。
-
-
class
torch.monitor.EventHandlerHandle¶ EventHandlerHandle 是由
register_event_handler返回的一种包装类型,用于通过unregister_event_handler取消注册处理程序。此类型不能直接初始化。
-
torch.monitor.log_event(event: torch._C._monitor.Event) → None¶ log_event 将指定事件记录到所有已注册的事件处理器中。具体的事件输出由事件处理器负责写入相应的事件接收端。
如果没有注册事件处理程序,则该方法将不执行任何操作。
-
torch.monitor.register_event_handler(callback: Callable[[torch._C._monitor.Event], None]) → torch._C._monitor.EventHandlerHandle¶ register_event_handler 注册一个回调函数,该函数将在每次通过
log_event记录事件时被调用。这些处理程序应避免阻塞主线程,因为它们在log_event调用期间运行,可能会干扰训练。
-
torch.monitor.unregister_event_handler(handler: torch._C._monitor.EventHandlerHandle) → None¶ unregister_event_handler 取消注册调用
register_event_handler后返回的EventHandlerHandle。在此之后,事件处理程序将不再接收事件。
-
class
torch.monitor.TensorboardEventHandler(writer)[source]¶ TensorboardEventHandler 是一个事件处理程序,它会将已知事件写入提供的 SummaryWriter 中。
这目前仅支持
torch.monitor.Stat个作为标量记录的事件。>>> from torch.utils.tensorboard import SummaryWriter >>> from torch.monitor import TensorboardEventHandler, register_event_handler >>> writer = SummaryWriter("log_dir") >>> register_event_handler(TensorboardEventHandler(writer))