Skip to content

Commit

Permalink
fix: tensors created by torch.tensor don't share memory with ndarrays…
Browse files Browse the repository at this point in the history
…,while tensors created by torch.from_numpy share memory with ndarrays.
  • Loading branch information
surpoloyang committed Jun 22, 2024
1 parent e6b18cc commit 6e480ae
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions chapter_preliminaries/ndarray.md
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ computation(X, Y)

:begin_tab:`pytorch`
将深度学习框架定义的张量[**转换为NumPy张量(`ndarray`**]很容易,反之也同样容易。
torch张量和numpy数组将共享它们的底层内存,就地操作更改一个张量也会同时更改另一个张量。
通过torch.numpy将torch张量转换为numpy数组,或是通过torch.from_numpy将numpy数组转换为torch张量,torch张量和numpy数组将共享它们的底层内存,就地操作更改一个张量也会同时更改另一个张量。而torch.tensor是为numpy数组新建一个副本,二者不共享内存
:end_tab:

```{.python .input}
Expand All @@ -614,7 +614,7 @@ type(A), type(B)
```{.python .input}
#@tab pytorch
A = X.numpy()
B = torch.tensor(A)
B = torch.from_numpy(A)
type(A), type(B)
```

Expand Down

0 comments on commit 6e480ae

Please sign in to comment.