提交 60c1ebd0 编写于 作者: 考拉不是大叔's avatar 考拉不是大叔

a

上级 16028c40
......@@ -235,7 +235,28 @@ tensor([[6., 5., 6., 6.],
>
> 自动赋值运算虽然可以节省内存, 但在求导时会因为丢失了中间过程而导致一些问题, 所以我们并不鼓励使用它。
## <span id="jump">tensor与Numpy的转化</span>
## <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}")
```
显示:
```python
t: tensor([1., 1., 1., 1., 1.])
n: [1. 1. 1. 1. 1.]
```
修改张量的值,则`Numpy array`数组值也会随之改变。
```python
t.add_(1)
print(f"t: {t}")
print(f"n: {n}")
```
显示:
```python
t: tensor([2., 2., 2., 2., 2.])
n: [2. 2. 2. 2. 2.]
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册