网络编程
位置:首页>> 网络编程>> Python编程>> pytorch和numpy默认浮点类型位数详解

pytorch和numpy默认浮点类型位数详解

作者:gy笨瓜  发布时间:2021-11-10 10:19:49 

标签:pytorch,numpy,默认浮点类型,位数

pytorch和numpy默认浮点类型位数

numpy中默认浮点类型为64位,pytorch中默认浮点类型位32位

测试代码如下

  • numpy版本:1.19.2

  • pytorch版本:1.2.0

In [1]: import torch
In [2]: import numpy as np
# 版本信息
In [3]: "pytorch version: {}, numpy version: {}".format(torch.__version__, np.__version__)
Out[3]: 'pytorch version: 1.2.0, numpy version: 1.19.2'

# numpy
In [4]: dat_np = np.array([1,2,3], dtype="float")
In [5]: dat_np.dtype
Out[5]: dtype('float64')

# pytorch
In [6]: dat_torch = torch.tensor([1,2,3])
In [7]: dat_torch = dat_torch.float()
In [8]: dat_torch.dtype
Out[8]: torch.float32

pytorch和numpy的默认类型与转换问题

pytorch对于浮点类型默认为float32,而numpy的默认类型是float64,转换的代码:

torch.from_numpy(a).type(torch.FloatTensor)
torch.from_numpy(np.float32(a))

来源:https://blog.csdn.net/u012633319/article/details/113784143

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com