Skip to content

Commit 73baa93

Browse files
authored
Merge pull request animator#583 from MrCodYrohit/main
Added PyTorch.md
2 parents 87851ce + ce2f710 commit 73baa93

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

contrib/machine-learning/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
- [Support Vector Machine Algorithm](support-vector-machine.md)
88
- [Artificial Neural Network from the Ground Up](ArtificialNeuralNetwork.md)
99
- [TensorFlow.md](tensorFlow.md)
10+
- [PyTorch.md](pytorch.md)
1011
- [Types of optimizers](Types_of_optimizers.md)

contrib/machine-learning/pytorch.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# PyTorch: A Comprehensive Overview
2+
3+
## Introduction
4+
PyTorch is an open-source deep learning framework developed by Facebook's AI Research lab. It provides a flexible and efficient platform for building and deploying machine learning models. PyTorch is known for its dynamic computational graph, ease of use, and strong support for GPU acceleration.
5+
6+
## Key Features
7+
- **Dynamic Computational Graphs**: PyTorch's dynamic computation graph (or define-by-run) allows you to change the network architecture during runtime. This feature makes debugging and experimenting with different model architectures easier.
8+
- **GPU Acceleration**: PyTorch supports CUDA, enabling efficient computation on GPUs.
9+
- **Extensive Libraries and Tools**: PyTorch has a rich ecosystem of libraries and tools such as torchvision for computer vision, torchtext for natural language processing, and more.
10+
- **Community Support**: PyTorch has a large and active community, providing extensive resources, tutorials, and forums for support.
11+
12+
## Installation
13+
To install PyTorch, you can use pip:
14+
15+
```sh
16+
pip install torch torchvision
17+
```
18+
19+
For detailed installation instructions, including GPU support, visit the [official PyTorch installation guide](https://pytorch.org/get-started/locally/).
20+
21+
## Basic Usage
22+
23+
### Tensors
24+
Tensors are the fundamental building blocks in PyTorch. They are similar to NumPy arrays but can run on GPUs.
25+
26+
```python
27+
import torch
28+
29+
# Creating a tensor
30+
x = torch.tensor([1.0, 2.0, 3.0])
31+
print(x)
32+
33+
# Performing basic operations
34+
y = torch.tensor([4.0, 5.0, 6.0])
35+
z = x + y
36+
print(z)
37+
```
38+
39+
### Autograd
40+
Autograd is PyTorch's automatic differentiation engine that powers neural network training. It tracks operations on tensors to automatically compute gradients.
41+
42+
```python
43+
# Requires gradient
44+
x = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
45+
46+
# Perform operations
47+
y = x ** 2
48+
z = y.sum()
49+
50+
# Compute gradients
51+
z.backward()
52+
print(x.grad)
53+
```
54+
55+
### Building Neural Networks
56+
PyTorch provides the `torch.nn` module to build neural networks.
57+
58+
```python
59+
import torch
60+
import torch.nn as nn
61+
import torch.optim as optim
62+
63+
# Define a simple neural network
64+
class SimpleNN(nn.Module):
65+
def __init__(self):
66+
super(SimpleNN, self).__init__()
67+
self.fc1 = nn.Linear(3, 1)
68+
69+
def forward(self, x):
70+
x = self.fc1(x)
71+
return x
72+
73+
# Create the network, define the criterion and optimizer
74+
model = SimpleNN()
75+
criterion = nn.MSELoss()
76+
optimizer = optim.SGD(model.parameters(), lr=0.01)
77+
78+
# Dummy input and target
79+
inputs = torch.tensor([[1.0, 2.0, 3.0]])
80+
targets = torch.tensor([[0.5]])
81+
82+
# Forward pass
83+
outputs = model(inputs)
84+
loss = criterion(outputs, targets)
85+
86+
# Backward pass and optimization
87+
loss.backward()
88+
optimizer.step()
89+
90+
print(f'Loss: {loss.item()}')
91+
```
92+
93+
## When to Use PyTorch
94+
### Use PyTorch When:
95+
1. **Research and Development**: PyTorch's dynamic computation graph makes it ideal for experimentation and prototyping.
96+
2. **Computer Vision and NLP**: With extensive libraries like torchvision and torchtext, PyTorch is well-suited for these domains.
97+
3. **Custom Operations**: If your work involves custom layers or operations, PyTorch provides the flexibility to implement and integrate them easily.
98+
4. **Community and Ecosystem**: If you prefer a strong community support and extensive third-party resources, PyTorch is a good choice.
99+
100+
### Consider Alternatives When:
101+
1. **Production Deployment**: While PyTorch has made strides in deployment (e.g., TorchServe), TensorFlow's TensorFlow Serving is more mature for large-scale deployment.
102+
2. **Static Graphs**: If your model architecture doesn't change frequently and you prefer static computation graphs, TensorFlow might be more suitable.
103+
3. **Multi-Language Support**: If you need integration with languages other than Python (e.g., Java, JavaScript), TensorFlow offers better support.
104+
105+
## Conclusion
106+
PyTorch is a powerful and flexible deep learning framework that caters to both researchers and practitioners. Its ease of use, dynamic computation graph, and strong community support make it an excellent choice for many machine learning tasks. However, for certain production scenarios or specific requirements, alternatives like TensorFlow may be more appropriate.
107+
108+
## Additional Resources
109+
- [PyTorch Official Documentation](https://pytorch.org/docs/stable/index.html)
110+
- [PyTorch Tutorials](https://pytorch.org/tutorials/)
111+
- [PyTorch Forum](https://discuss.pytorch.org/)
112+
113+
Feel free to explore and experiment with PyTorch to harness the full potential of this versatile framework!

0 commit comments

Comments
 (0)