自定义keras层引发:ValueError:操作对gradien具有“None”

2024-09-28 21:02:42 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试创建自定义层:

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.

操作都是简单的,直接从keras,所以这是没有意义的,他们没有梯度。我发现的另一件事是,如果我没有使用所有定义的权重,这个错误也会增加,但它也没有意义,因为我这样做了

那么错误是什么呢


Tags: fromimportselfaddinputnpsigmakeras