All of deep learning is computations on tensors, which are
generalizations of a matrix that can be indexed in more than 2
dimensions. We will see exactly what this means in-depth later. First,
let’s look what we can do with tensors.
Tensors can be created from Python lists with the torch.tensor()
function.
# torch.tensor(data) creates a torch.Tensor object with the given data.V_data=[1.,2.,3.]V=torch.tensor(V_data)print(V)# Creates a matrixM_data=[[1.,2.,3.],[4.,5.,6]]M=torch.tensor(M_data)print(M)# Create a 3D tensor of size 2x2x2.T_data=[[[1.,2.],[3.,4.]],[[5.,6.],[7.,8.]]]T=torch.tensor(T_data)print(T)
What is a 3D tensor anyway? Think about it like this. If you have a
vector, indexing into the vector gives you a scalar. If you have a
matrix, indexing into the matrix gives you a vector. If you have a 3D
tensor, then indexing into the tensor gives you a matrix!
A note on terminology:
when I say “tensor” in this tutorial, it refers
to any torch.Tensor object. Matrices and vectors are special cases of
torch.Tensors, where their dimension is 2 and 1 respectively. When I am
talking about 3D tensors, I will explicitly use the term “3D tensor”.
# Index into V and get a scalar (0 dimensional tensor)print(V[0])# Get a Python number from itprint(V[0].item())# Index into M and get a vectorprint(M[0])# Index into T and get a matrixprint
You can also create tensors of other data types. To create a tensor of integer types, try
torch.tensor([[1, 2], [3, 4]]) (where all elements in the list are integers).
You can also specify a data type by passing in dtype=torch.data_type.
Check the documentation for more data types, but
Float and Long will be the most common.
You can create a tensor with random data and the supplied dimensionality
with torch.randn()
Use the .view() method to reshape a tensor. This method receives heavy
use, because many neural network components expect their inputs to have
a certain shape. Often you will need to reshape before passing your data
to the component.
x=torch.randn(2,3,4)print(x)print(x.view(2,12))# Reshape to 2 rows, 12 columns# Same as above. If one of the dimensions is -1, its size can be inferredprint(x.view(2,-1))
The concept of a computation graph is essential to efficient deep
learning programming, because it allows you to not have to write the
back propagation gradients yourself. A computation graph is simply a
specification of how your data is combined to give you the output. Since
the graph totally specifies what parameters were involved with which
operations, it contains enough information to compute derivatives. This
probably sounds vague, so let’s see what is going on using the
fundamental flag requires_grad.
First, think from a programmers perspective. What is stored in the
torch.Tensor objects we were creating above? Obviously the data and the
shape, and maybe a few other things. But when we added two tensors
together, we got an output tensor. All this output tensor knows is its
data and shape. It has no idea that it was the sum of two other tensors
(it could have been read in from a file, it could be the result of some
other operation, etc.)
If requires_grad=True, the Tensor object keeps track of how it was
created. Let’s see it in action.
# Tensor factory methods have a ``requires_grad`` flagx=torch.tensor([1.,2.,3],requires_grad=True)# With requires_grad=True, you can still do all the operations you previously# couldy=torch.tensor([4.,5.,6],requires_grad=True)z=x+yprint(z)# BUT z knows something extra.print(z.grad_fn)
tensor([5., 7., 9.], grad_fn=<AddBackward0>)
<AddBackward0 object at 0x7f5785f2d480>
So Tensors know what created them. z knows that it wasn’t read in from
a file, it wasn’t the result of a multiplication or exponential or
whatever. And if you keep following z.grad_fn, you will find yourself at
x and y.
But how does that help us compute a gradient?
# Let's sum up all the entries in zs=z.sum()print(s)print(s.grad_fn)
tensor(21., grad_fn=<SumBackward0>)
<SumBackward0 object at 0x7f5785f2d660>
So now, what is the derivative of this sum with respect to the first
component of x? In math, we want
∂x0∂s
Well, s knows that it was created as a sum of the tensor z. z knows
that it was the sum x + y. So
s=x0+y0z0+x1+y1z1+x2+y2z2
And so s contains enough information to determine that the derivative
we want is 1!
Of course this glosses over the challenge of how to actually compute
that derivative. The point here is that s is carrying along enough
information that it is possible to compute it. In reality, the
developers of Pytorch program the sum() and + operations to know how to
compute their gradients, and run the back propagation algorithm. An
in-depth discussion of that algorithm is beyond the scope of this
tutorial.
Let’s have Pytorch compute the gradient, and see that we were right:
(note if you run this block multiple times, the gradient will increment.
That is because Pytorch accumulates the gradient into the .grad
property, since for many models this is very convenient.)
# calling .backward() on any variable will run backprop, starting from it.s.backward()print(x.grad)
tensor([1., 1., 1.])
Understanding what is going on in the block below is crucial for being a
successful programmer in deep learning.
x=torch.randn(2,2)y=torch.randn(2,2)# By default, user created Tensors have ``requires_grad=False``print(
False False
None
<AddBackward0 object at 0x7f5785f2d780>
True
None
You can also stop autograd from tracking history on Tensors
with .requires_grad=True by wrapping the code block in
withtorch.no_grad():