NN 输入层中的单元数量可以与数据中的特征数量不同吗?

Ahm*_*vcı 2 machine-learning neural-network keras

基于tensorflow keras API教程;

model = keras.Sequential([
  keras.layers.Dense(10, activation='softmax', input_shape=(32,)),
  keras.layers.Dense(10, activation='softmax')
])
Run Code Online (Sandbox Code Playgroud)

我不明白为什么输入层的单元数是 10,而输入形状是 32。另外,tensorflow 教程中有很多这样的例子。

des*_*aut 5

这是新从业者相当常见的困惑,而且并非没有原因:正如评论中已经暗示的那样,答案是,在 Keras Sequential API 中存在一个隐式输入层,由input_shape第一个显式层。

这在 Keras 功能 API 中直接可见(查看文档中的示例Input),其中本身是一个显式层,并且您的模型将编写为:

inputs = Input(shape=(32,))                     # input layer
x = Dense(10, activation='softmax')(inputs)     # hidden layer
outputs = Dense(10, activation='softmax')(x)    # output layer

model = Model(inputs, outputs)
Run Code Online (Sandbox Code Playgroud)

也就是说,您的模型实际上是一个具有三层(输入、隐藏和输出)的“古老”神经网络的示例,尽管它看起来像 Keras Sequential API 中的两层网络。

softmax(顺便说一句,与问题无关,对隐藏层进行激活没有多大意义。)