簡體   English   中英

自定義 keras 層引發:ValueError:漸變操作具有“無”

[英]Custom keras layer raises : ValueError: An operation has `None` for gradient

所以我正在嘗試創建我的自定義圖層:

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,所以它們沒有漸變是沒有意義的。 我發現的另一件事是,如果我沒有使用所有定義的權重,這個錯誤也會引發,但它也沒有意義,因為我這樣做了。

那么錯誤是什么?

好的,答案來自這里: Keras Custom Layer ValueError: An operation has `None` for gradient。 如前所述,我正在通過調用來破壞我的構建:

super(NoisyLayer, self).build(input_shape)

調用函數內部

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM