【问题标题】:Convolution with zero mean using keras使用 keras 进行零均值卷积
【发布时间】:2019-12-26 03:47:09
【问题描述】:

您如何实施conv2d,其中每个过滤器的均值为零。

我尝试通过 conv2d 中的 kernel_regularizer 参数来执行此操作,但由于某种原因遇到了问题。

def zero_mean_regularizer(weight_matrix):
    # weight matrix is channel last
    return weight_matrix - K.mean(weight_matrix, axis=(1, 2), keepdims=True)

但由于某种原因,我从 ModelCheckpoint 回调中收到了一个神秘错误。

self = <keras.callbacks.ModelCheckpoint object at 0x12e890358>, epoch = 0
logs = {'loss': array([[[[-0.24377288,  0.4010657 ,  0.03990834, -0.19173835,
           0.02325685, -0.12445911,  0.34307766...0454,  0.18098758,  0.05493904,
          -0.15479018, -0.19435076,  0.07913151,  0.20207654]]]],
      dtype=float32)}

    def on_epoch_end(self, epoch, logs=None):
        logs = logs or {}
        self.epochs_since_last_save += 1
        if self.epochs_since_last_save >= self.period:
            self.epochs_since_last_save = 0
            filepath = self.filepath.format(epoch=epoch + 1, **logs)
            if self.save_best_only:
                current = logs.get(self.monitor)
                if current is None:
                    warnings.warn('Can save best model only with %s available, '
                                  'skipping.' % (self.monitor), RuntimeWarning)
                else:
>                   if self.monitor_op(current, self.best):
E                   ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

似乎这个正则化器导致模型为单个时期创建多个损失值。

【问题讨论】:

    标签: python tensorflow keras conv-neural-network


    【解决方案1】:

    我认为您想将其用作约束,而不是正则化器。正则化器应该返回一个添加到损失中的标量值。这将鼓励某些行为(通过以较低的损失奖励它),但不会强制它。

    很可能,Keras 正在尝试将正则化器的返回参数(0 均值内核)添加到损失中,这会产生非标量损失值,这反过来会导致问题(并且不会)真的没有任何意义)。

    另一方面,kernel_constraint 是一个函数,它采用当前内核并返回一个新值,该值可能会以某种方式被修改,例如强制为 0。

    tl;dr:在您的模型代码中,将零均值函数用作kernel_constraint,而不是作为正则化器。

    【讨论】:

      最近更新 更多