我想这已经解释得很清楚了
Github issue
softmax()
仍在操作
0.0
值并返回
non-zero
link
).
只有这样才能从
softmax()
就是通过考试
. 如果将屏蔽值设置为
float64
,
Softmax()
要获得float64上的机器限制,您需要
tf.float64.min
-1.7976931348623157e+308
post
.
下面是一个实现,供您参考
tf.boolean_mask
tf.where
用于创建遮罩并将其传递给
softmax()
import tensorflow as tf
inputs = np.zeros([1,5])
inputs[0,1] = 0.5
inputs[0,2] = 0.1
inputs = tf.Variable(inputs)
#Returns only the elements that are not masked (2,)
with_boolmask = tf.boolean_mask(inputs, inputs!=0)
with_boolmask = tf.keras.layers.Softmax()(with_boolmask)
#Correct way to do it!
masked_inp = tf.where(inputs!=0, inputs, tf.float64.min) #<----
with_where = tf.keras.layers.Softmax()(masked_inp)
print('BOOLEAN MASK (NOT EXPECTED)')
print(with_boolmask)
print('')
print('MASKED INPUT - ')
print(masked_inp)
print('')
print('SOFTMAX OUTPUT')
print(with_where)
BOOLEAN MASK (NOT EXPECTED)
tf.Tensor([0.59868765 0.40131232], shape=(2,), dtype=float32)
MASKED INPUT -
tf.Tensor(
[[-1.79769313e+308 5.00000000e-001 1.00000000e-001 -1.79769313e+308
-1.79769313e+308]], shape=(1, 5), dtype=float64)
SOFTMAX OUTPUT
tf.Tensor([[0. 0.59868765 0.40131232 0. 0. ]], shape=(1, 5), dtype=float32)