训练模型后Keras遮罩破裂

时间:2020-06-09 00:58:41

标签: tensorflow keras masking

在训练模型之前,我的遮罩层正确地将不需要的输入清零。在模型编译和训练后,它会输出非零值:

import numpy as np
from tensorflow.keras.layers import Masking,Input,TimeDistributed,Dense
a = np.array([[[3,1,2],[-1,-1,-1],[2,1,1],[-1,-1,-1],[-1,-1,-1]], [[3,1,2],[-1,-1,-1],[2,1,1],[-1,-1,-1],[-1,-1,-1]]]).astype(np.float32)

rel_inputs = Input(shape=(a.shape[-2], a.shape[-1],))
masked = Masking(mask_value=-1.0)(rel_inputs)
x = TimeDistributed(Dense(16, activation='relu',
                          kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4),
                          bias_regularizer=regularizers.l2(1e-4),
                          activity_regularizer=regularizers.l2(1e-5)))
x2 = TimeDistributed(Dense(32, activation='relu',
                          kernel_regularizer=regularizers.l1_l2(l1=1e-5, l2=1e-4),
                          bias_regularizer=regularizers.l2(1e-4),
                          activity_regularizer=regularizers.l2(1e-5)))
rel_map = x(masked)
rel_map = Dropout(0.3)(rel_map)
rel_map = x2(rel_map)
rel_map = Dropout(0.3)(rel_map)
s = TimeDistributed(Dense(1))
output = s(rel_map)
output = Reshape((5,))(output)
#output = Lambda(lambda x: K.squeeze(x, -1))(output)
#irr_score = s(irr_map)
#diff = Subtract()([rel_score, irr_score])
model = Model(inputs=rel_inputs, outputs=output, name='rank_net_basic')
q = model.predict(a)
print (q)

enter image description here

0 个答案:

没有答案