DeepCTR icon indicating copy to clipboard operation
DeepCTR copied to clipboard

请问layers中的NoMask的作用是什么?

Open luckmoon opened this issue 5 years ago • 4 comments

luckmoon avatar Nov 10 '20 08:11 luckmoon

同问,看起来是没什么用的样子

weiiiiiqi avatar Dec 10 '20 08:12 weiiiiiqi

似乎是因为mask_zero造成的, mask_zero之后, 很多keras内部的功能就不能用了, 比如Flatten等等, 过一个空层之后, 就方便进行处理了。

hongtsingwang avatar Apr 23 '21 09:04 hongtsingwang

keras 如果在前一层支持 mask 的话,后面的层也必须支持,否则会报错,除非你自己再单独自定义一个类

yunchenran avatar Feb 11 '22 01:02 yunchenran

keras 如果在前一层支持 mask 的话,后面的层也必须支持,否则会报错,除非你自己再单独自定义一个类

实验了一下不会报错啊

import tensorflow as tf
from tensorflow.keras.layers import Concatenate, Embedding, Flatten

padded_inputs = np.array([[711,  632,   71,    0,    0,    0],
                          [73,    8, 3215,   55,  927,    0],
                          [83,   91,    1,  645, 1253,  927]])
x = Embedding(4000, 4, mask_zero=True)(padded_inputs)
y = Embedding(4000, 4, mask_zero=True)(padded_inputs)
x = x*tf.expand_dims(tf.cast(x._keras_mask, tf.float32), axis=-1)
y = y*tf.expand_dims(tf.cast(y._keras_mask, tf.float32), axis=-1)
a = tf.reduce_sum(x, axis=1, keepdims=True)
b = tf.reduce_sum(y, axis=1, keepdims=True)


Flatten()(Concatenate(axis=-1)([a, b]))

# <tf.Tensor: shape=(3, 8), dtype=float32, numpy=
# array([[ 0.03616517, -0.1038537 , -0.04291585,  0.07384852, -0.04866493,
#         -0.05156649,  0.0461111 ,  0.00820675],
#        [ 0.01083413, -0.04240791,  0.00778643,  0.0292058 ,  0.06188046,
#          0.02338365, -0.00932642, -0.03607894],
#        [-0.03638362,  0.03723145,  0.0550371 , -0.0271023 , -0.01352489,
#          0.0177963 , -0.05491198, -0.11716884]], dtype=float32)>
print(tf.__version__)
# 2.3.1

@yunchenran @hongtsingwang 在 #424 中,将NoMask去掉好像也没什么问题

pjgao avatar Apr 26 '22 14:04 pjgao