<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh">
	<id>https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=PyTorch</id>
	<title>PyTorch - 版本历史</title>
	<link rel="self" type="application/atom+xml" href="https://arolstar52-zhtest.hf.space/index.php?action=history&amp;feed=atom&amp;title=PyTorch"/>
	<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=PyTorch&amp;action=history"/>
	<updated>2026-06-25T00:10:18Z</updated>
	<subtitle>在这个wiki上该页的修订历史</subtitle>
	<generator>MediaWiki 1.43.8</generator>
	<entry>
		<id>https://arolstar52-zhtest.hf.space/index.php?title=PyTorch&amp;diff=3123219&amp;oldid=prev</id>
		<title>imported&gt;Cewbot：​清理跨語言連結Meta AI成為內部連結:編輯摘要的紅色連結經繁簡轉換後存在，非bot錯誤編輯 (本次機械人作業已完成39.1%)</title>
		<link rel="alternate" type="text/html" href="https://arolstar52-zhtest.hf.space/index.php?title=PyTorch&amp;diff=3123219&amp;oldid=prev"/>
		<updated>2025-12-29T01:19:51Z</updated>

		<summary type="html">&lt;p&gt;清理跨語言連結&lt;a href=&quot;/wiki/Meta_AI&quot; title=&quot;Meta AI&quot;&gt;Meta AI&lt;/a&gt;成為內部連結:編輯摘要的紅色連結經繁簡轉換後存在，非bot錯誤編輯 (本次機械人作業已完成39.1%)&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{NoteTA|G1=IT|G2=FL}}&lt;br /&gt;
{{Infobox software&lt;br /&gt;
| name = PyTorch&lt;br /&gt;
| logo = PyTorch logo black.svg&lt;br /&gt;
| screenshot = &lt;br /&gt;
| caption = &lt;br /&gt;
| collapsible = &lt;br /&gt;
| author = Adam Paszke, Sam Gross, Soumith Chintala, Gregory Chanan&lt;br /&gt;
| developer = [[Meta AI]]&lt;br /&gt;
| released = {{Start date and age|2016|October|df=yes}}&lt;br /&gt;
| latest release version = {{LSR/wikidata}}&lt;br /&gt;
| latest release date = &lt;br /&gt;
| programming language = [[Python]], [[C++]], [[CUDA]]&lt;br /&gt;
| operating system = [[Linux]], [[MacOS|macOS]], [[Microsoft Windows|Windows]]&lt;br /&gt;
| platform = [[IA-32]], [[x86-64]], [[ARM64]]&lt;br /&gt;
| repo = {{url|https://github.com/pytorch/pytorch}}&lt;br /&gt;
| genre = [[机器学习]]和[[深度学习]][[库]]&lt;br /&gt;
| license = [[BSD许可证]]&lt;br /&gt;
| website = {{url|https://pytorch.org/}}&lt;br /&gt;
}}&lt;br /&gt;
{{机器学习导航栏}}&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;PyTorch&amp;#039;&amp;#039;&amp;#039;是一个[[开源]]的[[Python|Python]][[机器学习]][[库]]，基于{{tsl|en|Torch (machine_learning)|Torch (机器学习)|Torch}}[[库]]&amp;lt;ref name=&amp;quot;:1&amp;quot;&amp;gt;{{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|accessdate=11 December 2017|archive-date=2018-07-12|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|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;:2&amp;quot;&amp;gt;{{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&amp;#039;Reilly Media|accessdate=11 December 2017|archive-date=2019-05-17|archive-url=https://web.archive.org/web/20190517055218/https://www.oreilly.com/ideas/why-ai-and-machine-learning-researchers-are-beginning-to-embrace-pytorch|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite book|url=https://link.springer.com/chapter/10.1007/978-1-4842-2766-4_12|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|access-date=2018-10-02|archive-date=2018-07-12|archive-url=https://web.archive.org/web/20180712053817/https://link.springer.com/chapter/10.1007/978-1-4842-2766-4_12|dead-url=no}}&amp;lt;/ref&amp;gt;，底层由C++实现，应用于[[人工智能]]领域，如[[计算机视觉]]和[[自然语言处理]]&amp;lt;ref&amp;gt;{{Cite web|url=http://dl4nlp.info/en/latest/|title=Natural Language Processing (NLP) with PyTorch — NLP with PyTorch  documentation|website=dl4nlp.info|language=en|access-date=2017-12-18|archive-date=2019-06-21|archive-url=https://web.archive.org/web/20190621235455/http://dl4nlp.info/en/latest/|dead-url=yes}}&amp;lt;/ref&amp;gt;。它最初由[[Meta Platforms]]的人工智能研究团队开发，現在屬於[[Linux基金会]]的一部分&amp;lt;ref&amp;gt;{{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=2017-12-07|work=O&amp;#039;Reilly Media|access-date=2017-12-18|language=en|archive-date=2019-03-30|archive-url=https://web.archive.org/web/20190330131436/https://www.oreilly.com/ideas/when-two-trends-fuse-pytorch-and-recommender-systems|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{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|date=|work=[[TechCrunch]]|access-date=2017-12-18|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/|archive-date=2020-07-06|dead-url=no|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.}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{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=2017-11-29|website=[[VentureBeat]]|language=en-US|archive-url=https://web.archive.org/web/20190330131432/https://venturebeat.com/2017/11/29/tech-giants-are-using-open-source-frameworks-to-dominate-the-ai-community/|archive-date=2019-03-30|dead-url=no|access-date=2017-12-18}}&amp;lt;/ref&amp;gt;。它是在修改後的[[BSD許可證]]下發布的[[自由及开放源代码软件]]。 儘管[[Python]]接口更加完善並且是開發的主要重點，但 PyTorch 也有[[C++]]接口&amp;lt;ref&amp;gt;{{Cite web|url=https://pytorch.org/cppdocs/frontend.html|title=The C++ Frontend|website=PyTorch Master Documentation|access-date=2019-07-29|archive-date=2020-05-08|archive-url=https://web.archive.org/web/20200508115859/https://pytorch.org/cppdocs/frontend.html|dead-url=no}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
許多[[深度學習]]軟體都是基於 PyTorch 構建的，包括[[特斯拉自动驾驶]]&amp;lt;ref&amp;gt;{{Cite web|last=Karpathy|first=Andrej|title=PyTorch at Tesla - Andrej Karpathy, Tesla|url=https://www.youtube.com/watch?v=oBklltKXtDE|access-date=2023-12-24|archive-date=2023-03-24|archive-url=https://web.archive.org/web/20230324144838/https://www.youtube.com/watch?v=oBklltKXtDE|dead-url=no}}&amp;lt;/ref&amp;gt;、[[優步|Uber]]的Pyro&amp;lt;ref&amp;gt;{{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|archive-date=2017-12-25|archive-url=https://web.archive.org/web/20171225034106/https://eng.uber.com/pyro/|dead-url=no}}&amp;lt;/ref&amp;gt;、[[Hugging Face]]的Transformers&amp;lt;ref&amp;gt;{{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|archive-date=2023-06-11|archive-url=https://web.archive.org/web/20230611061047/https://pytorch.org/hub/huggingface_pytorch-transformers/|dead-url=no}}&amp;lt;/ref&amp;gt;、 PyTorch Lightning&amp;lt;ref&amp;gt;{{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|archive-date=2022-06-08|archive-url=https://web.archive.org/web/20220608042634/https://github.com/PyTorchLightning/pytorch-lightning|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|url=https://pytorch.org/ecosystem/|title=Ecosystem Tools|website=pytorch.org|language=en|access-date=2020-06-18|archive-date=2023-07-18|archive-url=https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/|dead-url=no}}&amp;lt;/ref&amp;gt;、和Catalyst&amp;lt;ref&amp;gt;{{Citation|title=GitHub - catalyst-team/catalyst: Accelerated DL &amp;amp; RL|date=2019-12-05|url=https://github.com/catalyst-team/catalyst|publisher=Catalyst-Team|access-date=2019-12-05|archive-date=2019-12-22|archive-url=https://web.archive.org/web/20191222162045/https://github.com/catalyst-team/catalyst|dead-url=no}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Cite web|url=https://pytorch.org/ecosystem/|title=Ecosystem Tools|website=pytorch.org|language=en|access-date=2020-04-04|archive-date=2023-07-18|archive-url=https://web.archive.org/web/20230718105354/https://pytorch.org/ecosystem/|dead-url=no}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
==概述==&lt;br /&gt;
PyTorch主要有两大特征：&amp;lt;ref&amp;gt;{{cite web |url=https://pytorch.org/about/ |title=PyTorch – About |website=pytorch.org |accessdate=2018-06-11 |archive-url=https://web.archive.org/web/20180615190804/https://pytorch.org/about/ |archive-date=2018-06-15 |dead-url=yes }}&amp;lt;/ref&amp;gt;&lt;br /&gt;
* 类似于[[NumPy]]的[[张量]]计算，能在 [[圖形處理器|GPU]] 或 MPS 等硬件加速器上加速；&lt;br /&gt;
* 基于带[[自动微分]]系统&amp;lt;ref&amp;gt;{{cite journal&lt;br /&gt;
|author=R.E. Wengert&lt;br /&gt;
|title=A simple automatic derivative evaluation program&lt;br /&gt;
|journal=Comm. ACM&lt;br /&gt;
|volume=7&lt;br /&gt;
|year=1964&lt;br /&gt;
|pages=463–464&lt;br /&gt;
|doi=10.1145/355586.364791&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite journal&lt;br /&gt;
|last=Bartholomew-Biggs&lt;br /&gt;
|first=Michael&lt;br /&gt;
|last2=Brown&lt;br /&gt;
|first2=Steven&lt;br /&gt;
|last3=Christianson&lt;br /&gt;
|first3=Bruce&lt;br /&gt;
|last4=Dixon&lt;br /&gt;
|first4=Laurence&lt;br /&gt;
|date=2000&lt;br /&gt;
|title=Automatic differentiation of algorithms&lt;br /&gt;
|url=http://ac.els-cdn.com/S0377042700004222/1-s2.0-S0377042700004222-main.pdf?_tid=61070b3c-31f1-11e5-a550-00000aacb35f&amp;amp;acdnat=1437735029_c639352923bb07e65307eb75c420d42f&lt;br /&gt;
|journal=Journal of Computational and Applied Mathematics&lt;br /&gt;
|volume=124&lt;br /&gt;
|issue=1-2&lt;br /&gt;
|pages=171–190&lt;br /&gt;
|doi=10.1016/S0377-0427(00)00422-2&lt;br /&gt;
|bibcode=2000JCoAM.124..171B&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;的深度[[神经网络]]&amp;lt;ref name=&amp;quot;:0&amp;quot;&amp;gt;{{Cite book|title=神经网络与PyTorch实战|last=|first=|publisher=机械工业出版社|year=2018|isbn=9787111605775|location=|pages=|script-title=Application of Neural Network and PyTorch}}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
PyTorch包括torch.autograd、torch.nn、torch.optim等子模块&amp;lt;ref name=&amp;quot;:0&amp;quot; /&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
PyTorch包含多种[[损失函数]]，包括 MSE（均方误差 = L2 范数）、交叉熵损失和负熵似然损失（对[[分类器]]有用）等。&lt;br /&gt;
&lt;br /&gt;
== PyTorch張量 ==&lt;br /&gt;
PyTorch定義了一個名為張量(torch.Tensor) 的類別來儲存和操作同構多維矩形數字陣列。 PyTorch張量與NumPy陣列類似，但也可以在支援 [[CUDA]] 的 [[英伟达]] [[圖形處理器|GPU]] 上運作。 PyTorch 也一直在開發對其他 [[圖形處理器|GPU]] 平台的支持，例如 [[AMD]] 的 ROCm 和 [[Apple]] 的[[Metal (API)|Metal Framework]]&amp;lt;ref&amp;gt;{{Cite web |title=Introducing Accelerated PyTorch Training on Mac |url=https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/ |access-date=2022-06-04 |website=pytorch.org |language=en |archive-date=2024-01-29 |archive-url=https://web.archive.org/web/20240129141050/https://pytorch.org/blog/introducing-accelerated-pytorch-training-on-mac/ |dead-url=no }}&amp;lt;/ref&amp;gt;。&lt;br /&gt;
&lt;br /&gt;
张量是 PyTorch 中的核心数据抽象，PyTorch 支援各種張量子類型&amp;lt;ref&amp;gt;{{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 |date=2018-02-22 |archive-date=2019-10-22 |archive-url=https://web.archive.org/web/20191022200858/https://www.analyticsvidhya.com/blog/2018/02/pytorch-tutorial/ |dead-url=no }}&amp;lt;/ref&amp;gt;。通常地，一维张量称为&amp;#039;&amp;#039;向量（vector）&amp;#039;&amp;#039;，二维张量称为&amp;#039;&amp;#039;矩阵（matrix）。&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
张量的数据类型包括：&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.bool&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.int8&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.uint8&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.int16&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.int32&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.int64&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.half&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.float&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.double&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;torch.bfloat&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PyTorch神經網絡 ==&lt;br /&gt;
神经网络由对数据执行操作的层/模块组成。 torch.nn 命名空间提供了使用者需要的所有构建块来构建自己的神经网络。PyTorch 中的每个模块都对应nn.模块。 神经网络本身是由其他模块（层）组成的模块。这种嵌套结构允许使用者轻松构建并管理复杂的架构。神经网络中的许多层都是&amp;#039;&amp;#039;参数化的&amp;#039;&amp;#039;，即具有相关的权重以及在训练期间优化的偏差。自动子类化跟踪模型对象中定义的所有字段，并生成所有参数可使用模型或方法访问。&amp;lt;ref name=&amp;quot;:1&amp;quot; /&amp;gt;&amp;lt;syntaxhighlight lang=&amp;quot;pycon&amp;quot;&amp;gt;&lt;br /&gt;
import torch                     # for all things PyTorch&lt;br /&gt;
import torch.nn as nn            # for torch.nn.Module, the parent object for PyTorch models&lt;br /&gt;
import torch.nn.functional as F  # for the activation function&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;激活函数&amp;lt;code&amp;gt;torch.nn.Module&amp;lt;/code&amp;gt;具有封装所有主要内容的对象激活功能，包括 ReLU 及其许多变体、Tanh、 Hardtanh、sigmoid 等。&amp;lt;ref name=&amp;quot;:2&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== PyTorch模型常见图层类型 ==&lt;br /&gt;
&lt;br /&gt;
=== 线性层 ===&lt;br /&gt;
最基本的神经网络层类型是&amp;#039;&amp;#039;线性&amp;#039;&amp;#039;或&amp;#039;&amp;#039;完全连接&amp;#039;&amp;#039;层。在这个层中，每个输入都会影响每个图层的输出到由图层权重指定的程度。如果 模型有 m 个输入和 n 个输出，权重将是一个 &amp;#039;&amp;#039;m&amp;#039;&amp;#039; x &amp;#039;&amp;#039;n&amp;#039;&amp;#039; 矩阵。&lt;br /&gt;
&lt;br /&gt;
=== 卷积层 ===&lt;br /&gt;
卷积层旨在处理高度空间相关性。它们在[[计算机视觉]]中非常常用， 它们检测组成的特征的紧密分组更高级别的功能。它们也会在其他上下文中弹出。例如， 在 NLP 应用程序中，单词的直接上下文（即序列中附近的其他单词）可以影响语句。&lt;br /&gt;
&lt;br /&gt;
=== 循环层 ===&lt;br /&gt;
[[循环神经网络|递归神经网络]]（RNN）是用于顺序数据（从科学仪器到时间序列测量）的自然语言句子。&lt;br /&gt;
&lt;br /&gt;
== 例子 ==&lt;br /&gt;
下面的程序用简单的例子展示这个程序库的低层功能。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;pycon&amp;quot;&amp;gt;&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; import torch&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; dtype = torch.float&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; device = torch.device(&amp;quot;cpu&amp;quot;) # 本次在CPU上执行所有的计算&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; # device = torch.device(&amp;quot;cuda:0&amp;quot;) # 本次在GPU上执行所有的计算&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; # 建立一个张量并用随机数填充这个张量&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; a = torch.randn(2, 3, device=device, dtype=dtype)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(a) # 输出张量a&lt;br /&gt;
tensor([[-0.1460, -0.3490,  0.3705],&lt;br /&gt;
        [-1.1141,  0.7661,  1.0823]])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; # 建立一个张量并用随机数填充这个张量&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; b = torch.randn(2, 3, device=device, dtype=dtype)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(b) # 输出张量B&lt;br /&gt;
tensor([[ 0.6901, -0.9663,  0.3634],&lt;br /&gt;
        [-0.6538, -0.3728, -1.1323]])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(a*b) # 输出两个张量的乘积&lt;br /&gt;
tensor([[-0.1007,  0.3372,  0.1346],&lt;br /&gt;
        [ 0.7284, -0.2856, -1.2256]])&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(a.sum()) # 输出在张量a中所有元素的总和&lt;br /&gt;
tensor(0.6097)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(a[1,2]) # 输出第2行第3列（0起始）的元素&lt;br /&gt;
tensor(1.0823)&lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; &lt;br /&gt;
&amp;gt;&amp;gt;&amp;gt; print(a.max()) # 输出在张量a中的极大值&lt;br /&gt;
tensor(1.0823)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
下列代码块展示了&amp;lt;code&amp;gt;nn&amp;lt;/code&amp;gt;模块提供的高层功能的例子。例子中定义了具有线性层的神经网络。&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import torch&lt;br /&gt;
from torch import nn # 从PyTorch中导入nn子模块 &lt;br /&gt;
&lt;br /&gt;
class NeuralNetwork(nn.Module): # 神经网络被定义为类&lt;br /&gt;
    def __init__(self): # 在__init__方法中定义诸层和变量&lt;br /&gt;
        super(NeuralNetwork, self).__init__() # 必须出现在所有网络中&lt;br /&gt;
        self.flatten = nn.Flatten() # 定义一个压平层&lt;br /&gt;
        self.linear_relu_stack = nn.Sequential( # 定义诸层的一个堆栈&lt;br /&gt;
            nn.Linear(28*28, 512), # 线性层有一个输入和输出形状&lt;br /&gt;
            nn.ReLU(), # ReLU是nn提供的诸多激活函数之一&lt;br /&gt;
            nn.Linear(512, 512),&lt;br /&gt;
            nn.ReLU(),&lt;br /&gt;
            nn.Linear(512, 10), &lt;br /&gt;
        )&lt;br /&gt;
&lt;br /&gt;
    def forward(self, x): # 这个函数定义前向传递。&lt;br /&gt;
        x = self.flatten(x)&lt;br /&gt;
        logits = self.linear_relu_stack(x)&lt;br /&gt;
        return logits&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==参考文献==&lt;br /&gt;
{{reflist|2}}&lt;br /&gt;
&lt;br /&gt;
==参见==&lt;br /&gt;
* {{le|深度学习软件比较|Comparison of deep learning software}}&lt;br /&gt;
* [[人工神经网络]]&lt;br /&gt;
* [[深度学习]]&lt;br /&gt;
* [[机器学习]]&lt;br /&gt;
* [[损失函数]]&lt;br /&gt;
* [[激活函数]]&lt;br /&gt;
&lt;br /&gt;
==外部链接==&lt;br /&gt;
* {{Official website|pytorch.org}}&lt;br /&gt;
* 从 GitHub 访问 [https://github.com/pytorch/tutorials PyTorch 教程] {{Wayback|url=https://github.com/pytorch/tutorials |date=20240324113317 }}&lt;br /&gt;
* [https://www.youtube.com/@PyTorch PyTorch基金会的Youtube账号] {{Wayback|url=https://www.youtube.com/@PyTorch |date=20240408220445 }}&lt;br /&gt;
&lt;br /&gt;
{{深度学习软件}}&lt;br /&gt;
{{Differentiable computing}}&lt;br /&gt;
&lt;br /&gt;
[[Category:数据挖掘和机器学习软件]]&lt;br /&gt;
[[Category:用Python編程的自由軟體]]&lt;br /&gt;
[[Category:Python科学库]]&lt;br /&gt;
[[Category:开源人工智能]]&lt;/div&gt;</summary>
		<author><name>imported&gt;Cewbot</name></author>
	</entry>
</feed>