![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
PyTorch
pyxiea
双鸭山大学研究生,对深度学习、自然语言处理、推荐系统感兴趣
展开
-
nvidia-smi查看无进程但有显存占用、pytorch的dataloader在num_worker>0子进程没有在主进程被kill时被清理
复现步骤:pytorch dataloader的num_worker设为大于0的数(例如8),然后执行后台运行脚本的命令:nohup python -u script.py &,shell会返回进程pid,下图中我还加了指定GPU的环境变量以及输出重定向:使用 watch -n .5 ps aux --sort -rss' 持续按照内存从大到小查看进程,dataloader的代码执行之后会发现有1+8个你创建的进程是占用内存排在前列的(下图中我的用户名是xiepengyu):其中第一个进原创 2020-11-23 16:23:04 · 1830 阅读 · 3 评论 -
Pytorch DataLoader 内存泄漏 RuntimeError: received 0 items of ancdata
现象内存占用随着训练过程逐渐增大,最终Out of memory。即使将不再用到的变量及时删除并使用gc.collect(),也无法解决。解决方案方案1:Dataset 的成员变量避免使用Python原生数据格式以下面的Dataset代码为例,如果使用 self.data = [x for x in range(24000000)],即使用了List作为Dataset的成员变量,当 num_worker>0 时就会导致前面的现象,而改用numpy ndarray 包装一下就没问题了。另外,参考原创 2020-08-16 09:43:47 · 4829 阅读 · 5 评论 -
pytorch修改Dataparallel的主gpu
背景pytorch使用Dataparallel时会出现负载不均衡的现象,一般来说0号gpu占用显存最多。但是,有时候0号gpu显存不是特别多了,但是其他gpu显存很多,更适合用来作为主gpu。所以我们得想办法指定其他gpu为主gpu。方法1、修改之前,大家使用多gpu的代码指定device是这样指定的:device = torch.device("cuda" if torch.cuda.is_available() else "cpu")现在需要修改为:device = torch.devic原创 2020-05-23 10:19:36 · 1151 阅读 · 0 评论 -
pytorch中的nn.Bilinear
参考:pytorch中的nn.Bilinear的计算原理详解代码实现使用numpy实现Bilinear(来自参考资料):print('learn nn.Bilinear')m = nn.Bilinear(20, 30, 40)input1 = torch.randn(128, 20)input2 = torch.randn(128, 30)output = m(input1, i...原创 2020-04-12 10:42:29 · 6380 阅读 · 0 评论 -
None作为ndarray或tensor的索引
None作为ndarray或tensor的索引作用是增加维度,与 pytorch中的 torch.unsqueeze() 或 tensorflow 中的tf.expand_dims() 作用相同例子:In [5]: t=torch.from_numpy(np.arange(12).reshape(3,4))In [6]: tOut[6]:tensor([[ 0, 1, 2, 3]...原创 2020-04-07 12:01:00 · 2015 阅读 · 0 评论 -
softmax数值稳定性问题以及CrossEntropyWithLogits的由来
softmax自身导致的数值问题对于 x=[x1,x2,⋯ ,xn]x=[x_1,x_2,\cdots,x_n]x=[x1,x2,⋯,xn],softmax公式:softmax(x)=[a1,a2,⋯ ,an],ai=exi∑j=1nexjsoftmax(x)=[a_1,a_2,\cdots,a_n], \quad a_i=\frac{e^{x_i}}{\sum_{j=1}^{n}e^{...原创 2020-03-11 20:40:49 · 1087 阅读 · 0 评论 -
model.to(device)无法将自定义层的tensor转移到指定设备
有时在model内自定义的模块或参数无法被model.to(device)正确转移到指定设备。如果仅在CPU上跑是没问题的,但如果在GPU上跑,其余部分参数被转移到了GPU,这部分无法正确转移的参数却存放在CPU,就会报错。可以考虑以下几种解决办法:1、使用nn.ModuleList()而不是python的内建List来存放多个模块例如下面自定义的Module,如果将nn.ModuleLis...原创 2020-01-22 18:53:59 · 4421 阅读 · 2 评论 -
踩坑日志
RuntimeError: CUDA out of memory. Tried to allocate 9.00 MiB (GPU 0; 10.92 GiB total capacity; 208.18 MiB already allocated; 2.19 MiB free; 838.00 KiB cached)https://discuss.pytorch.org/t/gpu-is-not-...原创 2019-10-08 10:30:49 · 309 阅读 · 0 评论 -
PyTorch的permute和reshape/view的区别
二维的情况先用二维tensor作为例子,方便理解。permute作用为调换Tensor的维度,参数为调换的维度。例如对于一个二维Tensor来说,调用tensor.permute(1,0)意为将1轴(列轴)与0轴(行轴)调换,相当于进行转置。In [20]: a Out[20]: tensor([[0, 1, 2], ...原创 2019-09-28 15:24:34 · 12325 阅读 · 2 评论 -
PyTorch中使用Tensor作为索引
一维Tensor作为索引在Numpy中,我们可以传入数组作为索引,称为花式索引。这里只演示使用两个一维List的例子。In[42]: a=np.arange(18).reshape(6,3)In[43]: aOut[43]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 9, 10, 11]...原创 2019-09-28 10:23:33 · 13146 阅读 · 0 评论 -
pytorch中Tensor.backward()当Tensor不为标量的情况
运行如下代码:In [3]: import torchIn [4]: from torch.autograd import VariableIn [6]: x=Variable(torch.Tensor([2,3,4,5]),requires_grad=True)In [7]: y=Variable(torch.zeros(3))In [8]: y[0]=x[0]*x[1]I...原创 2019-09-22 22:41:04 · 1794 阅读 · 0 评论 -
pytorch多GPU实践——解决RuntimeError: Expected hidden[0] size (1, 2500, 50), got (1, 10000, 50)
说在前头本文针对以下读者:如果你也是在使用pytorch多gpu模式的过程遇到了题目所述的问题。如果你自定义的网络结构中使用到了RNN的hidden state。正文最近在想办法将一个pytorch项目修改为可以使用多GPU。这个项目是github上对于relational recurrent network(RRN)的一个pytorch实现(github地址)。RRN的网络结构使用...原创 2019-08-21 23:49:27 · 7325 阅读 · 0 评论