mirror of
https://github.com/jaywcjlove/reference.git
synced 2025-06-18 21:21:21 +08:00
doc: update docs/pytorch.md (#554) 5fc5c065df
This commit is contained in:
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 8.4 MiB After Width: | Height: | Size: 8.4 MiB |
@ -35,7 +35,7 @@
|
||||
备忘清单为您提供了 <a href="https://pytorch.org/">Pytorch</a> 基本语法和初步应用参考</p>
|
||||
</div></header><div class="menu-tocs"><div class="menu-btn"><svg aria-hidden="true" fill="currentColor" height="1em" width="1em" viewBox="0 0 16 16" version="1.1" data-view-component="true">
|
||||
<path fill-rule="evenodd" d="M2 4a1 1 0 100-2 1 1 0 000 2zm3.75-1.5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zm0 5a.75.75 0 000 1.5h8.5a.75.75 0 000-1.5h-8.5zM3 8a1 1 0 11-2 0 1 1 0 012 0zm-1 6a1 1 0 100-2 1 1 0 000 2z"></path>
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#认识-pytorch">认识 Pytorch</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建一个全零矩阵">创建一个全零矩阵</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数据创建张量">数据创建张量</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#pytorch-的基本语法">Pytorch 的基本语法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作1">加法操作(1)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作2">加法操作(2)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作3">加法操作(3)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作4">加法操作(4)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#张量操作">张量操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#张量形状">张量形状</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#取张量元素">取张量元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torch-tensor-和-numpy-array互换">Torch Tensor 和 Numpy array互换</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torch-tensor-转换为-numpy-array">Torch Tensor 转换为 Numpy array</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numpy-array转换为torch-tensor">Numpy array转换为Torch Tensor</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#导入-imports">导入 Imports</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#一般">一般</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#神经网络-api">神经网络 API</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torchscript-和-jit">Torchscript 和 JIT</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onnx">ONNX</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#vision">Vision</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#分布式训练">分布式训练</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</svg></div><div class="menu-modal"><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#入门">入门</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#介绍">介绍</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#认识-pytorch">认识 Pytorch</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#创建一个全零矩阵">创建一个全零矩阵</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#数据创建张量">数据创建张量</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#pytorch-的基本语法">Pytorch 的基本语法</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作1">加法操作(1)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作2">加法操作(2)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作3">加法操作(3)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#加法操作4">加法操作(4)</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#张量操作">张量操作</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#张量形状">张量形状</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#取张量元素">取张量元素</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torch-tensor-和-numpy-array互换">Torch Tensor 和 Numpy array互换</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torch-tensor-转换为-numpy-array">Torch Tensor 转换为 Numpy array</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#numpy-array转换为torch-tensor">Numpy array转换为Torch Tensor</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#squeeze函数">squeeze函数</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#unsqueeze函数">unsqueeze函数</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#导入-imports">导入 Imports</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#一般">一般</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#神经网络-api">神经网络 API</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#torchscript-和-jit">Torchscript 和 JIT</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#onnx">ONNX</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#vision">Vision</a><a aria-hidden="true" class="leve3 tocs-link" data-num="3" href="#分布式训练">分布式训练</a><a aria-hidden="true" class="leve2 tocs-link" data-num="2" href="#另见">另见</a></div></div><div class="h1wrap-body"><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="入门"><a aria-hidden="true" tabindex="-1" href="#入门"><span class="icon icon-link"></span></a>入门</h2><div class="wrap-body">
|
||||
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="介绍"><a aria-hidden="true" tabindex="-1" href="#介绍"><span class="icon icon-link"></span></a>介绍</h3><div class="wrap-body">
|
||||
<ul>
|
||||
<li><a href="https://pytorch.org/">Pytorch 官网</a> <em>(pytorch.org)</em></li>
|
||||
@ -157,6 +157,24 @@
|
||||
</span></code></pre>
|
||||
<!--rehype:className=wrap-text-->
|
||||
<p>注意: 所有在CPU上的Tensors, 除了CharTensor, 都可以转换为Numpy array并可以反向转换.</p>
|
||||
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="squeeze函数"><a aria-hidden="true" tabindex="-1" href="#squeeze函数"><span class="icon icon-link"></span></a>squeeze函数</h3><div class="wrap-body">
|
||||
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x <span class="token operator">=</span> torch<span class="token punctuation">.</span>rand<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># squeeze不加参数,默认去除所有为1的维度</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span>dim<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># squeeze加参数,去除指定为1的维度</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># squeeze加参数,如果不为1,则不变</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> torch<span class="token punctuation">.</span>squeeze<span class="token punctuation">(</span>x<span class="token punctuation">,</span><span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># 既可以是函数,也可以是方法</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
||||
</span></code></pre>
|
||||
</div></div></div><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="unsqueeze函数"><a aria-hidden="true" tabindex="-1" href="#unsqueeze函数"><span class="icon icon-link"></span></a>unsqueeze函数</h3><div class="wrap-body">
|
||||
<pre class="language-python"><code class="language-python code-highlight"><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x <span class="token operator">=</span> torch<span class="token punctuation">.</span>rand<span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">)</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> x<span class="token punctuation">.</span>unsqueeze<span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># unsqueeze必须加参数, _ 2 _ 28 _</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token comment"># 参数代表在哪里添加维度 0 1 2</span>
|
||||
</span><span class="code-line"><span class="token operator">>></span><span class="token operator">></span> torch<span class="token punctuation">.</span>unsqueeze<span class="token punctuation">(</span>x<span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">.</span>shape <span class="token comment"># 既可以是函数,也可以是方法</span>
|
||||
</span><span class="code-line">torch<span class="token punctuation">.</span>Size<span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">28</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span>
|
||||
</span></code></pre>
|
||||
</div></div></div></div></div><div class="wrap h2body-exist"><div class="wrap-header h2wrap"><h2 id="导入-imports"><a aria-hidden="true" tabindex="-1" href="#导入-imports"><span class="icon icon-link"></span></a>导入 Imports</h2><div class="wrap-body">
|
||||
</div></div><div class="h2wrap-body"><div class="wrap h3body-not-exist"><div class="wrap-header h3wrap"><h3 id="一般"><a aria-hidden="true" tabindex="-1" href="#一般"><span class="icon icon-link"></span></a>一般</h3><div class="wrap-body">
|
||||
<pre class="wrap-text"><code class="language-python code-highlight"><span class="code-line"><span class="token comment"># 根包</span>
|
||||
|
19
index.html
19
index.html
@ -959,8 +959,8 @@
|
||||
<a href="https://github.com/kubeme" title="kubernetes for me">
|
||||
<img src="https://avatars.githubusercontent.com/u/16346220?v=4" width="42;" alt="kubernetes for me">
|
||||
</a>
|
||||
<a href="https://github.com/larry-xue" title="larry">
|
||||
<img src="https://avatars.githubusercontent.com/u/48818060?v=4" width="42;" alt="larry">
|
||||
<a href="https://github.com/Zeng-qh" title="都一样">
|
||||
<img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样">
|
||||
</a>
|
||||
<a href="https://github.com/hua03" title="hua03">
|
||||
<img src="https://avatars.githubusercontent.com/u/19561959?v=4" width="42;" alt="hua03">
|
||||
@ -995,8 +995,8 @@
|
||||
<a href="https://github.com/zlfyuan" title="bgbgPang">
|
||||
<img src="https://avatars.githubusercontent.com/u/19658018?v=4" width="42;" alt="bgbgPang">
|
||||
</a>
|
||||
<a href="https://github.com/Zeng-qh" title="都一样">
|
||||
<img src="https://avatars.githubusercontent.com/u/40046415?v=4" width="42;" alt="都一样">
|
||||
<a href="https://github.com/Lmmmmmm-bb" title="_lmmmmmm">
|
||||
<img src="https://avatars.githubusercontent.com/u/54026110?v=4" width="42;" alt="_lmmmmmm">
|
||||
</a>
|
||||
<a href="https://github.com/binscor" title="Zheng Nai Bin">
|
||||
<img src="https://avatars.githubusercontent.com/u/37325821?v=4" width="42;" alt="Zheng Nai Bin">
|
||||
@ -1061,6 +1061,9 @@
|
||||
<a href="https://github.com/liliangrong777" title="liliangrong777">
|
||||
<img src="https://avatars.githubusercontent.com/u/58727146?v=4" width="42;" alt="liliangrong777">
|
||||
</a>
|
||||
<a href="https://github.com/larry-xue" title="larry">
|
||||
<img src="https://avatars.githubusercontent.com/u/48818060?v=4" width="42;" alt="larry">
|
||||
</a>
|
||||
<a href="https://github.com/Moeyuuko" title="Moeyuuko">
|
||||
<img src="https://avatars.githubusercontent.com/u/14266681?v=4" width="42;" alt="Moeyuuko">
|
||||
</a>
|
||||
@ -1127,9 +1130,6 @@
|
||||
<a href="https://github.com/13812700839" title="花殇">
|
||||
<img src="https://avatars.githubusercontent.com/u/58072506?v=4" width="42;" alt="花殇">
|
||||
</a>
|
||||
<a href="https://github.com/Lmmmmmm-bb" title="_lmmmmmm">
|
||||
<img src="https://avatars.githubusercontent.com/u/54026110?v=4" width="42;" alt="_lmmmmmm">
|
||||
</a>
|
||||
<a href="https://github.com/y52y" title="Zyj">
|
||||
<img src="https://avatars.githubusercontent.com/u/51304324?v=4" width="42;" alt="Zyj">
|
||||
</a>
|
||||
@ -1160,6 +1160,9 @@
|
||||
<a href="https://github.com/YLee9527" title="Terry Young">
|
||||
<img src="https://avatars.githubusercontent.com/u/18697332?v=4" width="42;" alt="Terry Young">
|
||||
</a>
|
||||
<a href="https://github.com/TRDSCSH" title="TRDSCSH">
|
||||
<img src="https://avatars.githubusercontent.com/u/125717891?v=4" width="42;" alt="TRDSCSH">
|
||||
</a>
|
||||
<a href="https://github.com/Xiwin" title="Xiwin">
|
||||
<img src="https://avatars.githubusercontent.com/u/107191230?v=4" width="42;" alt="Xiwin">
|
||||
</a>
|
||||
@ -1501,7 +1504,7 @@
|
||||
<!--rehype:class=home-card home-links-->
|
||||
<p>如果你有资源,可以很方便<a href="https://github.com/jaywcjlove/reference/issues/102#issue-1451649637">部署 web 版</a>,这非常简单,只需要克隆 <a href="https://github.com/jaywcjlove/reference/tree/gh-pages">gh-pages</a> 分支代码到你的静态服务就可以了,还可以使用 <a href="https://hub.docker.com/r/wcjiang/reference">docker</a> 快捷部署 web 版。</p>
|
||||
|
||||
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang. Updated on 2024/03/05 16:37:05</footer></footer><script src="data.js?v=1.5.2" defer></script><script src="js/fuse.min.js?v=1.5.2" defer></script><script src="js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
|
||||
</div></div><div class="h2wrap-body"></div></div></div></div><footer class="footer-wrap"><footer class="max-container">© 2022 Kenny Wang. Updated on 2024/03/05 21:24:58</footer></footer><script src="data.js?v=1.5.2" defer></script><script src="js/fuse.min.js?v=1.5.2" defer></script><script src="js/main.js?v=1.5.2" defer></script><div id="mysearch"><div class="mysearch-box"><div class="mysearch-input"><div><svg xmlns="http://www.w3.org/2000/svg" height="1em" width="1em" viewBox="0 0 18 18">
|
||||
<path fill="currentColor" d="M17.71,16.29 L14.31,12.9 C15.4069846,11.5024547 16.0022094,9.77665502 16,8 C16,3.581722 12.418278,0 8,0 C3.581722,0 0,3.581722 0,8 C0,12.418278 3.581722,16 8,16 C9.77665502,16.0022094 11.5024547,15.4069846 12.9,14.31 L16.29,17.71 C16.4777666,17.8993127 16.7333625,18.0057983 17,18.0057983 C17.2666375,18.0057983 17.5222334,17.8993127 17.71,17.71 C17.8993127,17.5222334 18.0057983,17.2666375 18.0057983,17 C18.0057983,16.7333625 17.8993127,16.4777666 17.71,16.29 Z M2,8 C2,4.6862915 4.6862915,2 8,2 C11.3137085,2 14,4.6862915 14,8 C14,11.3137085 11.3137085,14 8,14 C4.6862915,14 2,11.3137085 2,8 Z"></path>
|
||||
</svg><input id="mysearch-input" type="search" placeholder="搜索" autocomplete="off"><div class="mysearch-clear"></div></div><button id="mysearch-close" type="button">搜索</button></div><div class="mysearch-result"><div id="mysearch-menu"></div><div id="mysearch-content"></div></div></div></div></body>
|
||||
</html>
|
||||
|
Reference in New Issue
Block a user