PyTorch: Difference between revisions

Content deleted Content added
Example: Simplify call to super().__init__().
m History: spell out acronym, add link, improve wording
 
(41 intermediate revisions by 29 users not shown)
Line 1:
{{Short description|Open source machineMachine learning library}}
{{Use dmy dates|date=April 2025}}
{{Infobox software
| name = PyTorch
Line 8 ⟶ 9:
| author = {{Unbulleted list|Adam Paszke|Sam Gross|Soumith Chintala|Gregory Chanan}}
| developer = [[Meta AI]]
| released = {{Start date and age|2016|9|df=yes}}<ref>{{cite web|url=https://github.com/pytorch/pytorch/releases/tag/v0.1.1|title=PyTorch Alpha-1 release|last=Chintala|first=Soumith|website=[[GitHub]]|date=1 September 2016|access-date=19 August 2020|archive-date=29 August 2021|archive-url=https://web.archive.org/web/20210829055231/https://github.com/pytorch/pytorch/releases/tag/v0.1.1|url-status=live}}</ref>
| latest release version = {{wikidata|property|edit|reference|P348}}
| latest release date = {{start date and age|{{wikidata|qualifier|P348|P577}}}}
Line 17 ⟶ 18:
| language = English
| genre = [[Library (computing)|Library]] for [[machine learning]] and [[deep learning]]
| license = [[BSD-3]]<ref>{{cite web |last=Claburn |first=Thomas |date=12 September 2022 |title=PyTorch gets lit under The Linux Foundation |url=https://www.theregister.com/2022/09/12/pytorch_meta_linux_foundation/ |work=[[The Register]] |access-date=18 October 2022 |archive-date=18 October 2022 |archive-url=https://web.archive.org/web/20221018040848/https://www.theregister.com/2022/09/12/pytorch_meta_linux_foundation/ |url-status=live }}</ref>
| website = {{URL|https://pytorch.org/}}
}}
{{Machine learning}}
 
'''PyTorch''' is aan [[Open source|open-source]] [[machine learning]] [[Library (computing)|library]] based on the [[Torch (machine learning)|Torch]] library,<ref>{{cite news|url=https://www.infoworld.com/article/3159120/artificial-intelligence/facebook-brings-gpu-powered-machine-learning-to-python.html|title=Facebook brings GPU-powered machine learning to Python|last=Yegulalp|first=Serdar|date=19 January 2017|work=InfoWorld|access-date=11 December 2017|archive-date=12 July 2018|archive-url=https://web.archive.org/web/20180712054543/https://www.infoworld.com/article/3159120/artificial-intelligence/facebook-brings-gpu-powered-machine-learning-to-python.html|url-status=live}}</ref><ref>{{cite web|url=https://www.oreilly.com/ideas/why-ai-and-machine-learning-researchers-are-beginning-to-embrace-pytorch|title=Why AI and machine learning researchers are beginning to embrace PyTorch|last=Lorica|first=Ben|date=3 August 2017|publisher=O'Reilly Media|access-date=11 December 2017|archive-date=17 May 2019|archive-url=https://web.archive.org/web/20190517055218/https://www.oreilly.com/ideas/why-ai-and-machine-learning-researchers-are-beginning-to-embrace-pytorch|url-status=live}}</ref><ref>{{Cite book|title=Deep Learning with Python|last=Ketkar|first=Nikhil|date=2017|publisher=Apress, Berkeley, CA|isbn=9781484227657|pages=195–208|language=en|doi=10.1007/978-1-4842-2766-4_12|chapter=Introduction to PyTorch}}</ref> used for applications such as [[computer vision]], deep learning research<ref name=":0" /> and [[natural language processing]],<ref name=":0">{{Cite web|url=httphttps://dl4nlpwww.infodatacamp.com/en/latesttutorial/nlp-with-pytorch-a-comprehensive-guide|title=Natural Language Processing (NLP) with PyTorch: A NLPComprehensive withGuide|author=Moez PyTorchAli|date=Jun documentation2023|website=dl4nlpdatacamp.infocom|language=en|access-date=20171 April 2024|archive-12date=1 April 2024|archive-18url=https://web.archive.org/web/20240401214813/https://www.datacamp.com/tutorial/nlp-with-pytorch-a-comprehensive-guide|url-status=live}}</ref> originally developed by [[Meta AI]] and now part of the [[Linux Foundation]] umbrella.<ref>{{Cite news|url=https://www.oreilly.com/ideas/when-two-trends-fuse-pytorch-and-recommender-systems|title=When two trends fuse: PyTorch and recommender systems|last=Patel|first=Mo|date=7 December 2017-12-07|work=O'Reilly Media|access-date=2017-12-18 December 2017|language=en|archive-date=30 March 2019|archive-url=https://web.archive.org/web/20190330131436/https://www.oreilly.com/ideas/when-two-trends-fuse-pytorch-and-recommender-systems|url-status=live}}</ref><ref name=":1">{{Cite news|url=https://techcrunch.com/2017/09/07/facebook-and-microsoft-collaborate-to-simplify-conversions-from-pytorch-to-caffe2/|title=Facebook and Microsoft collaborate to simplify conversions from PyTorch to Caffe2|last=Mannes|first=John|work=[[TechCrunch]]|access-date=2017-12-18 December 2017|language=en|quote=FAIR is accustomed to working with PyTorch – a deep learning framework optimized for achieving state of the art results in research, regardless of resource constraints. Unfortunately in the real world, most of us are limited by the computational capabilities of our smartphones and computers.|archive-date=6 July 2020|archive-url=https://web.archive.org/web/20200706115906/https://techcrunch.com/2017/09/07/facebook-and-microsoft-collaborate-to-simplify-conversions-from-pytorch-to-caffe2/|url-status=live}}</ref><ref>{{Cite web|url=https://venturebeat.com/2017/11/29/tech-giants-are-using-open-source-frameworks-to-dominate-the-ai-community/|title=Tech giants are using open source frameworks to dominate the AI community|last=Arakelyan|first=Sophia|date=29 November 2017-11-29|website=[[VentureBeat]]|language=en-US|access-date=18 December 2017|archive-12date=30 March 2019|archive-18url=https://web.archive.org/web/20190330131432/https://venturebeat.com/2017/11/29/tech-giants-are-using-open-source-frameworks-to-dominate-the-ai-community/|url-status=live}}</ref><ref>{{Cite web |title=PyTorch strengthens its governance by joining the Linux Foundation |url=https://pytorch.org/blog/PyTorchfoundation/ |access-date=2022-09-13 September 2022 |website=pytorch.org |language=en}}</ref> It is recognized as one of the two most popular machine[[deep learning]] librariesframeworks, alongside others such as [[TensorFlow]],<ref>{{Cite web|url=https://github.com/cncf/velocity|title=Top 30 Open Source Projects.|website=Open Source Project Velocity by CNCF|access-date=12 October 2023|archive-date=3 September 2023|archive-url=https://web.archive.org/web/20230903024925/https://github.com/cncf/velocity|url-status=live}}</ref> offering [[free and open-source software]] released under the [[modified BSD license]]. Although the [[Python (programming language)|Python]] interface is more polished and the primary focus of development, PyTorch also has a [[C++]] interface.<ref>{{Cite web|url=https://pytorch.org/cppdocs/frontend.html|title=The C++ Frontend|website=PyTorch Master Documentation|access-date=29 July 2019|archive-07-date=29 July 2019|archive-url=https://web.archive.org/web/20190729202037/https://pytorch.org/cppdocs/frontend.html|url-status=live}}</ref>
 
PyTorch utilises [[tensor]]s as a intrinsic datatype, very similar to [[NumPy]]. Model training is handled by an [[automatic differentiation]] system, Autograd, which constructs a [[directed acyclic graph]] of a forward pass of a model for a given input, for which automatic differentiation utilising the [[chain rule]], computes model-wide gradients.<ref>{{Cite web|title=Overview of PyTorch Autograd Engine|website=PyTorch Blog|date=8 June 2021|url=https://pytorch.org/blog/overview-of-pytorch-autograd-engine|url-status=live}}</ref> PyTorch is capable of transparent leveraging of [[SIMD]] units, such as [[General-purpose computing on graphics processing units|GPGPU]]s.
A number of pieces of [[deep learning]] software are built on top of PyTorch, including [[Tesla Autopilot]],<ref>{{Cite web|last=Karpathy|first=Andrej|title=PyTorch at Tesla - Andrej Karpathy, Tesla|url=https://www.youtube.com/watch?v=oBklltKXtDE}}</ref> [[Uber]]'s Pyro,<ref>{{Cite news|url=https://eng.uber.com/pyro/|title=Uber AI Labs Open Sources Pyro, a Deep Probabilistic Programming Language|date=2017-11-03|work=Uber Engineering Blog|access-date=2017-12-18|language=en-US}}</ref> Hugging Face's Transformers,<ref>{{Citation|title=PYTORCH-TRANSFORMERS: PyTorch implementations of popular NLP Transformers|date=2019-12-01|url=https://pytorch.org/hub/huggingface_pytorch-transformers/|publisher=PyTorch Hub|access-date=2019-12-01}}</ref> [[PyTorch Lightning]],<ref>{{Citation|title=PYTORCH-Lightning: The lightweight PyTorch wrapper for ML researchers. Scale your models. Write less boilerplate|date=2020-06-18|url=https://github.com/PyTorchLightning/pytorch-lightning/|publisher=Lightning-Team|access-date=2020-06-18}}</ref><ref>{{Cite web|url=https://pytorch.org/ecosystem/|title=Ecosystem Tools|website=pytorch.org|language=en|access-date=2020-06-18}}</ref> and Catalyst.<ref>{{Citation|title=GitHub - catalyst-team/catalyst: Accelerated DL & RL|date=2019-12-05|url=https://github.com/catalyst-team/catalyst|publisher=Catalyst-Team|access-date=2019-12-05}}</ref><ref>{{Cite web|url=https://pytorch.org/ecosystem/|title= Ecosystem Tools|website=pytorch.org|language=en|access-date=2020-04-04}}</ref>
 
A number of pieces ofcommercial [[deep learning]] softwarearchitectures are built on top of PyTorch, including [[Tesla Autopilot]],<ref>{{Cite web|last=Karpathy|first=Andrej|title=PyTorch at Tesla - Andrej Karpathy, Tesla|website=[[YouTube]]|date=6 November 2019|url=https://www.youtube.com/watch?v=oBklltKXtDE|access-date=2 June 2020|archive-date=24 March 2023|archive-url=https://web.archive.org/web/20230324144838/https://www.youtube.com/watch?v=oBklltKXtDE|url-status=live}}</ref> [[Uber]]'s Pyro,<ref>{{Cite news|url=https://eng.uber.com/pyro/|title=Uber AI Labs Open Sources Pyro, a Deep Probabilistic Programming Language|date=3 November 2017-11-03|work=Uber Engineering Blog|access-date=2017-12-18 December 2017|language=en-US|archive-date=25 December 2017|archive-url=https://web.archive.org/web/20171225034106/https://eng.uber.com/pyro/|url-status=live}}</ref> [[Hugging Face]]'s Transformers,<ref>{{Citation|title=PYTORCH-TRANSFORMERS: PyTorch implementations of popular NLP Transformers|date=1 December 2019-12-01|url=https://pytorch.org/hub/huggingface_pytorch-transformers/|publisher=PyTorch Hub|access-date=2019-12-01}}</ref>1 [[PyTorchDecember Lightning]],<ref>{{Citation2019|titlearchive-date=PYTORCH-Lightning:11 TheJune lightweight PyTorch wrapper for ML researchers. Scale your models. Write less boilerplate2023|date=2020archive-06-18|url=https://githubweb.comarchive.org/web/20230611061047/https:/PyTorchLightning/pytorch.org/hub/huggingface_pytorch-lightningtransformers/|publisher=Lightningurl-Team|access-datestatus=2020-06-18live}}</ref><ref>{{Cite web|url=https://pytorch.org/ecosystem/|title=Ecosystem Tools|website=pytorch.org|language=en|access-date=18 June 2020|archive-06-date=18 July 2023|archive-url=https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/|url-status=live}}</ref> and Catalyst.<ref>{{Citation|title=GitHub - catalyst-team/catalyst: Accelerated DL & RL|date=5 December 2019-12-05|url=https://github.com/catalyst-team/catalyst|publisher=Catalyst-Team|access-date=5 December 2019|archive-12date=22 December 2019|archive-05url=https://web.archive.org/web/20191222162045/https://github.com/catalyst-team/catalyst|url-status=live}}</ref><ref>{{Cite web|url=https://pytorch.org/ecosystem/|title= Ecosystem Tools|website=pytorch.org|language=en|access-date=4 April 2020|archive-04date=18 July 2023|archive-04url=https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/|url-status=live}}</ref>
PyTorch provides two high-level features:<ref>{{cite web |url=https://pytorch.org/about/ |title=PyTorch – About |website=pytorch.org |access-date=2018-06-11 |archive-url=https://web.archive.org/web/20180615190804/https://pytorch.org/about/ |archive-date=2018-06-15 |url-status=dead }}</ref>
* Tensor computing (like [[NumPy]]) with strong acceleration via [[graphics processing unit]]s (GPU)
* [[Deep neural networks]] built on a tape-based [[automatic differentiation]] system
 
==History==
In 2001, Torch was written and released under a [[GNU General Public License|GPL license]]. It was a machine-learning library written in C++, supporting methods including neural networks, [[Support vector machine|support vector machines]] (SVM), [[Hidden Markov model|hidden Markov models]], etc.<ref>[http://torch.ch/torch3/matos/tutorial.pdf "Torch Tutorial", Ronan Collobert, IDIAP, 2002-10-02]</ref><ref>R. Collobert, S. Bengio and J. Mariéthoz. [https://infoscience.epfl.ch/server/api/core/bitstreams/7513f344-91b6-427d-a020-7836b150a150/content Torch: a modular machine learning software library]. Technical Report IDIAP-RR 02-46, IDIAP, 2002. </ref><ref>https://web.archive.org/web/20011031104036/http://www.torch.ch/</ref> It was improved to Torch7 in 2012.<ref>{{Citation |last=Collobert |first=Ronan |title=Implementing Neural Networks Efficiently |date=2012 |work=Neural Networks: Tricks of the Trade: Second Edition |pages=537–557 |editor-last=Montavon |editor-first=Grégoire |url=https://doi.org/10.1007/978-3-642-35289-8_28 |access-date=2025-06-10 |place=Berlin, Heidelberg |publisher=Springer |language=en |doi=10.1007/978-3-642-35289-8_28 |isbn=978-3-642-35289-8 |last2=Kavukcuoglu |first2=Koray |last3=Farabet |first3=Clément |editor2-last=Orr |editor2-first=Geneviève B. |editor3-last=Müller |editor3-first=Klaus-Robert|url-access=subscription }}</ref> Development on Torch ceased in 2018 and was subsumed by the PyTorch project.<ref>[https://github.com/torch/torch7/commit/fd0ee3bbf7bfdd21ab10c5ee70b74afaef9409e1 torch/torch7, Commit fd0ee3b, 2018-07-02]</ref>
Meta (formerly known as Facebook) operates both ''PyTorch'' and ''Convolutional Architecture for Fast Feature Embedding'' ([[Caffe (software)|Caffe2]]), but models defined by the two frameworks were mutually incompatible. The Open Neural Network Exchange ([[Open Neural Network Exchange|ONNX]]) project was created by Meta and [[Microsoft]] in September 2017 for converting models between frameworks. Caffe2 was merged into PyTorch at the end of March 2018.<ref>{{cite web|url=https://medium.com/@Synced/caffe2-merges-with-pytorch-a89c70ad9eb7|title=Caffe2 Merges With PyTorch|date=2018-04-02}}</ref> In September 2022, Meta announced that ''PyTorch'' would be governed by PyTorch Foundation, a newly created independent organization{{snd}}a subsidiary of [[Linux Foundation]].<ref>{{cite web |url=https://arstechnica.com/information-technology/2022/09/meta-spins-off-pytorch-foundation-to-make-ai-framework-vendor-neutral/ |title=Meta spins off PyTorch Foundation to make AI framework vendor neutral |date=2022-09-12 |website=[[Ars Technica]] |last=Edwards |first=Benj}}</ref>
 
Meta (formerly known as Facebook) operates both ''PyTorch'' and ''Convolutional Architecture for Fast Feature Embedding'' ([[Caffe (software)|Caffe2]]), but models defined by the two frameworks were mutually incompatible. The Open Neural Network Exchange ([[Open Neural Network Exchange|ONNX]]) project was created by Meta and [[Microsoft]] in September 2017 for converting models between frameworks. Caffe2 was merged into PyTorch at the end of March 2018.<ref>{{cite web|url=https://medium.com/@Synced/caffe2-merges-with-pytorch-a89c70ad9eb7|title=Caffe2 Merges With PyTorch|date=2 April 2018|access-04date=2 January 2019|archive-02date=30 March 2019|archive-url=https://web.archive.org/web/20190330143500/https://medium.com/@Synced/caffe2-merges-with-pytorch-a89c70ad9eb7|url-status=live}}</ref> In September 2022, Meta announced that ''PyTorch'' would be governed by the independent PyTorch Foundation, a newly created independent organization{{snd}}a subsidiary of the [[Linux Foundation]].<ref>{{cite web |url=https://arstechnica.com/information-technology/2022/09/meta-spins-off-pytorch-foundation-to-make-ai-framework-vendor-neutral/ |title=Meta spins off PyTorch Foundation to make AI framework vendor neutral |date=2022-09-12 September 2022 |website=[[Ars Technica]] |last=Edwards |first=Benj |access-date=13 September 2022 |archive-date=13 September 2022 |archive-url=https://web.archive.org/web/20220913034850/https://arstechnica.com/information-technology/2022/09/meta-spins-off-pytorch-foundation-to-make-ai-framework-vendor-neutral/ |url-status=live }}</ref>
PyTorch 2.0 was released on 15 March 2023.<ref>{{cite news |title=PyTorch 2.0 brings new fire to open-source machine learning |url=https://venturebeat.com/ai/pytorch-2-0-brings-new-fire-to-open-source-machine-learning/ |access-date=16 March 2023 |work=VentureBeat |date=15 March 2023}}</ref>
 
PyTorch 2.0 was released on 15 March 2023, introducing [[TorchDynamo]], a Python-level [[compiler]] that makes code run up to 2x faster, along with significant improvements in training and inference performance across major [[cloud computing|cloud platforms]].<ref>{{cite web|title=Dynamo Overview |url=https://pytorch.org/docs/stable/torch.compiler_dynamo_overview.html }}</ref><ref>{{cite news |title=PyTorch 2.0 brings new fire to open-source machine learning |url=https://venturebeat.com/ai/pytorch-2-0-brings-new-fire-to-open-source-machine-learning/ |access-date=16 March 2023 |work=VentureBeat |date=15 March 2023 |archive-date=16 March 2023 |archive-url=https://web.archive.org/web/20230316004808/https://venturebeat.com/ai/pytorch-2-0-brings-new-fire-to-open-source-machine-learning/ |url-status=live }}</ref>
 
==PyTorch tensors==
{{main|Tensor (machine learning)}}
PyTorch defines a class called Tensor (<code>torch.Tensor</code>) to store and operate on homogeneous multidimensional rectangular arrays of numbers. PyTorch Tensors are similar to [[NumPy]] Arrays, but can also be operated on by a [[CUDA]]-capable [[Nvidia|NVIDIA]] [[Graphics processing unit|GPU]]. PyTorch has also been developing support for other GPU platforms, for example, AMD's [[ROCm]]<ref>{{cite web|url=https://rocm.docs.amd.com/projects/install-on-linux/en/latest/how-to/3rd-party/pytorch-install.html|title=Installing PyTorch for ROCm|date=9 February 2024|website=rocm.docs.amd.com}}</ref> and Apple's [[Metal (API)|Metal Framework.]]<ref>{{Cite web |title=Introducing Accelerated PyTorch Training on Mac |url=https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/ |access-date=4 June 2022-06-04 |website=pytorch.org |language=en |archive-date=29 January 2024 |archive-url=https://web.archive.org/web/20240129141050/https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/ |url-status=live }}</ref>
 
PyTorch supports various sub-types of Tensors.<ref>{{cite web |url=https://www.analyticsvidhya.com/blog/2018/02/pytorch-tutorial/ |title=An Introduction to PyTorch – A Simple yet Powerful Deep Learning Library |website=analyticsvidhya.com |access-date=2018-06-11 June 2018 |date=22 February 2018 |archive-date=22 October 2019 |archive-url=https://web.archive.org/web/20191022200858/https://www.analyticsvidhya.com/blog/2018/02/pytorch-22tutorial/ |url-status=live }}</ref>
 
Note that the term "tensor" here does not carry the same meaning as tensor in mathematics or physics. The meaning of the word in machine learning is only superficially related to its original meaning as a certain kind of object in [[linear algebra]]. Tensors in PyTorch are simply multi-dimensional arrays.
=== Differences from physics "tensors" ===
A [[tensor]] in physics is similar to a PyTorch tensor, in that it's mostly a multidimensional array. The only additional feature of a physicist's tensor that's missing from a PyTorch tensor, is that when indexing its entries, some of the indices are written subscripted or superscripted, like <math>{{t_{ij}^k}_{l}}^{m}</math>. The number of superscripts and subscripts in a physics tensor is called the ''type'' of the tensor. It should be easy to understand that the type of the above tensor <math>t</math> is (2,3). A superscripted index is called ''a [[Covariance and contravariance of vectors|contravariant index, and a subscripted index is called a covariant index]].'' A physicist's tensor supports four fundamental operations:
 
# Addition
# [[Tensor#Tensor product|Tensor product]]
# [[Tensor contraction|Contraction]]
# Basis change
 
Of these, the co/contravariance distinction only affects the basis change operation. The other three operations are unaffected by co/contravariance, and are therefore easy to implement for PyTorch tensors. Thus, whether a PyTorch "tensor" deserves to be called a tensor is a matter of opinion.
 
A mathematician's tensor is also subtly different from a physicist's tensor, but the difference there is so minor that it has no effect on applications. For the sake of completeness, for a mathematician, a tensor of type (m,n) over a vector space <math>V</math> is an element of the vector space <math>\operatorname{Hom}(\underbrace{V \otimes \dotsb \otimes V}_n,
\underbrace{V \otimes \dotsb \otimes V}_m)
</math>. The first three of the four operations above can then be expressed in a basis-independent way, making the fourth one unnecessary. A physicist's tensor then results from picking a basis for <math>V</math>, which turns a mathematician's tensor into a multidimensional array. Since the choice of basis for <math>V</math> was ''arbitrary'', we need a basis change operation to eliminate this arbitrariness, which ends up being our operation 4.
 
== PyTorch neural networks ==
{{main|TensorNeural network (machine learning)}}
 
PyTorch defines a classmodule called nn (<code>torch.nn</code>) to describe neural networks and to support training. This module offers a comprehensive collection of building blocks for neural networks, including various layers and activation functions, enabling the construction of complex models. Networks are built by inheriting from the <code>torch.nn</code> module and defining the sequence of operations in the <code>forward()</code> function.
 
== Example ==
The following program shows the low-level functionality of the library with a simple example.
 
<syntaxhighlight lang="numpy" line="1">
import torch
dtype = torch.float
device = torch.device("cpu") # This# executesExecute all calculations on the CPU
# device = torch.device("cuda:0") # This# executesExecutes all calculations on the GPU
 
# Creation ofCreate a tensor and fillingfill of a tensorit with random numbers
a = torch.randn(2, 3, device=device, dtype=dtype)
print(a) # Output of tensor A
# Output: tensor([[-1.1884, 0.8498, -1.7129],
# [-0.8816, 0.1944, 0.5847]])
 
# Creation of a tensor and filling of a tensor with random numbers
b = torch.randn(2, 3, device=device, dtype=dtype)
print(b) # Output of tensor B
# Output: tensor([[ 0.7178, -0.8453, -1.3403],
# [ 1.3262, 1.1512, -1.7070]])
 
print(a * b)
print(a*b) # Output of a multiplication of the two tensors
# Output: tensor([[-0.8530, -0.7183, 2.58],
# [-1.1692, 0.2238, -0.9981]])
 
print(a.sum()) # Output of the sum of all elements in tensor A
# Output: tensor(-2.1540)
 
print(a[1, 2]) # Output of the element in the third column of the second row (zero -based)
# Output: tensor(0.5847)
 
print(a.max()) # Output of the maximum value in tensor A
# Output: tensor(0.8498)
</syntaxhighlight>
</syntaxhighlight>The following code-block shows an example of the higher level functionality provided <code>nn</code> module. A neural network with linear layers is defined in the example.<syntaxhighlight lang="python3" line="1">
 
import torch
The following code-block defines a neural network with linear layers using the <code>nn</code> module.
<syntaxhighlight lang="python3" line="1">
from torch import nn # Import the nn sub-module from PyTorch
 
class NeuralNetwork(nn.Module): # Neural networks are defined as classes
def __init__(self): # Layers and variables are defined in the __init__ method
super().__init__() # Must be in every network.
self.flatten = nn.Flatten() # DefiningConstruct a flattening layer.
self.linear_relu_stack = nn.Sequential( # DefiningConstruct a stack of layers.
nn.Linear(28 * 28, 512), # Linear Layers have an input and output shape
nn.ReLU(), # ReLU is one of many activation functions provided by nn
nn.Linear(512, 512),
nn.ReLU(),
Line 109 ⟶ 99:
)
 
def forward(self, x): # This function defines the forward pass.
x = self.flatten(x)
logits = self.linear_relu_stack(x)