python - 如何在感知器的批量训练期间计算偏差
问题描述
我正在对单节点感知器实施批量训练,但不知道如何更新我的偏差。
我正在更新重量如下:
对于每个批次,我在单个批次中运行以下临时重量更新
# 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])
然后一个完整的批次完成了我更新了我班上的主要重量
# update main weights (self.weights) where len(x) is the number of samples
self.weights = self.weights + (weight_update / len(x))
解决方案
我假设 (y[i] - o1)*x[i]) 是损失函数 wrt 权重的偏导数,我不确定你使用的是什么,但假设你使用了 -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
现在这条线,
weight_update = weight_update + (self.weights + self.learning_rate * (y[i] - o1)*x[i])
此时无需添加权重,只需添加渐变即可
weight_update += self.learning_rate * dLdw
# similarily
bias_update += self.learning_rate * dLdb
当一批完成后,就做
# 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
这是我几天前写的(MNIST 示例的 1 个隐藏分层网络),您可能会发现这很有用
推荐阅读
- excel - VBA:高级过滤器:如何按列表中的 NON FOUND 值过滤?
- google-chrome - Chrome 自动填充/搜索建议会泄露搜索信息
- solr - 在 solr 7.7 中使用特殊字符进行 Solr 排序
- c++ - 双向链表 C++。插入功能不起作用
- sql - 如何将列从 varchar 更改为几何
- java - 无法在控制器中调试@ExceptionHandler(ConstraintViolationException.class) - Spring boot
- python-3.x - 尝试更新数据框中的单元格时出现“IndexError:iloc 无法放大其目标对象”
- oauth - OAuth1 PALINTEXT:惊喜!只有 oauth_token 是强制性的吗?
- javascript - React 原生调用回调函数
- javascript - adsense 页面加载速度显着下降?