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.
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.
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.
Note that if you do not select the appropriate installation command, you will get an error.
The choices are
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.
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.)###
[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