APPENDIX B
Intro to PyTorch
In this appendix, you will be introduced to the PyTorch framework along with the
functionality that it offers. PyTorch is more involved than Keras is, and it is a lower-level
framework (meaning there’s more syntax, and elements aren’t abstracted away from you
like in Keras).
Regarding the setup, we use
• Torch version 0.4.1 (PyTorch)
• CUDA version 9.0.176
• cuDNN version 7.3.0.29
What Is PyTorch?
PyTorch is a deep learning library for Python, developed by artificial-intelligence
researchers at Facebook and based on the Torch library. While PyTorch is also a low-
level language like TensorFlow, it is easier to pick up because of the huge difference in
syntax. TensorFlow has a much steeper learning curve, and you have to define a lot more
elements than in PyTorch.
TensorFlow at the moment far surpasses PyTorch in how much community support
it has, and this is primarily because PyTorch is a relatively new framework. Although you
will find more resources for TensorFlow, more and more people are switching to PyTorch
due to it being more intuitive while still offering practically the same functionality as
TensorFlow (though TensorFlow does have some functions that PyTorch does not,
you can easily implement those functions in PyTorch if you know what the logic is; an
example of this is arctanh function).
In the end, it is mostly a matter of personal preference when deciding to use
TensorFlow or PyTorch. Depending on the context of your work, one framework might
be more suitable than the other.
362
That being said, PyTorch might be easier to use for research purposes, considering
that it is easier to prototype in due to the lessened burden from the syntax. On the other
hand, TensorFlow has more resources and the advantage of having TensorBoard. It
is also better suited for cross-platform compatibility, since a model can be trained in
Python but deployed in Java, for example, allowing for better scalability. If loading and
saving models is a priority, perhaps TensorFlow is more suitable. Again, it all comes
down to personal preference, since there’s usually a workaround for many of the
problems that both frameworks might face.
Using PyTorch
This section will be a bit different from the previous appendix. Here, we will demonstrate
how some basic tensor operations are done, and then move on to illustrating how to
use PyTorch by exploring PyTorch equivalent models of the temporal convolutional
networks in Chapter
7
.
First, let’s begin by looking at some simple tensor operations. If you would like to
know more about the framework itself and the functionality that it supports, check out
the documentation at
https://pytorch.org/docs/0.4.1/index.html
and the code implementation at
https://github.com/pytorch/pytorch
.
Let’s begin (see Figure
B-1
).
appendix B intro to pytorch
363
With PyTorch, you can see that the data values like the tensors are some sort of array,
unlike in TensorFlow. In TensorFlow, you must run the variable through a session to be
able to see the data values.
In comparison, Figure
B-2
shows TensorFlow.
Figure B-1. A series of tensor operations in PyTorch. The code shows the operation
and the output shows the results after the operations were performed on the
corresponding tensors
appendix B intro to pytorch
364
PyTorch has much more functionality in how you can manipulate tensors, so it’s
worth checking out the documentation if you haven’t.
Now, let’s move on to creating a PyTorch model in a somewhat advanced, but
organized format. Splitting up the definition of the model, the training process, and the
testing process into their respective parts will help you understand how these models are
created, trained, and evaluated.
You start by applying a convolutional neural network to the MNIST data set in order
to showcase the more customizable format of training.
As usual, you begin with your imports (see Figure
B-3
and Figure
B-4
).
Figure B-2. Some tensor operations conducted in TensorFlow. Note that to
actually see results, you need to pass everything through a TensorFlow session
appendix B intro to pytorch
365
In Chapter
3
, the code was introduced in a manner similar to basic Keras formatting,
so you defined the hyperparameters and loaded your data sets (data loaders in this case)
right after importing the modules you need.
Instead, you will now define the model (see Figure
B-5
and Figure
B-6
).
Do'stlaringiz bilan baham: |