Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenDocCN
pytorch-doc-zh
提交
77b716c2
P
pytorch-doc-zh
项目概览
OpenDocCN
/
pytorch-doc-zh
通知
120
Star
3932
Fork
992
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
pytorch-doc-zh
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
77b716c2
编写于
2月 02, 2021
作者:
片刻小哥哥
提交者:
GitHub
2月 02, 2021
浏览文件
操作
浏览文件
下载
差异文件
小哥哥 润得很棒 | Merge pull request #542 from kevin198528/master
重新润色张量章节
上级
fcebfaa2
4ea9e6b6
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
104 addition
and
141 deletion
+104
-141
docs/1.7/03.md
docs/1.7/03.md
+104
-141
未找到文件。
docs/1.7/03.md
浏览文件 @
77b716c2
# 张量
>
原文:
<https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py>
>
原文:
<https://pytorch.org/tutorials/beginner/blitz/tensor_tutorial.html#sphx-glr-beginner-blitz-tensor-tutorial-py>
张量
是一种特殊的数据结构,与数组和矩阵非常相似。 在 PyTorch 中,我们使用张量对模型的输入和输出以及模型的参数进行编码
。
张量
如同数组和矩阵一样, 是一种特殊的数据结构。在
`PyTorch`
中, 神经网络的输入、输出以及网络的参数等数据, 都是使用张量来进行描述
。
张量
与 NumPy 的
`ndarray`
相似,除了张量可以在 GPU 或其他专用硬件上运行以加速计算。 如果您熟悉
`ndarray`
,就可以使用张量 API。 如果没有,请遵循此快速 API 演练
。
张量
的使用和
`Numpy`
中的
`ndarrays`
很类似, 区别在于张量可以在
`GPU`
或其它专用硬件上运行, 这样可以得到更快的加速效果。如果你对
`ndarrays`
很熟悉的话, 张量的使用对你来说就很容易了。如果不太熟悉的话, 希望这篇有关张量
`API`
的快速入门教程能够帮到你
。
```
py
```
py
thon
import
torch
import
numpy
as
np
```
## 张量初始化
张量
可以通过多种方式初始化。 看下面
的例子:
张量
有很多种不同的初始化方法, 先来看看四个简单
的例子:
**
直接来自数据
**
**
1. 直接生成张量
**
张量可以直接从数据创建。 数据类型是自动推断的
。
由原始数据直接生成张量, 张量类型由原始数据类型决定
。
```
py
data
=
[[
1
,
2
],[
3
,
4
]]
```
py
thon
data
=
[[
1
,
2
],
[
3
,
4
]]
x_data
=
torch
.
tensor
(
data
)
```
**
来自 NumPy 数组
**
**
2. 通过Numpy数组来生成张量
**
可以从 NumPy 数组创建张量(反之亦然-参见
[
与 NumPy 桥接
](
#bridge-to-np-label
)
)
。
由已有的
`Numpy`
数组来生成张量(反过来也可以由张量来生成
`Numpy`
数组, 参考
[
张量与Numpy之间的转换
](
#jump
)
)
。
```
py
```
py
thon
np_array
=
np
.
array
(
data
)
x_np
=
torch
.
from_numpy
(
np_array
)
```
**3. 通过已有的张量来生成新的张量**
**来自另一个张量**
:
新的张量将继承已有张量的数据属性(结构、类型), 也可以重新指定新的数据类型。
除非明确覆盖,否则新张量将保留参数张量的属性(形状,数据类型)。
```
py
x_ones
=
torch
.
ones_like
(
x_data
)
# retains the properties of x_data
```
python
x_ones
=
torch
.
ones_like
(
x_data
)
# 保留 x_data 的属性
print
(
f
"Ones Tensor:
\n
{
x_ones
}
\n
"
)
x_rand
=
torch
.
rand_like
(
x_data
,
dtype
=
torch
.
float
)
# overrides the datatype of x_data
x_rand
=
torch
.
rand_like
(
x_data
,
dtype
=
torch
.
float
)
# 重写 x_data 的数据类型
int
->
float
print
(
f
"Random Tensor:
\n
{
x_rand
}
\n
"
)
```
出:
显示:
```
py
```
py
thon
Ones
Tensor
:
tensor
([[
1
,
1
],
[
1
,
1
]])
[
1
,
1
]])
Random
Tensor
:
tensor
([[
0.2143
,
0.8153
],
[
0.5212
,
0.8607
]])
tensor
([[
0.0381
,
0.5780
],
[
0.3963
,
0.0840
]])
```
**4. 通过指定数据维度来生成张量**
**具有随机或恒定值**
:
`shape`
是元组类型, 用来描述张量的维数, 下面3个函数通过传入
`shape`
来指定生成张量的维数。
`shape`
是张量尺寸的元组。 在下面的函数中,它确定输出张量的维数。
```
py
```
python
shape
=
(
2
,
3
,)
rand_tensor
=
torch
.
rand
(
shape
)
ones_tensor
=
torch
.
ones
(
shape
)
...
...
@@ -75,169 +69,157 @@ zeros_tensor = torch.zeros(shape)
print
(
f
"Random Tensor:
\n
{
rand_tensor
}
\n
"
)
print
(
f
"Ones Tensor:
\n
{
ones_tensor
}
\n
"
)
print
(
f
"Zeros Tensor:
\n
{
zeros_tensor
}
"
)
```
出:
显示:
```
py
```
py
thon
Random
Tensor
:
tensor
([[
0.
6513
,
0.6193
,
0.5550
],
[
0.7230
,
0.3545
,
0.9288
]])
tensor
([[
0.
0266
,
0.0553
,
0.9843
],
[
0.0398
,
0.8964
,
0.3457
]])
Ones
Tensor
:
tensor
([[
1.
,
1.
,
1.
],
[
1.
,
1.
,
1.
]])
[
1.
,
1.
,
1.
]])
Zeros
Tensor
:
tensor
([[
0.
,
0.
,
0.
],
[
0.
,
0.
,
0.
]])
[
0.
,
0.
,
0.
]])
```
* * *
## 张量属性
张量属性描述了它们的形状,数据类型以及存储它们的设备。
从张量属性我们可以得到张量的维数、数据类型以及它们所存储的设备(CPU或GPU)。
来看一个简单的例子:
```
py
```
py
thon
tensor
=
torch
.
rand
(
3
,
4
)
print
(
f
"Shape of tensor:
{
tensor
.
shape
}
"
)
print
(
f
"Datatype of tensor:
{
tensor
.
dtype
}
"
)
print
(
f
"Device tensor is stored on:
{
tensor
.
device
}
"
)
```
出:
```
py
Shape
of
tensor
:
torch
.
Size
([
3
,
4
])
Datatype
of
tensor
:
torch
.
float32
Device
tensor
is
stored
on
:
cpu
显示:
```
python
Shape
of
tensor
:
torch
.
Size
([
3
,
4
])
# 维数
Datatype
of
tensor
:
torch
.
float32
# 数据类型
Device
tensor
is
stored
on
:
cpu
# 存储设备
```
* * *
## 张量运算
[
在此处
](
https://pytorch.org/docs/stable/torch.html
)
全面描述了超过 100 个张量运算,包括转置,索引,切片,数学运算,线性代数,随机采样等
。
有超过100种张量相关的运算操作, 例如转置、索引、切片、数学运算、线性代数、随机采样等。更多的运算可以在这里
[
查看
](
https://pytorch.org/docs/stable/torch.html
)
。
它们每个都可以在 GPU 上运行(通常比 CPU 上更高的速度)。 如果您使用的是 Colab,请通过转到“编辑”>“笔记本设置”来分配 GPU
。
所有这些运算都可以在GPU上运行(相对于CPU来说可以达到更高的运算速度)。如果你使用的是Google的Colab环境, 可以通过
`Edit > Notebook Settings`
来分配一个GPU使用
。
```
py
#
We move our tensor to the GPU if available
```
py
thon
#
判断当前环境GPU是否可用, 然后将tensor导入GPU内运行
if
torch
.
cuda
.
is_available
():
tensor
=
tensor
.
to
(
'cuda'
)
```
尝试从列表中进行一些操作。 如果您熟悉 NumPy API,则可以轻松使用张量 API
。
光说不练假把式, 接下来的例子一定要动手跑一跑。如果你对Numpy的运算非常熟悉的话, 那tensor的运算对你来说就是小菜一碟
。
**
类似 Numpy 的标准索引和切片**
:
**
1. 张量的索引和切片**
```
py
```
py
thon
tensor
=
torch
.
ones
(
4
,
4
)
tensor
[:,
1
]
=
0
tensor
[:,
1
]
=
0
# 将第1列(从0开始)的数据全部赋值为0
print
(
tensor
)
```
出:
显示:
```
py
```
py
thon
tensor
([[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
]])
```
**连接张量**
可以使用
`torch.cat`
沿给定维度连接一系列张量。 另请参见
[
`torch.stack`
](
https://pytorch.org/docs/stable/generated/torch.stack.html
)
,这是另一个与
`torch.cat`
稍有不同的张量连接操作。
**2. 张量的拼接**
你可以通过
`torch.cat`
方法将一组张量按照指定的维度进行拼接, 也可以参考
[
`torch.stack`
](
https://pytorch.org/docs/stable/generated/torch.stack.html
)
方法。这个方法也可以实现拼接操作, 但和
`torch.cat`
稍微有点不同。
```
py
```
py
thon
t1
=
torch
.
cat
([
tensor
,
tensor
,
tensor
],
dim
=
1
)
print
(
t1
)
```
出:
显示:
```
py
```
tensor([[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.],
[1., 0., 1., 1., 1., 0., 1., 1., 1., 0., 1., 1.]])
```
**相乘张量**
```
py
# This computes the element-wise product
print
(
f
"tensor.mul(tensor)
\n
{
tensor
.
mul
(
tensor
)
}
\n
"
)
# Alternative syntax:
print
(
f
"tensor * tensor
\n
{
tensor
*
tensor
}
"
)
**3. 张量的乘积和矩阵乘法**
```
python
# 逐个元素相乘结果
print
(
f
"tensor.mul(tensor):
\n
{
tensor
.
mul
(
tensor
)
}
\n
"
)
# 等价写法:
print
(
f
"tensor * tensor:
\n
{
tensor
*
tensor
}
"
)
```
出:
显示:
```
py
tensor
.
mul
(
tensor
)
```
py
thon
tensor
.
mul
(
tensor
)
:
tensor
([[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
]])
tensor
*
tensor
tensor
*
tensor
:
tensor
([[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
]])
```
计算两个张量之间的矩阵乘法
```
py
print
(
f
"tensor.matmul(tensor.T)
\n
{
tensor
.
matmul
(
tensor
.
T
)
}
\n
"
)
# Alternative syntax:
print
(
f
"tensor @ tensor.T
\n
{
tensor
@
tensor
.
T
}
"
)
下面写法表示张量与张量的矩阵乘法:
```
python
print
(
f
"tensor.matmul(tensor.T):
\n
{
tensor
.
matmul
(
tensor
.
T
)
}
\n
"
)
# 等价写法:
print
(
f
"tensor @ tensor.T:
\n
{
tensor
@
tensor
.
T
}
"
)
```
出:
显示:
```
py
tensor
.
matmul
(
tensor
.
T
)
```
py
thon
tensor
.
matmul
(
tensor
.
T
)
:
tensor
([[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
]])
tensor
@
tensor
.
T
tensor
@
tensor
.
T
:
tensor
([[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
],
[
3.
,
3.
,
3.
,
3.
]])
```
**原地操作**
后缀为
`_`
的操作就位。 例如:
`x.copy_(y)`
,
`x.t_()`
将更改为
`x`
。
**4. 自动赋值运算**
自动赋值运算通常在方法后有
`_`
作为后缀, 例如:
`x.copy_(y)`
,
`x.t_()`
操作会改变
`x`
的取值。
```
py
```
py
thon
print
(
tensor
,
"
\n
"
)
tensor
.
add_
(
5
)
print
(
tensor
)
```
出:
显示:
```
py
```
py
thon
tensor
([[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
[
1.
,
0.
,
1.
,
1.
],
...
...
@@ -247,77 +229,58 @@ tensor([[6., 5., 6., 6.],
[
6.
,
5.
,
6.
,
6.
],
[
6.
,
5.
,
6.
,
6.
],
[
6.
,
5.
,
6.
,
6.
]])
```
注意
原地操作可以节省一些内存,但是在计算导数时可能会因为立即丢失历史记录而出现问题。 因此,不鼓励使用它们
。
> 注意:
>
> 自动赋值运算虽然可以节省内存, 但在求导时会因为丢失了中间过程而导致一些问题, 所以我们并不鼓励使用它
。
* * *
## 与 NumPy 桥接
CPU 和 NumPy 数组上的张量可以共享其基础内存位置,更改一个将更改另一个。
### 张量到 NumPy 数组
```
py
## <span id="jump">Tensor与Numpy的转化</span>
张量和
`Numpy array`
数组在CPU上可以共用一块内存区域, 改变其中一个另一个也会随之改变。
**1. 由张量变换为Numpy array数组**
```
python
t
=
torch
.
ones
(
5
)
print
(
f
"t:
{
t
}
"
)
n
=
t
.
numpy
()
print
(
f
"n:
{
n
}
"
)
```
出:
```
py
显示:
```
python
t
:
tensor
([
1.
,
1.
,
1.
,
1.
,
1.
])
n
:
[
1
\
.
1
\
.
1
\
.
1
\
.
1.
]
n
:
[
1.
1.
1.
1.
1.
]
```
张量的变化反映在 NumPy 数组中。
```
py
修改张量的值,则
`Numpy array`
数组值也会随之改变。
```
python
t
.
add_
(
1
)
print
(
f
"t:
{
t
}
"
)
print
(
f
"n:
{
n
}
"
)
```
出:
```
py
显示:
```
python
t
:
tensor
([
2.
,
2.
,
2.
,
2.
,
2.
])
n
:
[
2
\
.
2
\
.
2
\
.
2
\
.
2.
]
n
:
[
2.
2.
2.
2.
2.
]
```
### 将 NumPy 数组转换为张量
**2. 由Numpy array数组转为张量**
```
py
```
py
thon
n
=
np
.
ones
(
5
)
t
=
torch
.
from_numpy
(
n
)
```
NumPy 数组中的更改反映在张量中
。
修改
`Numpy array`
数组的值,则张量值也会随之改变
。
```
py
```
py
thon
np
.
add
(
n
,
1
,
out
=
n
)
print
(
f
"t:
{
t
}
"
)
print
(
f
"n:
{
n
}
"
)
```
出:
显示:
```
py
```
py
thon
t
:
tensor
([
2.
,
2.
,
2.
,
2.
,
2.
],
dtype
=
torch
.
float64
)
n
:
[
2
\
.
2
\
.
2
\
.
2
\
.
2.
]
n
:
[
2.
2.
2.
2.
2.
]
```
**脚本的总运行时间**
:(0 分钟 0.045 秒)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录