[PYTHON] Introduction to PyTorch (1) Automatic differentiation

Getting Started with Pytorch

Since Chainer announced the transition to PyTorch, I think that the flow from TensorFlow to PyTorch will be strengthened in Japan as well, so I will do the tutorial.

Installation

See Official.

tutorial

Try it as per Official.

1. What is PyTorch?

The writing style is quite Numpy-like.

$ x = torch.empty(5, 3)
$ print(x)
tensor([[2.4835e+27, 2.5428e+30, 1.0877e-19],
        [1.5163e+23, 2.2012e+12, 3.7899e+22],
        [5.2480e+05, 1.0175e+31, 9.7056e+24],
        [1.6283e+32, 3.7913e+22, 3.9653e+28],
        [1.0876e-19, 6.2027e+26, 2.3685e+21]])

Suffixing a method with _ (underscore) is a destructive process.

$ x = torch.tensor([5.5, 3])
$ y = torch.tensor([2.5, 5])
$ y.add_(x)
$ print(y)
tensor([8., 8.])

Very easy to exchange tensors between CPU and GPU.

if torch.cuda.is_available():
    device = torch.device("cuda")          
    y = torch.ones_like(x, device=device)  
    x = x.to(device)                       
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!

Recommended Posts

Introduction to PyTorch (1) Automatic differentiation
Introduction to Lightning pytorch
Introduction to Thano Function definition and automatic differentiation
[PyTorch Tutorial ②] Autograd: Automatic differentiation
[Details (?)] Introduction to pytorch ~ CNN CIFAR10 ~
Introduction to Scrapy (1)
Introduction to Scrapy (3)
Introduction to Supervisor
Introduction to Tkinter 1: Introduction
pytorch super introduction
[PyTorch] Introduction to document classification using BERT
Introduction to PyQt
Introduction to Scrapy (2)
[Python] Introduction to CNN with Pytorch MNIST
[Linux] Introduction to Linux
[Introduction to Pytorch] I played with sinGAN ♬
Introduction to Scrapy (4)
Introduction to discord.py (2)
Introduction to discord.py
[Super Introduction to Machine Learning] Learn Pytorch tutorials
[PyTorch] Introduction to Japanese document classification using BERT
[Super Introduction to Machine Learning] Learn Pytorch tutorials
Introduction to Web Scraping
Introduction to Nonparametric Bayes
Introduction to EV3 / MicroPython
Introduction to Python language
Introduction to TensorFlow-Image Recognition
Introduction to OpenCV (python)-(2)
[Pytorch] numpy to tensor
Introduction to PyQt4 Part 1
Introduction to Dependency Injection
Introduction to Private Chainer
PyTorch introduction (virtual environment)
PyTorch Super Introduction PyTorch Basics
Introduction to machine learning
[Introduction to Pytorch] I tried categorizing Cifar10 with VGG16 ♬
AOJ Introduction to Programming Topic # 1, Topic # 2, Topic # 3, Topic # 4
Introduction to electronic paper modules
A quick introduction to pytest-mock
Introduction to dictionary lookup algorithm
Introduction to Monte Carlo Method
opencv-python Introduction to image processing
Introduction to Python Django (2) Win
Introduction to Cython Writing [Notes]
An introduction to private TensorFlow
Kubernetes Scheduler Introduction to Homebrew
An introduction to machine learning
[Introduction to cx_Oracle] Overview of cx_Oracle
A super introduction to Linux
AOJ Introduction to Programming Topic # 7, Topic # 8
[Introduction to pytorch-lightning] First Lit ♬
[Introduction to pytorch] Preprocessing by audio I / O and torch audio (> <;)
Introduction to Anomaly Detection 1 Basics
Introduction to RDB with sqlalchemy Ⅰ
[Introduction to Systre] Fibonacci Retracement ♬
Introduction to Nonlinear Optimization (I)
Introduction to serial communication [Python]
AOJ Introduction to Programming Topic # 5, Topic # 6
Introduction to Deep Learning ~ Learning Rules ~
[Introduction to Python] <list> [edit: 2020/02/22]
Introduction to Python (Python version APG4b)