PyTorch: Difference between revisions

 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:


==Installation==
==Installation==
See [https://pytorch.org/get-started/locally/ PyTorch Getting Started]
See [https://pytorch.org/get-started/locally/ PyTorch Getting Started] and [https://pytorch.org/get-started/previous-versions/ PyTorch Previous Versions]
<syntaxhighlight lang="bash">


# If using conda, python 3.5+, and CUDA 10.0 (+ compatible cudnn)
I recommend using the conda installation method since it is paired with the correct version of cuda.
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
</syntaxhighlight>


==Getting Started==
==Getting Started==
* [https://pytorch.org/tutorials/ PyTorch Tutorials]
* [https://pytorch.org/tutorials/ PyTorch Tutorials]


{{hidden | Example |
<syntaxhighlight lang="python">
<syntaxhighlight lang="python">
import torch
import torch
import torch.nn as nn
import torch.nn as nn
model = nn.Sequential(nn.Linear(5, 5),nn.ReLU(),nn.Linear(5, 1))
criterion = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)


# Training
# Training
for epoch in range(epochs):
for epoch in range(epochs):
  running_loss = 0.0
     for i, data in enumerate(trainloader):
     for i, data in enumerate(trainloader, 0):
         # get the inputs; e.g. data is a list of [inputs, labels]
         # get the inputs; data is a list of [inputs, labels]
         inputs, labels = data
         inputs, labels = data


Line 26: Line 27:
         optimizer.zero_grad()
         optimizer.zero_grad()


         # forward + backward + optimize
         # forward
         outputs = net(inputs)
         outputs = model(inputs)
         loss = criterion(outputs, labels)
         loss = criterion(outputs, labels)
       
        # backward
         loss.backward()
         loss.backward()
         optimizer.step()
         optimizer.step()


</syntaxhighlight>
</syntaxhighlight>
}}


==Importing Data==
==Importing Data==
Line 38: Line 42:


==Usage==
==Usage==
Note that there are some useful functions under <code>torch.nn.functional</code> which is typically imported as <code>F</code>.
Note that there are several useful functions under <code>torch.nn.functional</code> which is typically imported as <code>F</code>.
Most neural network layers are actually implemented in functional.


===torch.meshgrid===
===torch.meshgrid===
Line 66: Line 71:
* In your class include all other modules you need during init.
* In your class include all other modules you need during init.
** If you have a list of modules, make sure to wrap them in <code>nn.ModuleList</code> or <code>nn.Sequential</code> so they are properly recognized.
** If you have a list of modules, make sure to wrap them in <code>nn.ModuleList</code> or <code>nn.Sequential</code> so they are properly recognized.
* Wrap any parameters for you model in <code>nn.Parameter(weight, requires_grad=True)</code>.
* Write a forward pass for your model.
* Write a forward pass for your model.


Line 71: Line 77:
See [https://pytorch.org/tutorials/beginner/former_torchies/parallelism_tutorial.html Multi-GPU Examples].
See [https://pytorch.org/tutorials/beginner/former_torchies/parallelism_tutorial.html Multi-GPU Examples].


==nn.DataParallel==
===nn.DataParallel===


The basic idea is to wrap blocks in [https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html#torch.nn.DataParallel <code>nn.DataParallel</code>].   
The basic idea is to wrap blocks in [https://pytorch.org/docs/stable/generated/torch.nn.DataParallel.html#torch.nn.DataParallel <code>nn.DataParallel</code>].   
Line 168: Line 174:
</syntaxhighlight>
</syntaxhighlight>


==PyTorch3D==
==Libraries==
A list of useful libraries
 
===torchvision===
https://pytorch.org/vision/stable/index.html
 
Official tools for image manipulation such as blur, bounding boxes.
 
===torchmetrics===
https://torchmetrics.readthedocs.io/en/stable/
 
Various metrics such as PSNR, SSIM, LPIPS
 
===PyTorch3D===
{{main | PyTorch3D}}
{{main | PyTorch3D}}
[https://github.com/facebookresearch/pytorch3d PyTorch3D] is a library by Facebook AI Research which contains differentiable renderers for meshes and point clouds
[https://github.com/facebookresearch/pytorch3d PyTorch3D]
It is built using custom CUDA kernels and only runs on Linux.
 
Facebook library with differentiable renderers for meshes and point clouds.