如何在神经网络训练的损失函数中执行快速傅里叶变换

我目前正在处理一个全卷积神经网络(输入图像,输出图像),并尝试实现一个损失函数,该函数在对两张图像进行某些操作之前,先对它们进行快速傅里叶变换,代码如下所示

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 lossVwith strategy.scope():  model = hd_unet_model(INPUT_SIZE)  model.compile(optimizer=Adam(lr=0.1),                loss= fourierLoss2,                metrics=tf.keras.metrics.MeanSquaredError())

两个张量(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 没有注册的梯度函数。有人知道如何解决这个问题吗?


回答:

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

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注