DEV Community

Cover image for Pytorch codes - Part 1
Sattineez_SSC
Sattineez_SSC

Posted on

Pytorch codes - Part 1

Hi, DEV community πŸ™‹β€β™€οΈ

In this post, I will abord some pytorch codes to help with your journey. Most of my knowledge comes from Freecodecamp tutorial, TechwithTim. The Cherno, Programing with Mosh and Fireship (all from youtube). They deserve credit for teaching me a lot of cool stuff.πŸ₯°

  1. Imports
  • General:

imports the root package, imports dataset representation and loading

import torch
from torch.utils.data import Dataset, DataLoader
Enter fullscreen mode Exit fullscreen mode
  • Neural Network API:

imports the computation graph, puts the tensor node in the computation graph, imports neural networks, layers ..., import optimizers and the hybrid frontend decorator plus tracing jit.

import torch.autograd as autograd
from torch import Tensor
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.jit import script, trace
Enter fullscreen mode Exit fullscreen mode
  • Torchscript and JIT
torch.jit.trace()

@script 
Enter fullscreen mode Exit fullscreen mode

-ONNX

the last line prints a human-readable representation

torch.onnx.export(model,dummy data, xxxx.proto)

model= onnx.load("ModelName.proto")
onnx.checker.check_model(model)

onnx.helper.printable_graph(model.graph) #this one
Enter fullscreen mode Exit fullscreen mode

-Vision

imports vision datasets. architect and transforms that can be composed

from torchvision import datasets, models, transforms

import torchvision.transforms as transforms
Enter fullscreen mode Exit fullscreen mode
  • Distributed Training

it will distribute the communication and the memory sharing processes

import torch.distributed as dist
from torch.multiprocessing import Process
Enter fullscreen mode Exit fullscreen mode
  1. Data Utilities
  • Datasets abstract class representing datasets, labelling the datasets in form of tensor and concatenation of them
Dataset
TensorDataset
Concat Dataset
Enter fullscreen mode Exit fullscreen mode
  • Data loaders and data samplers
DataLoader(dataset, batch_size=1, ...)

sampler.Sampler(dataset, ...)

sampler.XSampler where # Sequencial || Random || SubsetRandom || 
                       WeightedRandom || Batch || Distributed

Enter fullscreen mode Exit fullscreen mode

Stay tuned for the part 2 β˜†: .q. o(≧▽≦)o .q.:β˜†

Top comments (0)