【发布时间】:2021-01-15 23:55:04
【问题描述】:
我正在恢复这个 github issue,因为我相信它是有效的并且需要解释。 tf.keras 有一个掩蔽层,其文档内容为
对于输入张量中的每个时间步(张量中的第 1 维),如果 在那个时间步,输入张量中的所有值都等于 mask_value,则时间步将全部被屏蔽(跳过) 下游层(只要它们支持遮罩)。
如果任何下游层不支持屏蔽但收到这样的 输入掩码,将引发异常。
# create padded zeros and change two valid entries.
inputs = np.zeros([1,5])
inputs[0,1] = 0.5
inputs[0,2] = 0.1
inputs = tf.Variable(inputs)
masked_inputs = tf.keras.layers.Masking(mask_value=0.0)(inputs)
with_masking = tf.keras.layers.Softmax()(masked_inputs)
without_masking = tf.keras.layers.Softmax()(inputs)
这两个结果几乎相同
with_masking
<tf.Tensor: shape=(1, 5), dtype=float32, numpy=
array([[0.1737954 , 0.28654018, 0.19207363, 0.1737954 , 0.1737954 ]],
dtype=float32)>
without_masking
<tf.Tensor: shape=(1, 5), dtype=float64, numpy=array([[0.1737954 , 0.28654017, 0.19207362, 0.1737954 , 0.1737954 ]])>
预期行为
我希望只对有效条目进行 softmax,类似于
#Assign one large value
inputs = np.zeros([1,2])
inputs[0,0] = 0.5
inputs[0,1] = 0.1
inputs = tf.Variable(inputs)
without_masking = tf.keras.layers.Softmax()(inputs)
without_masking
<tf.Tensor: shape=(1, 2), dtype=float64, numpy=array([[0.59868766, 0.40131234]])>
填充在正确的位置
with_masking
<tf.Tensor: shape=(1, 5), dtype=float32, numpy=
array([[0 , 0.59868766, 0.40131234, 0, 0 ]],
dtype=float32)>
要忽略 softmax 函数中的 0,我们可以切换出大量负数吗?
相关:tensorflow - softmax ignore negative labels (just like caffe)
from tensorflow import __version__
__version__
'2.3.1'
【问题讨论】:
标签: tensorflow keras deep-learning