[PYTHON] [Note] [PyTorch] From installation to easy usage

Introduction

I decided to use a neural network in my research, so I decided to use PyTorch (brain death). I know it's popular, and I only know that I pushed the chainer made by PFN to the end of development (I wonder if this expression is correct ...), but I have never used it concretely. , Prepare the environment and use it for a while, and keep a record of it.

What is PyTorch?

PyTorch is a Python library developed by Facebook, mainly for neural networks (NN) (my bias). It is a convenient tool that helps to lower the hurdle of NN, such as quick calculation of NN by making GPU available and automatic differentiation system.

How to download

Writer's environment OS:Windows10 Python : Python3.7 Package : Anaconda IDE : Spyder4.1.4 GPU: Only the onboard one. NVIDIA is not pinched

First, get the pytorch installation command from the following WEB page.

PyTorch

Note that if you do not select the appropriate installation command, you will get an error.

The choices are

    1. Build (I checked it, but I'm not sure, so select the left according to other articles) 2.OS
    1. package
  1. language
  2. CUDA (It seems to be for using GPU) Five of. According to the author's environment, I made it as shown in the image below.

image.png

The Run this Command part at the bottom is the installation command. Then install it by following the steps below. ① Right-click the Anaconda prompt and select Other → Run as administrator to open it. ② Execute the following command

conda install pytorch torchvision cpuonly -c pytorch

This is OK

Note: It worked on my home desktop, but for some reason it didn't work on my laptop and I got the following error:

OSError:[WinError 126]The specified module cannot be found.

After searching by the error message, @ kunishou0903's this page [(What to do when "OSError: [WinError 126] The specified module cannot be found in import torch")](https://qiita.com I got to / kunishou0903 / items / b1789c446770c8c97613) and ran the following command, which was introduced in "Installing an older version of Pytorch" in this article, and it worked.

conda install pytorch==1.5.0 torchvision==0.6.0 -c pytorch

So don't forget that the laptop pytorch is an older version.

Easy to use

You can refer to matrix elements by row and column numbers, calculate averages, transpose, and do most of what you can do with numpy. Moreover, it can be converted to numpy. I don't use GPU, so I don't feel the benefits so much, but I thought that automatic differentiation is convenient, so I will write it below.

#Library
import torch

#Matrix creation
A = torch.tensor([[1,2],[3,4]]) 
B = torch.tensor([[4,3],[2,1]])

#Product of matrices
torch.mm(A,B)
###tensor([[ 8,  5],[20, 13]])###

#Automatic differentiation
x = torch.tensor(3.0, requires_grad = True) #Requires if you want X to be a variable_Set grad argument to True
                                            #The first 4 gives a point to X.
a = torch.tensor(2.0) #Tilt
b = 1.0               #Intercept
y = a*x + 1 #Function definition

print(y) # x =Value at 3
###tensor(7., grad_fn=<AddBackward0>)###

y.backward() #requires_Differentiation execution with variables with grad = True

print(x.grad) #Differentiated result
###tensor(2.)###

Quote source

[1]PyTorch [2] (What to do when "OS Error: [WinError 126] The specified module cannot be found in import torch") [3] PyTorch environment construction (Windows10, Anaconda3, Pycharm, Python3) I didn't quote anything directly, but please refer to this page as well. Did. [4] "Introduction to PyTorch development that can be used in the field, by Shaseibashi" ← I bought it. The best.

Recommended Posts

[Note] [PyTorch] From installation to easy usage
From easy git installation to docker startup python
From Elasticsearch installation to data entry
OpenMPI installation from download to pass-through
Installation and easy usage of pytest
Easy conversion from UTC to local time
Flask tutorial (from installation to hello world)
Introduction to Scapy ① (From installation to execution of Scapy)
Easy script migration from CentOS to Oracle Linux
[Ansible installation procedure] From installation to execution of playbook
[PyTorch] Installation method
Points to note when switching from NAOqi OS 2.4.3 to 2.5.5
Sum from 1 to 10
From ROS for Windows installation to operation check
An easy way to call Java from Python
RDKit easy installation
From Kafka to KSQL --Easy environment construction with docker
RaspberryPi3 (STRETCH) setup from OS installation to Hello World
Python development environment construction 2020 [From Python installation to poetry introduction]
Mercurial stopped working after migrating from Python 2 to Python 3 (Note)
Changes from Python 3.0 to Python 3.5
Changes from Python 2 to Python 3.0
Introduction to Lightning pytorch
Transition from WSL1 to WSL2
Easy to use Flask
[Pytorch] numpy to tensor
Note to daemonize python
Easy to use SQLite3
From editing to execution
[CentOS 7.7] From desktop environment installation to remote desktop connection possible (from minimum installation)
Explanation from installation of Sphinx to use of external theme (Bootswatch)
Points to note when deleting multiple elements from the List
RabbitMQ Tutorial (1) -From Installation to Hello World (Official Site Translation)