我目前正在尝试使用名称为自定义激活的多个图层cust_sig。但是当我尝试编译模型时,我得到一个 ValueError ,因为多个层具有相同的 name 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.
哔哔one
ITMISS
红颜莎娜
相关分类