【发布时间】:2018-08-22 01:51:55
【问题描述】:
我在尝试通过 Keras 功能 API 使用激活时遇到问题。我最初的目标是在 relu 和leaky relu 之间进行选择,所以我想出了以下代码:
def activation(x, activation_type):
if activation_type == 'leaky_relu':
return activations.relu(x, alpha=0.3)
else:
return activations.get(activation_type)(x)
# building the model
inputs = keras.Input(input_shape, dtype='float32')
x = Conv2D(filters, (3, 3), padding='same')(inputs)
x = activation(x, 'relu')
但是这样的事情会产生错误:AttributeError: 'Tensor' object has no attribute '_keras_history'。我发现这可能表明我在Model中的输入和输出没有连接。
keras.advanced_activations 是在函数式 API 中实现此类功能的唯一方法吗?
编辑:这是有效的激活功能版本:
def activation(self, x):
if self.activation_type == 'leaky_relu':
act = lambda x: activations.relu(x, alpha=0.3)
else:
act = activations.get(self.activation_type)
return layers.Activation(act)(x)
【问题讨论】:
标签: keras