首页 > 解决方案 > 如何在神经网络训练的损失函数中执行快速傅立叶变换

问题描述

我目前正在研究一个完全卷积神经网络(图像输入,图像输出),我正在尝试实现一个损失函数,在对它们进行一些操作之前对 2 个图像进行快速傅立叶变换,代码是这样的

def fourierLoss2(y_actual,y_pred):
  actual_fft = tf.signal.rfft3d(y_actual)
  pred_fft = tf.signal.rfft3d(y_pred)
  lossV=tf.math.real(tf.math.reduce_mean(tf.math.square(actual_fft-pred_fft)))
  return lossV

with strategy.scope():
  model = hd_unet_model(INPUT_SIZE)
  model.compile(optimizer=Adam(lr=0.1),
                loss= fourierLoss2,
                metrics=tf.keras.metrics.MeanSquaredError())

2 个张量 (y_actual,y_pred) 具有浮点类型。但是如果我尝试训练模型,我会收到以下错误

    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:806 train_function  *
        return step_function(self, iterator)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:796 step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:1211 run
        return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/distribute_lib.py:2585 call_for_each_replica
        return self._call_for_each_replica(fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/mirrored_strategy.py:585 _call_for_each_replica
        self._container_strategy(), fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/mirrored_run.py:96 call_for_each_replica
        return _call_for_each_replica(strategy, fn, args, kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/mirrored_run.py:237 _call_for_each_replica
        coord.join(threads)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/coordinator.py:389 join
        six.reraise(*self._exc_info_to_raise)
    /usr/local/lib/python3.6/dist-packages/six.py:703 reraise
        raise value
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/coordinator.py:297 stop_on_exception
        yield
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/distribute/mirrored_run.py:323 run
        self.main_result = self.main_fn(*self.main_args, **self.main_kwargs)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:789 run_step  **
        outputs = model.train_step(data)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:757 train_step
        self.trainable_variables)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/training.py:2722 _minimize
        gradients = tape.gradient(loss, trainable_variables)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/backprop.py:1073 gradient
        unconnected_gradients=unconnected_gradients)
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/imperative_grad.py:77 imperative_grad
        compat.as_str(unconnected_gradients.value))
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/eager/backprop.py:151 _gradient_function
        grad_fn = ops._gradient_registry.lookup(op_name)  # pylint: disable=protected-access
    /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/registry.py:97 lookup
        "%s registry has no entry for: %s" % (self._name, name))

    LookupError: gradient registry has no entry for: RFFT3D

经过一些研究,我了解到问题在于操作 tf.signal.rfft3d 没有注册的梯度函数。有人知道解决这个问题的方法吗?

标签: pythontensorflowmachine-learningkerastensor

解决方案


我已经找到了解决问题的方法,而不是使用tf.signal.rfft3d我必须使用tf.signal.fft3d,这个函数有一个梯度条目并在损失函数中工作,缺点是现在我必须在之前将浮点张量转换为复杂类型傅立叶变换


推荐阅读