Python 自定义keras层升高:ValueError:渐变操作具有“无”

Python 自定义keras层升高:ValueError:渐变操作具有“无”,python,tensorflow,keras,Python,Tensorflow,Keras,因此,我尝试创建自定义图层,如下所示: from keras.layers import Layer, Multiply, Add, Dot, add, Dense import keras.backend as K import numpy as np from itertools import product from keras.activations import relu from time import time import tensorflow as tf from keras

因此,我尝试创建自定义图层,如下所示:

from keras.layers import Layer, Multiply, Add, Dot, add, Dense
import keras.backend as K
import numpy as np
from itertools import product
from keras.activations import relu
from time import time
import tensorflow as tf
from keras import initializers

class NoisyLayer(Dense):
    def __init__(self, units, **kwargs):
        self.units = units 
        super(NoisyLayer, self).__init__(units,**kwargs)

    def build(self, input_shape):
        self.state_shape = input_shape
        self.weight_mu = self.add_weight(name='weight_mu', 
                                      shape=(input_shape[1], self.units),
                                      initializer=self.kernel_initializer,
                                      trainable=True)
        self.weight_sigma = self.add_weight(name='weight_sigma', 
                                      shape=(input_shape[1], self.units),
                                      initializer=initializers.Constant(0.017),
                                      trainable=True)
        self.bias_mu = self.add_weight(name='bias_mu', 
                                      shape=(self.units,),
                                      initializer=self.bias_initializer,
                                      trainable=True)
        self.bias_sigma = self.add_weight(name='bias_mu', 
                                      shape=(self.units,),
                                      initializer=initializers.Constant(0.017),
                                      trainable=True)

        super(NoisyLayer, self).build(input_shape)

    def call(self, input_tensor):

        WeightedInp = K.dot(input_tensor, 
                            self.weight_mu + self.weight_sigma)
        bias = self.bias_mu + self.bias_sigma
        return relu(K.bias_add(WeightedInp, bias))

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.units)
我用这个简单的脚本来测试它:

from  NoisyLayer import NoisyLayer
from keras.models import Model
from keras.layers import Input
import numpy as np
from keras.optimizers import Adam

inp = Input(shape=[4])
out = NoisyLayer(units=2)(inp)

model = Model(inp, out)
model.compile(Adam(), loss='mse')

a = np.random.rand(4)
f = np.random.rand(2)

a = np.expand_dims(a, 0)
f = np.expand_dims(f, 0)

for step in range(10000):
     print("Alrighty: ", step)
     _ = model.fit(a,f)
其中提出:

ValueError: An operation has `None` for gradient. Please make sure that all of your ops have a gradient defined (i.e. are differentiable). Common ops without gradient: K.argmax, K.round, K.eval.
老年退休金计划都是简单而直接的,所以他们没有梯度是没有意义的。我发现的另一件事是,如果我没有使用所有定义的权重,这个错误也会增加,但它也没有意义,因为我使用了


那么错误是什么呢?

好的,答案如下: 如前所述,我正在通过调用以下命令来破坏我的构建:

super(NoisyLayer, self).build(input_shape)

在呼叫功能中

确定答案如下: 如前所述,我正在通过调用以下命令来破坏我的构建:

super(NoisyLayer, self).build(input_shape)
调用函数内部