"ValueError: Unknown layer: ... " when calling copy.deepcopy(network) using Tensorflow

"ValueError: Unknown layer: ... " when calling copy.deepcopy(network) using Tensorflow

我目前正在 Tensorflow 中设计一个 NoisyNet,我需要为其定义一个自定义层。复制包含该自定义层的模型时,python 会引发错误 ValueError: Unknown layer: NoisyLayer。提供层的实现 .

目标是复制一个网络创建它的第二个实例。为此,我使用命令 net_copy = copy.deepcopy(net_original),只要我不在要复制的模型中包含上面提到的自定义层,它就可以工作。 我看到为了保存和加载,存在一种指定自定义属性(例如自定义图层)的方法,但我找不到适用于 copy.deepcopy() 的类似命令,其中副本是通过 [=16] 导入的=].

我在 Python3 中使用 Tensorflow 1.12.0。

同样,自定义图层在上面的link下提供。 使用自定义层的网络如下所示:

class Network:
    def __init__(self, actionspace_size, learning_rate, gradient_momentum, gradient_min):
        frames_input = keras.layers.Input((84, 84, 4))
        actions_input = keras.layers.Input((actionspace_size,))

        conv1 = keras.layers.Conv2D(16, (8, 8), strides=(4, 4), activation="relu")(frames_input)
        conv2 = keras.layers.Conv2D(32, (4, 4), strides=(2, 2), activation="relu")(conv1)

        flattened = keras.layers.Flatten()(conv2)

        # NoisyNet        
        hidden = NoisyLayer(activation=tf.nn.relu)(inputs=flattened, resample_noise_flag=True)
        output = NoisyLayer(in_shape=(1,256), out_units=actionspace_size)(inputs=hidden, resample_noise_flag=True)

        filtered_output = keras.layers.merge.Multiply()([output, actions_input])

        self.model = keras.models.Model(inputs=[frames_input, actions_input], outputs=filtered_output)

        self.model.compile(loss='mse', optimizer=keras.optimizers.RMSprop(lr=learning_rate, rho=gradient_momentum, epsilon=gradient_min))

调用时

q_net = Network(actionspace_size, learning_rate, gradient_momentum, gradient_min).
target_net = copy.deepcopy(q_net)

出现以下错误:

Traceback (most recent call last):
  File "DQN_tf_NoisyNet.py", line 315, in <module>
    main()
  File "DQN_tf_NoisyNet.py", line 252, in main
    target_net = copy.deepcopy(q_net)
  File "/usr/lib/python3.5/copy.py", line 182, in deepcopy
    y = _reconstruct(x, rv, 1, memo)
  File "/usr/lib/python3.5/copy.py", line 299, in _reconstruct
    y.__setstate__(state)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1266, in __setstate__
    model = saving.unpickle_model(state)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 435, in unpickle_model
    return _deserialize_model(f)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 225, in _deserialize_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/saving.py", line 458, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 145, in deserialize_keras_object
    list(custom_objects.items())))
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1022, in from_config
    process_layer(layer_data)
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1008, in process_layer
    custom_objects=custom_objects)
  File "/usr/local/lib/python3.5/dist-packages/keras/layers/__init__.py", line 55, in deserialize
    printable_module_name='layer')
  File "/usr/local/lib/python3.5/dist-packages/keras/utils/generic_utils.py", line 138, in deserialize_keras_object
    ': ' + class_name)
ValueError: Unknown layer: NoisyLayer

我知道网络本身不是问题(深度复制方法也不是),因为一旦我用标准密集层替换 NoisyLayers(自定义),两者都可以正常工作。

有谁知道如何复制包含自定义层的 Tensorflow 模型?提前致谢!

找到解决方案:

同样,问题是 Tensorflow/Keras 不知道如何解释自定义图层。因此,要提供如何解释层的信息,可以使用 Keras 的 CustomObjectScope 并在该范围内复制模型,如下所示:

# Import
import copy
from keras.utils import CustomObjectScope

# Copy
with CustomObjectScope({"MyCustomLayer":MyCustomLayer}):
        model_copy = copy.deepcopy(model)

这会处理复制部分。但是,只要没有将自定义输入指定为自定义层构造函数的参数 (__init(...)),这只会开箱即用。

我猜这是因为在幕后 copy() 函数似乎暂时保存然后使用一些 pickle 功能再次加载原始模型,这样就必须声明值进一步 constructor-parameters 以及以下内容:

如果自定义 class 的开头如下所示,其中 output_dim 是上面提到的自定义参数之一:

class MyCustomLayer(keras.layers.Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(MyCustomLayer, self).__init__(**kwargs)

然后必须向 class MyCustomLayer 添加一个函数,该函数还负责使自定义构造函数参数持久保存和加载(复制时):

def get_config(self):
        config = super(MyCustomLayer, self).get_config()

        # Specify here all the values for the constructor's parameters
        config['output_dim'] = self.output_dim

        return config

这两个步骤解决了我的问题。