gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:09:29 25 4
gpt4 key购买 nike

最近我从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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com