python常用代码

系统 417 0

目录

  • 常用代码片段及技巧
    • 自动选择GPU和CPU
    • 切换当前目录
    • 临时添加环境目录
    • 打印模型参数
    • 将tensor的列表转换为tensor
    • 内存不够
    • debug tensor memory

常用代码片段及技巧

自动选择GPU和CPU

          
            device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# model and tensor to device
vgg = models.vgg16().to(device)
          
        

切换当前目录

          
            import os
try:
    os.chdir(os.path.join(os.getcwd(), '..'))
    print(os.getcwd())
except:
    pass
          
        

临时添加环境目录

          
            import sys
sys.path.append('引用模块的地址')
print(sys.path)
          
        

打印模型参数

          
            from torchsummary import summary
# 1 means in_channels
summary(model, (1, 28, 28))
          
        

将tensor的列表转换为tensor

          
            x = torch.stack(tensor_list)
          
        

内存不够

  • Smaller batch size
  • torch.cuda.empty_cache() every few minibatches
  • 分布式计算
  • 训练数据和测试数据分开
  • 每次用完之后删去variable,采用 del x

debug tensor memory

resource` module is a Unix specific package as seen in https://docs.python.org/2/library/resource.html which is why it worked for you in Ubuntu, but raised an error when trying to use it in Windows.

Here is what solved it for me.

  1. Downgrade to the Apache Spark 2.3.2 prebuild version
  2. Install (or downgrade) jdk to version 1.8.0
    • My installed jdk was 1.9.0, which doesn't seem to be compatiable with spark 2.3.2 or 2.4.0
  3. make sure that when you run java -version in cmd (command prompt), it show java version 8. If you are seeing version 9, you will need to change your system ENV PATH to ensure it points to java version 8.
  4. Check this link to get help on changing the PATH if you have multiple java version installed.
          
            device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

def debug_memory():
    import collections, gc, resource, torch
    print('maxrss = {}'.format(
        resource.getrusage(resource.RUSAGE_SELF).ru_maxrss))
    tensors = collections.Counter((str(o.device), o.dtype, tuple(o.shape))
                                  for o in gc.get_objects()
                                  if torch.is_tensor(o))
    for line in sorted(tensors.items()):
        print('{}\t{}'.format(*line))
        
        
 # example
import tensor
 x = torch.tensor(3,3)
 debug_memory()
 
 y = torch.tensor(3,3)
 debug_memory()
 
 z = [torch.randn(i).long() for i in range(10)]
 debug_memory()
          
        

更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论