日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

python常用代碼

系統(tǒng) 1889 0

目錄

  • 常用代碼片段及技巧
    • 自動選擇GPU和CPU
    • 切換當前目錄
    • 臨時添加環(huán)境目錄
    • 打印模型參數(shù)
    • 將tensor的列表轉(zhuǎn)換為tensor
    • 內(nèi)存不夠
    • 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
          
        

臨時添加環(huán)境目錄

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

打印模型參數(shù)

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

將tensor的列表轉(zhuǎn)換為tensor

          
            x = torch.stack(tensor_list)
          
        

內(nèi)存不夠

  • Smaller batch size
  • torch.cuda.empty_cache() every few minibatches
  • 分布式計算
  • 訓(xùn)練數(shù)據(jù)和測試數(shù)據(jù)分開
  • 每次用完之后刪去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()
          
        

更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 大安市| 皮山县| 沾化县| 鸡东县| 海原县| 和田县| 澄江县| 民勤县| 太保市| 北京市| 安泽县| 汉沽区| 贵定县| 兴山县| 南安市| 清水县| 武汉市| 林甸县| 古丈县| 囊谦县| 武汉市| 五原县| 广安市| 什邡市| 越西县| 苏尼特左旗| 鲁甸县| 铁力市| 留坝县| 麻阳| 桂林市| 舞阳县| 尖扎县| 刚察县| 扶沟县| 南部县| 鄂尔多斯市| 本溪| 石台县| 南郑县| 沁水县|