首页 > 解决方案 > 在 Keras 神经网络中应用 scipy stats 函数作为层

问题描述

我想在 Keras 神经网络中应用一个 scipy stats 函数作为一个层,如下所示:

from scipy import stats

class BoxCox(layers.Layer):

    def call(self, inputs):
        return stats.boxcox(inputs)

# part of usage in model
x1 = layers.Dense(81)(x)
x1 = BoxCox()(x1)
x1 = layers.Dropout(0.25)(x1)

stats库函数不接受张量存在问题。例如,错误消息是

NotImplementedError: Cannot convert a symbolic Tensor (activation_12/IdentityN:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

有什么方法可以在神经网络中运行这样的功能作为层?

非常感谢您的帮助。谢谢!

标签: pythontensorflowkerasneural-networkstatistics

解决方案


层必须支持梯度的反向传播。只有 tensorflow 函数支持它。您不能使用其他功能。


推荐阅读