Python keras自定义层中的0个训练参数

Python keras自定义层中的0个训练参数,python,tensorflow,deep-learning,keras,layer,Python,Tensorflow,Deep Learning,Keras,Layer,最近我从tensorflow切换到keras,我需要创建一个自定义层 我对类的定义如下: class Apply_conv2d(Layer): def __init__(self, **kwargs): super(Apply_conv2d, self).__init__(**kwargs) def build(self, input_shape): super(Apply_conv2d, self).build(input_shape) # Be sure to call

最近我从tensorflow切换到keras,我需要创建一个自定义层

我对类的定义如下:

class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    super(Apply_conv2d, self).__init__(**kwargs)

def build(self, input_shape):
    super(Apply_conv2d, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x):
    res = Conv2D(32, (1, 1), padding='same')(x)
    self.shape = res.shape
    res = k.reshape(res, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return res

def compute_output_shape(self, input_shape):
    return (None, input_shape[3])

但是当我打印
model.summary()
时,我在使用该层时得到了0个可训练的参数

这种实现有什么问题? 非常感谢。
编辑
我将类定义更改为:

class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    self.trainable = True
    super(Apply_conv2d, self).__init__(**kwargs)

def build(self, input_shape):
    w = self.add_weight(name='kernel', shape=(1, 1, 2048, 32), initializer='uniform', trainable=True)
    b = self.add_weight(name='kernel', shape=(32,), initializer='uniform', trainable=True)
    self.kernel = [w, b]
    super(Apply_conv2d, self).build(input_shape)  # Be sure to call this somewhere!

def call(self, x):
    res = Conv2D(32, (1, 1), padding='same', name='feature_conv', weights=self.kernel)(x)
    self.shape = res.shape
    res = k.reshape(res, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return res

def compute_output_shape(self, input_shape):
    return (None, input_shape[3])
但这仍然不起作用…
错误是:

    Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 
    2017.2.3\helpers\pydev\pydevd.py", line 1668, in <module>
       main()
     File "C:\Program Files\JetBrains\PyCharm Community Edition 
   2017.2.3\helpers\pydev\pydevd.py", line 1662, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py", line 1072, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D/script.py", line 123, in <module>
    model = cl.get_model(inputs)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py", line 77, in get_model
    x3 = Apply_conv2d()(x)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 603, in __call__
    output = self.call(inputs, **kwargs)
  File "C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py", line 104, in call
    res = Conv2D(32, (1, 1), padding='same', name='feature_conv', weights=self.kernel)(x)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 583, in __call__
    self.set_weights(self._initial_weights)
  File "C:\Program Files\Python35\lib\site-packages\keras\engine\topology.py", line 1203, in set_weights
    K.batch_set_value(weight_value_tuples)
  File "C:\Program Files\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 2239, in batch_set_value
    value = np.asarray(value, dtype=dtype(x))
  File "C:\Program Files\Python35\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
    return array(a, dtype, copy=False, order=order)
ValueError: setting an array element with a sequence.
回溯(最近一次呼叫最后一次):
文件“C:\Program Files\JetBrains\PyCharm社区版
2017.2.3\helpers\pydev\pydevd.py“,第1668行,在
main()
文件“C:\Program Files\JetBrains\PyCharm社区版
2017.2.3\helpers\pydev\pydevd.py“,第1662行,主视图
globals=debugger.run(setup['file'],None,None,is_模块)
文件“C:\Program Files\JetBrains\PyCharm Community Edition 2017.2.3\helpers\pydev\pydevd.py”,第1072行,正在运行
pydev_imports.execfile(文件、全局、局部)#执行脚本
文件“C:\Program Files\JetBrains\PyCharm社区版2017.2.3\helpers\pydev\\u pydev\u imps\\u pydev\u execfile.py”,execfile中第18行
exec(编译(内容+“\n”,文件,'exec'),全局,loc)
文件“C:/Users/Reza/Dropbox/Reza/VOC2012-D/script.py”,第123行,在
模型=cl.get_模型(输入)
get_模型中的文件“C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py”,第77行
x3=应用(x)
文件“C:\Program Files\Python35\lib\site packages\keras\engine\topology.py”,第603行,在调用中__
输出=自调用(输入,**kwargs)
文件“C:/Users/Reza/Dropbox/Reza/VOC2012-D\custom_layers.py”,第104行,在调用中
res=Conv2D(32,(1,1),padding='same',name='feature\u conv',weights=self.kernel)(x)
文件“C:\Program Files\Python35\lib\site packages\keras\engine\topology.py”,第583行,在调用中__
自设置权重(自初始权重)
文件“C:\Program Files\Python35\lib\site packages\keras\engine\topology.py”,第1203行,在集合中
K.批量设置值(权重值元组)
文件“C:\Program Files\Python35\lib\site packages\keras\backend\tensorflow\u backend.py”,第2239行,在批处理设置值中
value=np.asarray(value,dtype=dtype(x))
文件“C:\Program Files\Python35\lib\site packages\numpy\core\numeric.py”,第531行,在asarray中
返回数组(a,数据类型,copy=False,order=order)
ValueError:使用序列设置数组元素。


有什么建议吗?

经过大量研究和尝试各种方法,我终于找到了解决方案。
我应该使用来自keras的原始conv操作,因此实现应该是这样的:

class Apply_conv2d(Layer):
def __init__(self, **kwargs):
    super(Apply_conv2d, self).__init__(**kwargs)
    self.trainable = True

def build(self, input_shape):
    self.kernel = self.add_weight(name='kernel', shape=(1, 1, 2048, 32), initializer='uniform', trainable=True)
    self.bias = self.add_weight(name='bias', shape=(32,), initializer='uniform', trainable=True)

def call(self, inputs, **kwargs):
    outputs = k.conv2d(inputs, self.kernel)
    outputs = k.bias_add(outputs, self.bias)
    self.shape = outputs.shape
    outputs = k.reshape(outputs, [-1, self.shape[1] * self.shape[2] * self.shape[3]])
    return outputs

def compute_output_shape(self, input_shape):
    return (input_shape[0], input_shape[3])

您缺少一个步骤,需要在
build()
期间创建可训练重量数组。但是,由于您的自定义层看起来像一个执行
Conv2D->flant()
的层?您可能需要将权重数组设置为
res。获取\u weights()
。顺便说一下,每次调用该层时,都会初始化一个新的Conv2D层。不确定这是不是故意的,也许你想把它移到建筑上去?@umutto我已经照你说的做了,但还是不起作用