
PyTorch中Tensor张量的数据类型转换方法
5星
- 浏览量: 0
- 大小:None
- 文件类型:None
简介:
本篇文章主要介绍在深度学习框架PyTorch中如何进行Tensor(张量)数据类型的转换,帮助读者掌握不同场景下的使用技巧。
1. tensor张量与numpy相互转换
tensor 转 numpy:
```python
import torch
a = torch.ones([2,5])
# 输出为:
tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
b = a.numpy()
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]], dtype=float32)
```
numpy 转 tensor:
```python
import numpy as np
a = np.ones([2,5])
# 输出为:
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
b = torch.from_numpy(a)
```
注意:上述代码片段展示了如何在PyTorch的tensor和numpy数组之间进行转换。
全部评论 (0)
还没有任何评论哟~


