日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù)詳解_python

作者:gy笨瓜 ? 更新時(shí)間: 2023-04-01 編程語言

pytorch和numpy默認(rèn)浮點(diǎn)類型位數(shù)

numpy中默認(rèn)浮點(diǎn)類型為64位,pytorch中默認(rèn)浮點(diǎn)類型位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的默認(rèn)類型與轉(zhuǎn)換問題

pytorch對于浮點(diǎn)類型默認(rèn)為float32,而numpy的默認(rèn)類型是float64,轉(zhuǎn)換的代碼:

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

總結(jié)

原文鏈接:https://blog.csdn.net/u012633319/article/details/113784143

欄目分類
最近更新