GPU与可视化

Gpu加速

GPU加速

device=torch.device('cuda:0')
net=MLP.to(device)
对应数据也需要加入.to(device)或者使用data.cuda()

.item():取tensor中的值

Visdom可视化

tensorboardX

pip install tensorboardX

from tensorboardX import SummaryWriter
writer=SummaryWriter()
writer.add_scalar('data/scalar_group', {'xsinx':n_iter*np.sin(n_iter),'xcos':n_iter*np.cos(n_iter),n_iter})
writer.add_image('Image',x,n_iter)
writer.add_text('Text','text logged at step:'+str(n_iter),n_iter)
for name,param in resnet18.named_parameters():
writer.add_histogram(name,param.clone().cpu().adta.numpy(),n_iter)
writer.close()

Visdom

运行效率更高

pip install visdom
python -m visdom.server
如果运行报错,则重新下载visdom后通过进入文件目录后
pip install -e 安装
#一条线
from visdom import Visdom
viz=Visdom()
viz.line([0.],[0.],win='train_loss',opts=dict(title='train loss'))#创建一条直线(y,x,ID,opt:属性配置)
viz.line([loss.item()],[global_step],win='train_loss',update='append')#将数据添加到直线中(update操作)
#多条线
from visdom import Visdom
viz=Visdom()
viz.line([0.0,0.0],[0.0,0.0],win='test',opts=dict(title='test loss&acc',legend=['loss','acc']))#创建两条直线([y1,y2],x,ID,opt:属性配置)
viz.line([[test_loss,cprrect/len(test_loader.dataset)]],[global_step],win='test',update='append')#将数据添加到直线中(update操作)
#可视化image与pred
from visdom import Visdom
viz=Visdom()
viz.images(data.reshape(-1,1,28,28),win='x')
viz.text(str(pred.detach().cpu().numpy),win='pred',opts=dict(title='pred'))