【问题标题】:Convert from sequential to Functional API model从顺序 API 模型转换为功能 API 模型
【发布时间】:2021-01-02 11:40:56
【问题描述】:

我有以下代码sn-p:

model = Sequential()
model.add(Conv2D(16, (5, 5), input_shape=(256, 256, 1)))
x = model.layers[0].output
model.add(Lambda(lambda x: tf.abs(x)))
model.add(Activation(activation='tanh'))

我的问题,如何将这些步骤转换为功能 API Keras 模型。我困惑的想法是如何将 ABS 层插入到功能 API 模型中。

【问题讨论】:

  • 你的意思是功能 API 吗?
  • 是的,我是这个意思。

标签: python-3.x tensorflow keras conv-neural-network


【解决方案1】:

让我们看看您的模型与顺序实现和功能 API 实现:

以下是一些导入:

import tensorflow as tf
from tensorflow.keras.layers import Lambda,Conv2D, Activation, Input
from tensorflow.keras import Model, Sequential

这是您使用顺序模型的实现:

model = Sequential()
model.add(Conv2D(16, (5, 5), input_shape=(256, 256, 1)))
x = model.layers[0].output
model.add(Lambda(lambda x: tf.abs(x)))
model.add(Activation(activation='tanh'))

model.summary()

总结输出:

Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_6 (Conv2D)            (None, 252, 252, 16)      416       
_________________________________________________________________
lambda_6 (Lambda)            (None, 252, 252, 16)      0         
_________________________________________________________________
activation_5 (Activation)    (None, 252, 252, 16)      0         
=================================================================
Total params: 416
Trainable params: 416
Non-trainable params: 0
_________________________________________________________________

现在使用功能 API 实现:

首先,定义你的函数:

def arbitrary_functionality(tensor):

  return tf.abs(tensor)

还有:

input_layer = Input(shape=(256, 256, 1))
conv1 = Conv2D(16, (5, 5))(input_layer)
lambda_layer = Lambda(arbitrary_functionality)(conv1)
output_layer = Activation(activation='tanh')(lambda_layer)

model_2 = Model(inputs=input_layer, outputs=output_layer)
model_2 .summary()

总结输出:

Model: "model_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_7 (InputLayer)         [(None, 256, 256, 1)]     0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 252, 252, 16)      416       
_________________________________________________________________
lambda_9 (Lambda)            (None, 252, 252, 16)      0         
_________________________________________________________________
activation_8 (Activation)    (None, 252, 252, 16)      0         
=================================================================
Total params: 416
Trainable params: 416
Non-trainable params: 0
_________________________________________________________________

注意:根据 TensorFlow 文档,更好的方法是继承 Layer 类。查看示例here

【讨论】:

    猜你喜欢
    • 2020-07-22
    • 2017-10-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    • 2018-05-23
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多