[英]How to calculate bias during batch training of a Perceptron
I am in the process of implementing batch training on a single node perceptron but cant figure out how to update my bias.我正在对单节点感知器实施批量训练,但不知道如何更新我的偏差。
I am updating the weight as follow:我正在更新重量如下:
For each batch I am running the following temporary weight update during a single batch对于每个批次,我在单个批次中运行以下临时重量更新
# update weight_update where y[i] is the actual label and o1 is the predicted output and x[i] is the input
weight_update = weight_update + (self.weights + self.learning_rate * (y[i] - o1)*x[i])
then one a complete batch is completed I update the main weight in my class然后一个完整的批次完成我更新我的 class 中的主要重量
# update main weights (self.weights) where len(x) is the number of samples
self.weights = self.weights + (weight_update / len(x))
I assume (y[i] - o1)*x[i]) is the partial derivative of loss function wrt weight, I am not sure what you used, but just assume you used -1/2 * (y[i] - o1)^2我假设 (y[i] - o1)*x[i]) 是损失 function 重量的偏导数,我不确定你使用了什么,但假设你使用了 -1/2 * (y[i] - o1)^2
now let o1 = wx + b, where w is weight matrix and b is bias vector,
also let, L = -1/2 * (y[i] - o1)^2
you have already calculated dLdw = dLd(o1) * d(o1)dw = (y[i] - o1) * x
In a summilar way calculate dLdb,
dLdb = dLd(o1) * d(o1)db
dLd(o1) = (y[i] - o1)
d(o1)db = d/db (wx + b) = 0 + 1 = 1
so dLdb = (y[i] - o1) * 1
Now this line,现在这条线,
weight_update = weight_update + (self.weights + self.learning_rate * (y[i] - o1)*x[i])
There's no need to add the weights at this point, just add the gradients此时无需添加权重,只需添加渐变即可
weight_update += self.learning_rate * dLdw
# similarily
bias_update += self.learning_rate * dLdb
When one batch is completed, just do当一批完成后,就做
# update main weights (self.weights) and biases (self.biases)
# where len(x) is the number of samples
self.weights += (weight_update / len(x))
self.biases+= (bias_update / len(x))
# dont forget to set the values of weight_update, bias_update to 0
This is something I wrote a few days back (1 hidden layered network for the MNIST example) you might find this useful 这是我几天前写的(MNIST 示例的 1 个隐藏分层网络),您可能会发现这很有用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.