Tensorflow 2:如何在 keras 功能 API 中使用密集层堆栈?

阿迪亚乌拉

我正在构建一个模型,我想在其中提供 no_of_dense_layers 作为参数,并期望函数在循环中创建密集层。

在循环中创建密集层不是问题,我的问题是如何在 Keras 的密集层堆栈中传递值?

假设我想要 3 个密集层:

def get_layers(no_of_dense_layers  , dense_size):
    return [tf.keras.layers.Dense(dense_size[i], activation = 'elu', 
                                      kernel_initializer=tf.keras.initializers.glorot_uniform(seed=200)) for i in range(no_of_dense_layers)]

现在,如果我想使用 Sequential API,我可以这样做:

perceptron = tf.keras.Sequential(get_layers(3,[1000,500,300]))

但是如果我想使用函数式API,如何实现相同的功能?

像这样的东西:

input_layer = tf.keras.Input(shape=(1024), dtype='float32', name='embedding_input')

## This layer should pass input of first denselayer >> dense_layer2 >> dense_layer3
   dense_layers = get_layers(3,[1000,500,300])


# Above layer should be equal to : 
# x = tf.keras.layers.Dense(1000)
# x = tf.keras.layers.Dense(500)
# x = tf.keras.layers.Dense(300)


# Then simply pass the output of all three dense layers to classification last layer

# classification_layer 

cls_layer  = tf.keras.layers.Dense(1, activation= 'elu')(dense_layers)

我尝试过的:

first_layer = dense_layers[0](input_layer)
for k in dense_layers[1:]:
    print(k(first_layer))

有没有其他方法?

谢谢!

马可·切利亚尼

这里有一个可能性:

def get_layers(inp, no_of_dense_layers, dense_size):
    
    for i in range(no_of_dense_layers):
    
        x = Dense(dense_size[i])(inp)
        inp = x
        
    return x

inp = Input((1024,))
x = get_layers(inp, 3, [1000,500,300])    
out = Dense(1)(x)

m = Model(inp, out)
m.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_45 (InputLayer)        [(None, 1024)]            0         
_________________________________________________________________
dense_88 (Dense)             (None, 1000)              1025000   
_________________________________________________________________
dense_89 (Dense)             (None, 500)               500500    
_________________________________________________________________
dense_90 (Dense)             (None, 300)               150300    
_________________________________________________________________
dense_91 (Dense)             (None, 1)                 301       

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用tf.keras功能API时如何在Tensorflow / Keras中使用for循环?

如何在 Keras 中使用 Tensorflow 2 数据集 API?

在Keras功能API中使用嵌入层

如何将TensorFlow Dataset API与密集层结合使用

如何在Keras中使用功能性API在快捷连接中添加卷积层?

如何在Tensorflow 2.x Keras自定义层中使用多个输入?

Tensorflow Keras功能API模型可以训练Tensorflow变量吗?可以在功能性API模型中使用Tensorflow操作吗?

如何使用功能 API 在 keras 中实现 Merge 功能

在Keras中使用Tensorflow层

如何在Keras中使用密集网

使用Tensorflow Keras功能API在专家模型混合中进行短路计算

如何在一个Keras层中使用不同的激活功能?

功能性API中的Tensorflow Keras乙状结肠激活

Keras功能多输入层API

如何使用Keras功能API查看神经网络的摘要

使用Tensorflow 2的Keras Functional API时传递`training = true`

在tensorflow代码中使用keras层

如何使用 Tensorflow 功能 API 沿批量维度进行广播?

如何在功能性API中正确使用Tensorflow插件的指标?

TensorFlow Keras Sequential API GPU 使用

如何结合 Tensorflow 数据集和 Keras 预处理功能?

如何使Keras在Anaconda中使用Tensorflow后端?

如何在Keras Tensorflow 2.3中使用随机缩放

如何在R中使keras使用Python安装的Tensorflow

如何在Tensorflow-keras中使用nlp的预测?

如何在Keras中使用tensorflow.python.ops?

如何在Keras中使用Lambda层?

如何使用预训练模型的第一层来提取Keras模型中的特征(功能性API)

在keras或Tensorflow中的LSTM层之前添加密集层?