Skip to content

Commit bb16fcb

Browse files
authored
Make misc improvements to quickstart (#43)
1 parent fc655da commit bb16fcb

File tree

4 files changed

+128
-113
lines changed

4 files changed

+128
-113
lines changed

beginner_source/quickstart/autograd_tutorial.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"""
2020

2121
import torch
22+
2223
x = torch.ones(5) # input tensor
2324
y = torch.zeros(3) # expected output
2425
w = torch.randn(5, 3, requires_grad=True)
@@ -55,7 +56,8 @@
5556
# documentation <https://pytorch.org/docs/stable/autograd.html#function>`__.
5657
#
5758

58-
print(z.grad_fn, loss.grad_fn, sep='\n')
59+
print('Gradient function for z =',z.grad_fn)
60+
print('Gradient function for loss =', loss.grad_fn)
5961

6062
######################################################################
6163
# Computing Gradients
@@ -152,8 +154,9 @@
152154
#
153155

154156
x = torch.zeros(2, requires_grad=True)
155-
def f(x): return (x-torch.tensor([3, -2])).pow(2).sum()
156157

158+
def f(x):
159+
return (x-torch.tensor([3, -2])).pow(2).sum()
157160

158161
lr = 0.1
159162

beginner_source/quickstart/dataquickstart_tutorial.py

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#
2424
# If not properly organized, code for processing data samples can quickly get messy and become hard to maintain. Since different model architectures can be applied to many data types, we ideally want our dataset code to be decoupled from our model training code. To this end, PyTorch provides a simple Datasets interface for linking managing collections of data.
2525
#
26-
# A whole set of example datasets such as Fashion MNIST that implement this interface are built into PyTorch extension libraries. They are subclasses of torch.utils.data.Dataset that have parameters and functions specific to the type of data and the particular dataset. The actual data samples can be downloaded from the internet.These are useful for benchmarking and testing your models before training on your own custom datasets.
26+
# A whole set of example datasets such as Fashion MNIST that implement this interface are built into PyTorch extension libraries. They are subclasses of `torch.utils.data.Dataset` that have parameters and functions specific to the type of data and the particular dataset. The actual data samples can be downloaded from the internet. These are useful for benchmarking and testing your models before training on your own custom datasets.
2727
#
2828
# You can find some of them below.
2929
#
@@ -36,7 +36,7 @@
3636
# Iterating through a Dataset
3737
# -----------------
3838
#
39-
# Once we have a Dataset we can index it manually like a list `clothing[index]`.
39+
# Once we have a Dataset ``ds``, we can index it manually like a list: ``ds[index]``.
4040
#
4141
# Here is an example of how to load the `Fashion-MNIST <https://research.zalando.com/welcome/mission/research-projects/fashion-mnist/>`_ dataset from torch vision.
4242
# `Fashion-MNIST <https://research.zalando.com/welcome/mission/research-projects/fashion-mnist/>`_ is a dataset of Zalando’s article images consisting of of 60,000 training examples and 10,000 test examples.
@@ -60,23 +60,24 @@
6060
cols, rows = 3, 3
6161
for i in range(1, cols*rows +1):
6262
sample_idx = np.random.randint(len(clothing))
63-
img = clothing[sample_idx][0][0,:,:]
63+
img = clothing[sample_idx][0]
6464
figure.add_subplot(rows, cols, i)
6565
plt.title(labels_map[clothing[sample_idx][1]])
6666
plt.axis('off')
6767
plt.imshow(img, cmap='gray')
6868
plt.show()
6969

7070
#################################################################
71-
# .. figure:: /_static/img/quickstart/fashion_mnist.png
71+
# ..
72+
# .. figure:: /_static/img/quickstart/fashion_mnist.png
7273
# :alt: fashion_mnist
7374
#
7475

7576
#################################################################
7677
# Creating a Custom Dataset
7778
# -----------------
7879
#
79-
# To work with your own data lets look at the a simple custom image Dataset implementation:
80+
# To work with your own data, we need to implement a custom class that inherits from ``Dataset```. Let's look at a custom image dataset implementation. In this example, we have a number of images stored in a directory, and their labels stored separately in CSV annotation file.
8081
#
8182

8283
import os
@@ -114,7 +115,7 @@ def __getitem__(self, idx):
114115
# Imports
115116
# -------
116117
#
117-
# Import os for file handling, torch for PyTorch, `pandas <https://pandas.pydata.org/>`_ for loading labels, `torch vision <https://pytorch.org/blog/pytorch-1.7-released/>`_ to read image files, and Dataset to implement the Dataset interface.
118+
# Import `os` for file handling, torch for PyTorch, `pandas <https://pandas.pydata.org/>`_ for loading labels, `torch vision <https://pytorch.org/blog/pytorch-1.7-released/>`_ to read image files, and Dataset to implement the Dataset interface.
118119
#
119120
# Example:
120121
#
@@ -130,16 +131,13 @@ def __getitem__(self, idx):
130131
# Init
131132
# -----------------
132133
#
133-
# The init function is used for all the first time operations when our Dataset is loaded. In this case we use it to load our annotation labels to memory and the keep track of directory of our image file. Note that different types of data can take different init inputs you are not limited to just an annotations file, directory_path and transforms but for images this is a standard practice.
134-
# A sample csv annotations file may look as follows:
135-
#
136-
# tshirt1.jpg, 0
137-
#
138-
# tshirt2.jpg, 0
134+
# The init function is used for all the first time operations when our Dataset is loaded. In this case we use it to load our annotation labels to memory and the keep track of directory of our image file. Note that different types of data can take different init inputs. You are not limited to just an annotations file, directory path and transforms, but for images this is a standard practice.
135+
# A sample csv annotations file may look as follows: ::
139136
#
137+
# tshirt1.jpg, 0
138+
# tshirt2.jpg, 0
140139
# ......
141-
#
142-
# ankleboot999.jpg, 9
140+
# ankleboot999.jpg, 9
143141
#
144142
# Example:
145143
#
@@ -153,7 +151,7 @@ def __init__(self, annotations_file, img_dir, transform=None):
153151
# __len__
154152
# -----------------
155153
#
156-
# The __len__ function is very simple here we just need to return the number of samples in our dataset.
154+
# The __len__ function is very simple, we just need to return the number of samples in our dataset.
157155
#
158156
# Example:
159157

@@ -164,9 +162,9 @@ def __len__(self):
164162
# __getitem__
165163
# -----------------
166164
#
167-
# The __getitem__ function is the most important function in the Datasets interface this. It takes a tensor or an index as input and returns a loaded sample from you dataset at from the given indecies.
165+
# The __getitem__ function is the most important function in the Datasets interface. It takes a tensor or an index as input and returns a loaded sample from you dataset at the given indices.
168166
#
169-
# In this sample if provided a tensor we convert the tensor to a list containing our index. We then load the file at the given index from our image directory as well as the image label from our pandas annotations DataFrame. This image and label are then wrapped in a single sample dictionary which we can apply a Transform on and return. To learn more about Transforms see the next section of the Blitz.
167+
# If provided a tensor as an index, we convert the tensor to a list first. We then load the file at the given index from our image directory, as well as the image label from our pandas annotations DataFrame. This image and label are then wrapped in a single sample dictionary which we can apply a Transform on and return. To learn more about Transforms see the next section of the Blitz.
170168
#
171169
# Example:
172170
#
@@ -190,16 +188,17 @@ def __getitem__(self, idx):
190188
# Now we have a organized mechansim for managing data which is great, but there is still a lot of manual work we would have to do train a model with our Dataset.
191189
#
192190
# For example we would have to manually maintain the code for:
191+
#
193192
# * Batching
194193
# * Suffling
195194
# * Parallel batch distribution
196195
#
197-
# The PyTorch Dataloader *torch.utils.data.DataLoader* is an iterator that handles all of this complexity for us enabling us to load a dataset and focusing on train our model.
196+
# The PyTorch Dataloader ``torch.utils.data.DataLoader`` is an iterator that handles all of this complexity for us, enabling us to load a dataset and focus on training our model.
198197

199198
dataloader = DataLoader(clothing, batch_size=4, shuffle=True, num_workers=0)
200199

201200
#################################################################
202-
# With this we have all we need to know to load an process data of any kind in PyTorch to train deep learning models.
201+
# With this we have all we need to know to load and process data of any kind in PyTorch to train deep learning models.
203202
#
204203
# Next: Learn more about how to `transform that data for training <transforms_tutorial.html>`_.
205204
#

beginner_source/quickstart/tensor_tutorial.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
similar to **NumPy array**, and supports similar operations. However,
99
there are two very important features of Torch tensors that make them
1010
especially useful for training large-scale neural networks:
11-
- Tensor operations can be performed on GPUs or other specialized hardware to accelerate computing
12-
- Tensor operations support automatic differentiation using
13-
`pytorch.autograd engine <autograd_tutorial.html>`__
11+
12+
* Tensor operations can be performed on GPUs or other specialized hardware to accelerate computing
13+
* Tensor operations support automatic differentiation using `pytorch.autograd engine <autograd_tutorial.html>`__
1414
Conversion between Torch tensors and NumPy arrays can be done easily:
1515
"""
1616

@@ -178,9 +178,9 @@
178178
# using different dimensions:
179179
#
180180

181-
print(x.size()) # original size of x is 3x5
182-
print(x.view(5, 3, 1).size()) # will give size 5x3x1
183-
print(x.view(5, -1)) # will result in size 5x3
181+
print('Original size of x =',x.size()) # original size of x is 3x5
182+
print('Size after reshaping is',x.view(5, 3, 1).size()) # will give size 5x3x1
183+
print('Reshaped tensor:\n',x.view(5, -1)) # will result in size 5x3
184184

185185

186186
######################################################################
@@ -205,7 +205,9 @@
205205
#
206206

207207
print(x.size()) # original size of x is 3x5
208-
print(x[0].size(), x[:, 0].size(), x[..., 1].size()) # will give 5, 3, 3
208+
print('First row: ',x[0])
209+
print('First column: ', x[:, 0])
210+
print('Last column:', x[..., -1])
209211

210212

211213
######################################################################
@@ -215,7 +217,7 @@
215217
#
216218

217219
val = x.sum().item() # will compute the sum of all elements
218-
220+
print(val)
219221

220222
######################################################################
221223
# Hardware-Accelerated Computations
@@ -241,7 +243,6 @@
241243
y = torch.ones_like(x) # create tensor on CPU
242244
y = y.to(device) # move tensor to another device
243245
z = x+y # this is performed on GPU if it is available
244-
print(z)
245246
print(z.to("cpu", torch.double))
246247

247248

0 commit comments

Comments
 (0)