因此,我试图像这样创建自定义图层:
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,简单而直接,因此没有渐变没有意义。我发现的另一件事是,如果我未使用所有已定义的权重,也会引发此错误,但是自从我这样做以来,它也没有任何意义。
那是什么错误?
答案 0 :(得分:0)
好的,答案是从这里开始的: Keras Custom Layer ValueError: An operation has `None` for gradient. 正如我所说的,我正在通过以下方式破坏我的构建:
super(NoisyLayer, self).build(input_shape)
调用函数内部