Keras 如何优化没有激活的层的权重?

How does Keras optimize weights on layers which have no activation?

背景:

如果我没记错的话,在训练网络时我们前馈对每一层执行 sigmoid(sum(W*x)) 然后在反向传播中我们计算误差和增量(变化)然后我们计算梯度并更新权重。

假设我们在其中一层上没有激活,keras 如何计算梯度?是否仅使用 sum(W*x)*next_layer_delta*weights 的值来获取当前层的增量并使用它来计算梯度?

代码:

我写了这段代码来创建 word2vec 模型(skip-gram):

model = Sequential()
model.add(Dense(2, input_dim=len(tokens_enc)))#what does it mean for it not to have an activation here? This makes it linear because there is no non-linear function such as tanh!
model.add(Dense(len(tokens_enc), activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])
# Fit the model
model.fit(X, y, epochs=20000)

输入输出都是1个热向量。

问题:keras如何在这种情况下优化权重以及在hidden[=33=中没有激活函数的含义是什么? ] 层?

通常情况下,线性激活函数仅应用于某些回归问题的最后一层。当然,你仍然可以将它用作多层网络中的隐藏层。但是,如果您将多个线性层堆叠在一起,它将充当一个线性层,因此您无法使用它构建大型模型。线性激活函数的局部梯度=1,因此,一个完整节点的局部梯度就是权重本身。

Keras 使用了 Theano 和 TensorFlow 的自动微分功能(取决于你使用的后端),所以 Keras 没有做任何关于没有激活函数的特殊事情。

梯度由 Theano/TensorFlow 计算,他们计算出正确的梯度。