tensorflow:寄存器numpy bfloat16扩展

2024-10-05 13:13:54 发布

您现在位置:Python中文网/ 问答频道 /正文

正如我所看到的,tensorflow中bfloat16有一个numpy扩展:

https://github.com/tensorflow/tensorflow/blob/24ffe9f729160a095a5cab8f5923920182803182/tensorflow/python/lib/core/bfloat16_wrapper.cc

可以通过调用RegisterNumpyBfloat16来启用此扩展。或者至少应该这样。我已安装tensorflow 2.4.1并尝试启用该扩展,但出现以下错误:

> tf.RegisterNumpyBfloat16()

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-4f89beb80796> in <module>
----> 1 tf.RegisterNumpyBfloat16()

AttributeError: module 'tensorflow' has no attribute 'RegisterNumpyBfloat16'

有人知道我做错了什么吗?或者如何启用此numpy扩展

多谢各位


Tags: httpscorenumpygithubcomlibtftensorflow
2条回答

我刚刚在TensorFlow 2.4.0和NumPy 1.19.4上试过这个

import numpy as np
import tensorflow as tf

bfloat16 = tf.bfloat16.as_numpy_dtype

np.array([1.0, 2.0, 3.0], dtype=bfloat16)
# array([bfloat16(1), bfloat16(2), bfloat16(3)], dtype=bfloat16)

试试这个:

from tensorflow.python import _pywrap_bfloat16


bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()
print(bfloat16)
# <class 'bfloat16'>

print(bfloat16(1.0))
# bfloat16(1)

相关问题 更多 >

    热门问题