'torch.DoubleTensor' 变 'torch.FloatTensor'
时间: 2023-06-27 11:02:07 浏览: 105
可以使用 `:float()` 方法将 `torch.DoubleTensor` 类型的 Tensor 转换为 `torch.FloatTensor` 类型的 Tensor。
示例代码如下:
```python
import torch
# 创建一个 DoubleTensor
double_tensor = torch.randn(3, 4, dtype=torch.double)
print(double_tensor)
print(double_tensor.type()) # 输出当前 Tensor 类型
# 将 DoubleTensor 转换为 FloatTensor
float_tensor = double_tensor.float()
print(float_tensor)
print(float_tensor.type()) # 输出当前 Tensor 类型
```
输出结果:
```
tensor([[-0.0659, -0.2944, 0.0803, -0.5139],
[ 1.3551, -0.7856, -0.4142, -0.5371],
[ 2.1308, -0.0817, -0.6928, -1.3455]], dtype=torch.float64)
torch.DoubleTensor
tensor([[-0.0659, -0.2944, 0.0803, -0.5139],
[ 1.3551, -0.7856, -0.4142, -0.5371],
[ 2.1308, -0.0817, -0.6928, -1.3455]])
torch.FloatTensor
```
阅读全文