python - 0 keras自定义层中的训练参数

标签 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 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.


有什么建议吗?

最佳答案

经过大量研究并尝试各种方法终于找到了解决方案。
我应该使用 keras 的原始转换操作,所以实现应该是这样的:

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])

关于python - 0 keras自定义层中的训练参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48617167/

相关文章:

machine-learning - 为什么VGG16网络中的卷积层是64层?又是如何确定的呢?

python - 将 4 channel RGB-D 图像输入 LSTM

python - 基于 Tkinter 的最小化程序中的热键

tensorflow - 为什么我们需要使用 feed_dict 传递值来打印 TensorFlow 中的损失值

python - 使用 zeep/python 创建 XML 序列

python - 使用 Tensorflow 的 tf.data API 为训练和验证设置不同的批量大小

python - tensorflow 负载模型错误

python - 使用 Tensorflow 收敛 LSTM 网络

python - 如何将 Python 版本添加到 Python 启动器?

python - "E1101"- "Class"的实例没有 "method"成员