Tensorflow 2.2 [预测必须> = 0] [条件x> = y不按元素进行:] [xdense_1 / Sigmoid:0=]

问题描述

我正在使用TensorFlow 2.2和Keras训练二进制检测架构。以前,如果我将数据加载到与模型训练相同的脚本中,则可以进行此工作。但是,当我使用更大的数据集(x6个以上的样本,正样本与负样本的比率相同)时,我现在得到了这一组错误(它运行了5-10个时期(我多次运行)),然后才给出此错误):

tensorflow.python.framework.errors_impl.InvalidArgumentError: 2 root error(s) found.
  (0) Invalid argument:  assertion failed: [predictions must be >= 0] [Condition x >= y did not hold element-wise:] [x (dense_1/Sigmoid:0) = ] [[[nan][nan][nan]]...] [y (Cast_4/x:0) = ] [0]
     [[{{node assert_greater_equal/Assert/AssertGuard/else/_1/Assert}}]]
     [[gradient_tape/point_conv_fp_1/ScatterNd/_192]]
  (1) Invalid argument:  assertion failed: [predictions must be >= 0] [Condition x >= y did not hold element-wise:] [x (dense_1/Sigmoid:0) = ] [[[nan][nan][nan]]...] [y (Cast_4/x:0) = ] [0]
     [[{{node assert_greater_equal/Assert/AssertGuard/else/_1/Assert}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_function_14820]

这是架构:

Architecture

这是与出现错误的图层有关的代码:

# initialisation
..
# point_conv_sa layers
..
self.dense4 = keras.layers.Dense(128,activation=tf.nn.elu)
self.bn4 = keras.layers.BatchNormalization()
self.dropout4 = keras.layers.Dropout(0.5)

# This line corresponds to 'dense_1' in the image
self.dense_fin = keras.layers.Dense(self.num_classes,activation=tf.nn.sigmoid,bias_initializer=self.initial_bias)

# training step
..
# point_conv_fp layers
..
net = self.dense4(points)
net = self.bn4(net)
net = self.dropout4(net)

pred = self.dense_fin(net)

return pred

与Im使用的损失函数有关。我使用keras.losses.BinaryCrossentropy(),无论大小数据集都没有问题。然后我根据https://github.com/mkocabas/focal-loss-keras更改了焦点损失,但对于大型数据集,它失败了:

def focal_loss(gamma=2.,alpha=.25):
    def focal_loss_fixed(y_true,y_pred):
        pt_1 = tf.where(tf.equal(y_true,1),y_pred,tf.ones_like(y_pred))
        pt_0 = tf.where(tf.equal(y_true,0),tf.zeros_like(y_pred))
        return -K.mean(alpha * K.pow(1. - pt_1,gamma) * K.log(pt_1)) - K.mean((1 - alpha) * K.pow(pt_0,gamma) * K.log(1. - pt_0))
    return focal_loss_fixed

....
model.compile(
optimizer=keras.optimizers.Adam(config['lr']),loss = focal_loss(alpha=config['fl_alpha'],gamma=config['fl_gamma']),metrics=[Precision(),Recall(),AUC()]
)

让我知道是否需要更多信息。

欢呼

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...