python - 如何自定义 Keras 图层名称并使其自动递增 layer.name

标签 python tensorflow keras activation-function

我目前正在尝试使用名为 cust_sig 的自定义激活来创建多层。但是当我尝试编译模型时,我得到了一个 ValueError,因为多个层具有相同的名称 cust_sig。我知道我可以手动更改每一层的名称,但想知道是否可以做一些事情来自动将 _1, _2, ... 添加到名称中,就像它为内置图层。可以在下面找到模型定义。

# Creating a model
from tensorflow.python.keras import keras
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.layers import Dense

# Custom activation function
from tensorflow.python.keras.layers import Activation
from tensorflow.python.keras import backend as K
from keras.utils.generic_utils import get_custom_objects

def custom_activation(x):
    return (K.sigmoid(x) * 5) - 1

get_custom_objects().update({'custom_activation': Activation(custom_activation)})

data_format = 'channels_first'

spec_input = keras.layers.Input(shape=(1, 3, 256), name='spec')
x = keras.layers.Flatten(data_format)(spec_input)

for layer in range(3):
  x = Dense(512)(x)
  x = Activation('custom_activation', name='cust_sig')(x)

out = Dense(256, activation="sigmoid", name='out')(x)
model = Model(inputs=spec_input, outputs=out)

错误信息如下所示

Traceback (most recent call last):
  File "/home/xyz/anaconda3/envs/ctf/lib/python3.7/site-packages/tensorflow/python/training/tracking/base.py", line 457, in _method_wrapper
    result = method(self, *args, **kwargs)
  File "/home/xyz/anaconda3/envs/ctf/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 315, in _init_graph_network
    self.inputs, self.outputs)
  File "/home/xyz/anaconda3/envs/ctf/lib/python3.7/site-packages/tensorflow/python/keras/engine/network.py", line 1861, in _map_graph_network
    str(all_names.count(name)) + ' times in the model. '
ValueError: The name "cust_sig" is used 3 times in the model. All layer names should be unique.

最佳答案

下面应该做的:

def custom_activation(x):
    return (K.sigmoid(x) * 5) - 1

class CustSig(Layer):
    def __init__(self, my_activation, **kwargs):
        super(CustSig, self).__init__(**kwargs)
        self.supports_masking = True
        self.activation = my_activation

    def call(self, inputs):
        return self.activation(inputs)

    def get_config(self):
        config = {'activation': activations.serialize(self.activation)}
        base_config = super(Activation, self).get_config()
        return dict(list(base_config.items()) + list(config.items()))

    def compute_output_shape(self, input_shape):
        return input_shape


解释:

来自 source code , 自动命名的工作原理如下:

if not name:
  self._name = backend.unique_object_name(
      generic_utils.to_snake_case(self.__class__.__name__),
      zero_based=zero_based)
else:
  self._name = name

检查 Keras 图是否存在与您正在定义的对象同名的现有对象 - 如果存在,则继续递增 1,直到没有匹配项。要注意的是,您不能指定 name=,因为这会根据上述条件消除自动命名。

唯一的解决方法可能是定义您自己的自定义激活层,使用所需的名称作为类名,如上所示,其表现如下:

ipt = Input(shape=(1, 3, 256), name='spec')
x   = Flatten('channels_last')(ipt)
for _ in range(3):
    x   = Dense(512)(x)
    x   = CustSig(custom_activation)(x)
out = Dense(256, activation='sigmoid', name='out')(x)

model = Model(ipt, out)

print(model.layers[3].name)
print(model.layers[5].name)
print(model.layers[7].name)
cust_sig
cust_sig_1
cust_sig_2

关于python - 如何自定义 Keras 图层名称并使其自动递增 layer.name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58349239/

相关文章:

java - Python XML + Java XML 互操作性

python - tensorflow 中的字符串连接

python - 强化学习中的负奖励

python - 如何在 Tensorflow 中创建自定义图像数据集

javascript - Flask 服务器发送事件套接字异常

python - 如何将另一条路径添加到 AWS SageMaker 上的 Python 路径

python - Scikit 学习谱聚类获取每个聚类的项目

python - 使用 TensorArray 的梯度错误

python - 使用 Keras 实现神经网络

python - fit_generator 的训练精度为 0