将tensor转化为numpy
时间: 2023-10-15 20:02:45 浏览: 88
pytorch 实现tensor与numpy数组转换
在Python中,可以使用`numpy()`函数将PyTorch张量(tensor)转换为NumPy数组。示例如下:
```python
import torch
# 创建一个tensor
x = torch.rand(3, 4)
print(x)
# 将tensor转换为numpy数组
x_np = x.numpy()
print(x_np)
```
输出:
```
tensor([[0.7859, 0.7165, 0.9594, 0.5908],
[0.9069, 0.0752, 0.5431, 0.6825],
[0.7306, 0.1635, 0.2345, 0.1770]])
[[0.7859413 0.71647847 0.9594089 0.59084415]
[0.9069421 0.07516301 0.54306674 0.68253046]
[0.7305553 0.16349268 0.23447865 0.17696494]]
```
阅读全文