Torch Memo
Preliminaries
.__dir__()
: Getting all the functions in the module (Tool function)
TensorBoard
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| from torch.utils.tensorboard import SummaryWriter import numpy as np from PIL import Image
writer = SummaryWriter("logs")
def compute(x): return (torch.sin(torch.tensor(x)) + torch.randn(1) * torch.sin(torch.tensor(x)))
for i in range(100): writer.add_scalar("y=x", compute(i).item(), i)
img = Image.open("003.jpg") img_array = np.array(img)
writer.add_image("test", img_array, 1, dataformats='HWC')
|
Use Transforms to make Data Augmentation and design your own class of data!
Use GPU in torch
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
import torch
import argparse
parser = argparse.ArgumentParser(description="Selecting device for Pytorch") parser.add_argument( "--device", type=str, choices=["cuda", "cpu"], default="cuda" if torch.cuda.is_available() else "cpu", help= "Device to use, cuda or cpu" ) args = parser.parse_args()
device = args.device print(f"Device selectd, using {device}")
|
Tensor Operations
torch中最关键的数据结构就是张量,因此对张量做操作是数据预处理的关键操作之一。