Keras澄清了隐藏层的定义 [英] Keras clarification on definition of hidden layer

查看:68
本文介绍了Keras澄清了隐藏层的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在跟踪有关在Keras中构建简单的深度神经网络的教程,并且提供的代码是:

I am following a tutorial on building a simple deep neural network in Keras, and the code provided was:

# create model
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

第一行model.add是不是定义了第一个隐藏层,在输入层中有8个输入?因此,除了代码input_dim=8之外,是否需要指定输入层?

Is the first model.add line to define the first hidden layer, with 8 inputs in the input layer? Is there thus no need to specify the input layer except for the code input_dim=8?

推荐答案

您是对的.

创建Sequential模型时,输入"layer" *input_diminput_shapebatch_input_shape定义.

When you're creating a Sequential model, the input "layer"* is defined by input_dim or by input_shape, or by batch_input_shape.

*-输入层实际上不是一个层,而只是一个用于以特定格式接收数据的容器".

* - The input layer is not really a layer, but just a "container" for receiving data in a specific format.

稍后,您可能会发现使用功能性API模型而不是顺序模型非常有用.在这种情况下,您将使用以下命令定义输入张量:

Later you might find it very useful to use functional API models instead of sequential models. In that case, then you will define the input tensor with:

inputs = Input((8,))

然后将此张量传递给各层:

And pass this tensor through the layers:

outputs = Dense(12, input_dim=8, activation='relu')(inputs)
outputs = Dense(8, activation='relu')(outputs)
outputs = Dense(1, activation='sigmoid')(outputs)

要创建模型,请执行以下操作:

To create the model:

model = Model(inputs,outputs)

一开始似乎麻烦太多,但是很快您就会感到需要创建分支,联接模型,拆分模型等.

It seems too much trouble at first, but soon you will feel the need to create branches, join models, split models, etc.

这篇关于Keras澄清了隐藏层的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆