@@ -15,21 +15,6 @@ and the true output.
1515
1616: CONTENTS
1717
18- ## Warm-up: numpy
19-
20- Before introducing PyTorch, we will first implement the network using numpy.
21-
22- Numpy provides an n-dimensional array object, and many functions for manipulating
23- these arrays. Numpy is a generic framework for scientific computing; it does not
24- know anything about computation graphs, or deep learning, or gradients. However
25- we can easily use numpy to fit a two-layer network to random data by manually
26- implementing the forward and backward passes through the network using numpy
27- operations:
28-
29- ``` python
30- :INCLUDE tensor/ two_layer_net_numpy.py
31- ```
32-
3318## PyTorch: Tensors
3419
3520Numpy is a great framework, but it cannot utilize GPUs to accelerate its
@@ -130,14 +115,112 @@ will optimize the model using the Adam algorithm provided by the `optim` package
130115
131116## PyTorch: RNNs
132117
118+ TODO
133119
134- ## Data Loading
120+ ## PyTorch: Data Loading
135121
136122
137123``` python
138124:INCLUDE nn/ two_layer_net_optim.py
139125```
140126
127+ ## PyTorch for Torch Users
128+
129+ The non-autograd parts of pytorch will be quite familiar to torch users, but there are
130+ a few important changes to be aware of:
131+
132+ ** Inplace / Out-of-place**
133+
134+ The first difference is that ALL operations on the tensor that operate in-place on it will have an ** _ ** postfix.
135+ For example, ` add ` is the out-of-place version, and ` add_ ` is the in-place version.
136+
137+ ``` python
138+ a.fill_(3.5 )
139+ # a has now been filled with the value 3.5
140+
141+ b = a.add(4.0 )
142+ # a is still filled with 3.5
143+ # new tensor b is returned with values 3.5 + 4.0 = 7.5
144+ ```
145+
146+ Some operations like narrow do not have in-place versions, and hence, ` .narrow_ ` does not exist.
147+ Similarly, some operations like ` fill_ ` do not have an out-of-place version, so ` .fill ` does not exist.
148+
149+ ** Zero Indexing**
150+
151+ Another difference is that Tensors are zero-indexed. (Torch tensors are one-indexed)
152+
153+ ``` python
154+ b = a[0 ,3 ] # select 1st row, 4th column from a
155+ ```
156+
157+ Tensors can be also indexed with Python's slicing
158+
159+ ``` python
160+ b = a[:,3 :5 ] # selects all rows, columns 3 to 5
161+ ```
162+
163+ ** No camel casing**
164+
165+ The next small difference is that all functions are now NOT camelCase anymore.
166+ For example ` indexAdd ` is now called ` index_add_ `
167+
168+ ``` python
169+ x = torch.ones(5 , 5 )
170+ print (x)
171+ z = torch.Tensor(5 , 2 )
172+ z[:,0 ] = 10
173+ z[:,1 ] = 100
174+ print (z)
175+ x.index_add_(1 , torch.LongTensor([4 ,0 ]), z)
176+ print (x)
177+ ```
178+
179+ ** Numpy Bridge**
180+
181+ Converting a torch Tensor to a numpy array and vice versa is a breeze.
182+ The torch Tensor and numpy array will share their underlying memory, and changing one will change the other.
183+
184+ * Converting torch Tensor to numpy Array*
185+
186+ ``` python
187+ a = torch.ones(5 )
188+ print (a)
189+ b = a.numpy()
190+ print (b)
191+ a.add_(1 )
192+ print (a)
193+ print (b) # see how the numpy array changed in value
194+ ```
195+
196+ * Converting numpy Array to torch Tensor*
197+
198+ ``` python
199+ import numpy as np
200+ a = np.ones(5 )
201+ b = torch.DoubleTensor(a)
202+ np.add(a, 1 , out = a)
203+ print (a)
204+ print (b) # see how changing the np array changed the torch Tensor automatically
205+ ```
206+
207+ All the Tensors on the CPU except a CharTensor support converting to NumPy and back.
208+
209+ ** CUDA Tensors**
210+
211+ CUDA Tensors are nice and easy in pytorch, and they are much more consistent as well.
212+ Transfering a CUDA tensor from the CPU to GPU will retain it's type.
213+
214+ ``` python
215+ # creates a LongTensor and transfers it
216+ # to GPU as torch.cuda.LongTensor
217+ a = torch.LongTensor(10 ).fill_(3 ).cuda()
218+ print (type (a))
219+ b = a.cpu()
220+ # transfers it to CPU, back to
221+ # being a torch.LongTensor
222+ ```
223+
141224# Advanced Topics
142225
143226## PyTorch: Defining new autograd functions
@@ -194,9 +277,9 @@ fit a simple two-layer net:
194277:INCLUDE autograd/ tf_two_layer_net.py
195278```
196279
197- # HOGWILD
280+ # PyTorch: HOGWILD
198281
199- FIXME
282+ TODO @ sgross
200283
201284## PyTorch: Control Flow + Weight Sharing
202285As an example of dynamic graphs and weight sharing, we implement a very strange
0 commit comments