我们在调试或者编写深度学习网络代码,最大的挑战之一,尤其对新手来说,就是把所有的张量维度正确对齐。如果以前就有 TensorSensor 这个工具,相信我们的开发效率会更高,本文介绍TensorSensor的用法。
TensorSensor安装
pip install tensor-sensor
向量计算可视化
import tsensor
import graphviz
import torch
import sys
W = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([9, 10]).reshape(2, 1)
x = torch.tensor([4, 5]).reshape(2, 1)
h = torch.tensor([1,2])
with tsensor.explain():
a = torch.relu(x)
b = W @ b + h.dot(h)
可视化上述向量的运算:
W = torch.rand(size=(2000,2000))
b = torch.rand(size=(2000,1))
h = torch.rand(size=(1_000_000,))
x = torch.rand(size=(2000,1))
with tsensor.explain() as explained:
a = torch.relu(x)
b = W @ b + torch.zeros(2000,1)+(h+3).dot(h)
网络模型运算可视化:
import torch.nn as nn
x1=torch.randn((8,3,640,640))
with tsensor.explain() as explained:
a = nn.Conv2d(3,32,kernel_size=3)(x1)
b = nn.BatchNorm2d(32)(a)
c = torch.relu(b)
向量维度可视化:
结束语
TensorSensor 将张量库的调用视为操作符,无论是对网络层还是对 torch.dot(a,b) 之类的简单操作的调用。在库函数中触发的异常会产生消息,消息标示了函数和任何张量参数的维数。大家可以先实践,看这个库是否有效提高日常开发工作的效率。
欢迎扫码关注: