gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:11:28 26 4
gpt4 key购买 nike

背景:

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

假设我们在其中一层没有激活,keras 如何计算梯度?它只是取 sum(W*x)*next_layer_delta*weights 的值吗?获取当前层的增量并用它来计算梯度?<​​/p>

代码:

我编写了这段代码来创建 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 如何优化权重?在隐藏层中没有激活函数会产生什么影响?

最佳答案

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

关于python - Keras 如何优化没有激活的层上的权重?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51623082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com