Colab/Python常用代码单元格

Colab/Python常用代码单元格

oyxy2019 226 2023-11-20

Colab/Python常用代码单元格

谷歌硬盘
from google.colab import drive
drive.mount('/content/drive')

# 转储google硬盘
# ! cp -r <dir> /content/drive/MyDrive

# 从google硬盘加载
import os
os.chdir('/content/drive/MyDrive')
# gdown下载Google硬盘文件
!gdown --id '1Car9FI8hQBMiVOjgn_wrwMtHphXb8hc4' --output Data.zip
!unzip -q Data.zip
!ls Data
# git下载文件
!git clone https://github.com/gystar/devign_lab
# cd到目标路径下
import os
path = '/content/drive/MyDrive/devign_lab/'
os.chdir(path)
print("Current Working Directory:", os.getcwd())
os.listdir(path)
# 打包下载
!zip -r folder_name.zip folder_name
# 解压,要注意如果压缩包第一级目录不是项目根目录的话则要mkdir
!unzip -d target_dir folder_name.zip
# 显示文件大小
du -sh *
⭕**oyxy注:以上代码均执行一次运行成功即可**

**要运行下面的单元格可以直接选中此单元格,然后Ctrl+F10(等于【代码执行程序】【运行当前单元格以及其后的所有单元格】)**
GPU相关
# python控制台
import torch
torch.__version__
torch.cuda.is_available()
torch.cuda.device_count()
print(torch.cuda.get_device_name(0))
# python代码
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(torch.cuda.get_device_name(device) if device.type == 'cuda' else "CPU")
data.to(device)
固定随机种子
import numpy as np
import torch
import random

def same_seeds(seed):
    random.seed(seed) 
    np.random.seed(seed)  
    torch.manual_seed(seed)
    if torch.cuda.is_available():
        torch.cuda.manual_seed(seed)
        torch.cuda.manual_seed_all(seed) 
    torch.backends.cudnn.benchmark = False
    torch.backends.cudnn.deterministic = True

same_seeds(1314)
打印输出相关
# 打印tensor
print(f"xx: {xx.shape} \n{xx}")
# model相关
print("Total parameters:", sum(p.numel() for p in model.parameters()))
# 将print输出重定向到文件(w表示覆盖写,a表示追加写)
import sys
sys.stdout = open('output.txt', 'w')
# 命令行方式,-a表示追加写
python test.py | tee output.txt
python test3.py | tee -a output.txt