且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在 tensorflow 中表达这个自定义损失函数?

更新时间:2023-12-02 20:12:46

1.torch.finfo() 做了什么以及如何在 TensorFlow 中表达它?

.finfo() 提供了一种获得浮点类型的机器限制的巧妙方法.此函数在 NumpyTorch 以及 Torlowerreferr实验性.

1. What does torch.finfo() do and how to express it in TensorFlow?

.finfo() provides a neat way to get machine limits for floating-point types. This function is available in Numpy, Torch as well as Tensorflow experimental.

.finfo().max 返回可表示为该 dtype 的最大可能数.

.finfo().max returns the largest possible number representable as that dtype.

注意:还有一个用于整数类型的 .iinfo().

NOTE: There is also a .iinfo() for integer types.

这里有几个 finfoiinfo 的例子.

Here are a few examples of finfo and iinfo in action.

print('FLOATS')
print('float16',torch.finfo(torch.float16).max)
print('float32',torch.finfo(torch.float32).max)
print('float64',torch.finfo(torch.float64).max)
print('')
print('INTEGERS')
print('int16',torch.iinfo(torch.int16).max)
print('int32',torch.iinfo(torch.int32).max)
print('int64',torch.iinfo(torch.int64).max)

FLOATS
float16 65504.0
float32 3.4028234663852886e+38
float64 1.7976931348623157e+308

INTEGERS
int16 32767
int32 2147483647
int64 9223372036854775807

如果你想在tensorflow中实现这个,你可以使用tf.experimental.numpy.finfo来解决这个问题.

If you want to implement this in tensorflow, you can use tf.experimental.numpy.finfo to solve this.

print(tf.experimental.numpy.finfo(tf.float32))
print('Max ->',tf.experimental.numpy.finfo(tf.float32).max)  #<---- THIS IS WHAT YOU WANT

Machine parameters for float32
---------------------------------------------------------------
precision =   6   resolution = 1.0000000e-06
machep =    -23   eps =        1.1920929e-07
negep =     -24   epsneg =     5.9604645e-08
minexp =   -126   tiny =       1.1754944e-38
maxexp =    128   max =        3.4028235e+38
nexp =        8   min =        -max
---------------------------------------------------------------

Max -> 3.4028235e+38

2.y_hat.dtype 是否只返回数据类型?

是的.

在 Torch 中,它会返回 torch.float32 或类似的东西.在 Tensorflow 中,它会返回 tf.float32 或类似的东西.

In torch, it would return torch.float32 or something like that. In Tensorflow it would return tf.float32 or something like that.