当前位置: 首页 > news >正文

建个简单网站英语seo什么意思

建个简单网站,英语seo什么意思,做的网站怎么样才能再网上看到,网站排名如何上升计算图结构 分析: 起始节点 ab 5 - 3ac 2b 3d 5b 6e 7c d^2f 2e最终输出 g 3f - o(其中 o 是另一个输入) 前向传播 前向传播按照上述顺序计算每个节点的值。 反向传播过程 反向传播的目标是计算损失函数(这里假设为…

计算图结构

**加粗样式**

分析:

  1. 起始节点 a
  2. b = 5 - 3a
  3. c = 2b + 3
  4. d = 5b + 6
  5. e = 7c + d^2
  6. f = 2e
  7. 最终输出 g = 3f - o(其中 o 是另一个输入)

前向传播

前向传播按照上述顺序计算每个节点的值。

反向传播过程

反向传播的目标是计算损失函数(这里假设为 g)对每个中间变量和输入的偏导数。从右向左进行计算:

  1. ∂g/∂o = -1
  2. ∂g/∂f = 3
  3. ∂f/∂e = 2
  4. ∂e/∂c = 7
  5. ∂e/∂d = 2d
  6. ∂d/∂b = 5
  7. ∂c/∂b = 2
  8. ∂b/∂a = -3

链式法则应用

使用链式法则计算出 g 对每个变量的全导数:

  1. dg/df = ∂g/∂f = 3
  2. dg/de = (∂g/∂f) * (∂f/∂e) = 3 * 2 = 6
  3. dg/dc = (dg/de) * (∂e/∂c) = 6 * 7 = 42
  4. dg/dd = (dg/de) * (∂e/∂d) = 6 * 2d
  5. dg/db = (dg/dc) * (∂c/∂b) + (dg/dd) * (∂d/∂b)
    = 42 * 2 + 6 * 2d * 5
    = 84 + 60d
  6. dg/da = (dg/db) * (∂b/∂a)
    = (84 + 60d) * (-3)
    = -252 - 180d

最终梯度

最终得到 g 对输入 a 和 o 的梯度:

  • dg/da = -252 - 180d
  • dg/do = -1

代码实现

静态图

import mathclass Node:"""表示计算图中的一个节点。每个节点都可以存储一个值、梯度,并且知道如何计算前向传播和反向传播。"""def __init__(self, value=None):self.value = value  # 节点的值self.gradient = 0   # 节点的梯度self.parents = []   # 父节点列表self.forward_fn = lambda: None  # 前向传播函数self.backward_fn = lambda: None  # 反向传播函数def __add__(self, other):"""加法操作"""return self._create_binary_operation(other, lambda x, y: x + y, lambda: (1, 1))def __mul__(self, other):"""乘法操作"""return self._create_binary_operation(other, lambda x, y: x * y, lambda: (other.value, self.value))def __sub__(self, other):"""减法操作"""return self._create_binary_operation(other, lambda x, y: x - y, lambda: (1, -1))def __pow__(self, power):"""幂运算"""result = Node()result.parents = [self]def forward():result.value = math.pow(self.value, power)def backward():self.gradient += power * math.pow(self.value, power-1) * result.gradientresult.forward_fn = forwardresult.backward_fn = backwardreturn resultdef _create_binary_operation(self, other, forward_op, gradient_op):"""创建二元操作的辅助方法。用于简化加法、乘法和减法的实现。"""result = Node()result.parents = [self, other]def forward():result.value = forward_op(self.value, other.value)def backward():grads = gradient_op()self.gradient += grads[0] * result.gradientother.gradient += grads[1] * result.gradientresult.forward_fn = forwardresult.backward_fn = backwardreturn resultdef topological_sort(node):"""对计算图进行拓扑排序。确保在前向和反向传播中按正确的顺序处理节点。"""visited = set()topo_order = []def dfs(n):if n not in visited:visited.add(n)for parent in n.parents:dfs(parent)topo_order.append(n)dfs(node)return topo_order# 构建计算图
a = Node(2)  # 假设a的初始值为2
o = Node(1)  # 假设o的初始值为1# 按照给定的数学表达式构建计算图
b = Node(5) - a * Node(3)
c = b * Node(2) + Node(3)
d = b * Node(5) + Node(6)
e = c * Node(7) + d ** 2
f = e * Node(2)
g = f * Node(3) - o# 前向传播
sorted_nodes = topological_sort(g)
for node in sorted_nodes:node.forward_fn()# 反向传播
g.gradient = 1  # 设置输出节点的梯度为1
for node in reversed(sorted_nodes):node.backward_fn()# 打印结果
print(f"g = {g.value}")
print(f"dg/da = {a.gradient}")
print(f"dg/do = {o.gradient}")# 验证手动计算的结果
d_value = 5 * b.value + 6
expected_dg_da = -252 - 180 * d_value
print(f"Expected dg/da = {expected_dg_da}")
print(f"Difference: {abs(a.gradient - expected_dg_da)}")

动态图

import mathclass Node:"""表示计算图中的一个节点。实现了动态计算图的核心功能,包括前向计算和反向传播。"""def __init__(self, value, children=(), op=''):self.value = value  # 节点的值self.grad = 0       # 节点的梯度self._backward = lambda: None  # 反向传播函数,默认为空操作self._prev = set(children)  # 前驱节点集合self._op = op  # 操作符,用于调试def __add__(self, other):"""加法操作"""other = other if isinstance(other, Node) else Node(other)result = Node(self.value + other.value, (self, other), '+')def _backward():self.grad += result.gradother.grad += result.gradresult._backward = _backwardreturn resultdef __mul__(self, other):"""乘法操作"""other = other if isinstance(other, Node) else Node(other)result = Node(self.value * other.value, (self, other), '*')def _backward():self.grad += other.value * result.gradother.grad += self.value * result.gradresult._backward = _backwardreturn resultdef __pow__(self, other):"""幂运算"""assert isinstance(other, (int, float)), "only supporting int/float powers for now"result = Node(self.value ** other, (self,), f'**{other}')def _backward():self.grad += (other * self.value**(other-1)) * result.gradresult._backward = _backwardreturn resultdef __neg__(self):"""取反操作"""return self * -1def __sub__(self, other):"""减法操作"""return self + (-other)def __truediv__(self, other):"""除法操作"""return self * other**-1def __radd__(self, other):"""反向加法"""return self + otherdef __rmul__(self, other):"""反向乘法"""return self * otherdef __rtruediv__(self, other):"""反向除法"""return other * self**-1def tanh(self):"""双曲正切函数"""x = self.valuet = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)result = Node(t, (self,), 'tanh')def _backward():self.grad += (1 - t**2) * result.gradresult._backward = _backwardreturn resultdef backward(self):"""执行反向传播,计算梯度。使用拓扑排序确保正确的反向传播顺序。"""topo = []visited = set()def build_topo(v):if v not in visited:visited.add(v)for child in v._prev:build_topo(child)topo.append(v)build_topo(self)self.grad = 1  # 设置输出节点的梯度为1for node in reversed(topo):node._backward()  # 对每个节点执行反向传播def main():"""主函数,用于测试自动微分系统。构建一个计算图,执行反向传播,并验证结果。"""# 构建计算图a = Node(2)o = Node(1)b = Node(5) - a * 3c = b * 2 + 3d = b * 5 + 6e = c * 7 + d ** 2f = e * 2g = f * 3 - o# 反向传播g.backward()# 打印结果print(f"g = {g.value}")print(f"dg/da = {a.grad}")print(f"dg/do = {o.grad}")# 验证手动计算的结果d_value = 5 * b.value + 6expected_dg_da = -252 - 180 * d_valueprint(f"Expected dg/da = {expected_dg_da}")print(f"Difference: {abs(a.grad - expected_dg_da)}")if __name__ == "__main__":main()

解释:

  1. Node 类代表计算图中的一个节点,包含值、梯度、父节点以及前向和反向传播函数。
  2. 重载的数学运算符 (__add__, __mul__, __sub__, __pow__) 允许直观地构建计算图。
  3. _create_binary_operation 方法用于创建二元操作,简化了加法、乘法和减法的实现。
  4. topological_sort 函数对计算图进行拓扑排序,确保正确的计算顺序。
import mathclass Node:"""表示计算图中的一个节点。实现了动态计算图的核心功能,包括前向计算和反向传播。"""def __init__(self, value, children=(), op=''):self.value = value  # 节点的值self.grad = 0       # 节点的梯度self._backward = lambda: None  # 反向传播函数,默认为空操作self._prev = set(children)  # 前驱节点集合self._op = op  # 操作符,用于调试def __add__(self, other):"""加法操作"""other = other if isinstance(other, Node) else Node(other)result = Node(self.value + other.value, (self, other), '+')def _backward():self.grad += result.gradother.grad += result.gradresult._backward = _backwardreturn resultdef __mul__(self, other):"""乘法操作"""other = other if isinstance(other, Node) else Node(other)result = Node(self.value * other.value, (self, other), '*')def _backward():self.grad += other.value * result.gradother.grad += self.value * result.gradresult._backward = _backwardreturn resultdef __pow__(self, other):"""幂运算"""assert isinstance(other, (int, float)), "only supporting int/float powers for now"result = Node(self.value ** other, (self,), f'**{other}')def _backward():self.grad += (other * self.value**(other-1)) * result.gradresult._backward = _backwardreturn resultdef __neg__(self):"""取反操作"""return self * -1def __sub__(self, other):"""减法操作"""return self + (-other)def __truediv__(self, other):"""除法操作"""return self * other**-1def __radd__(self, other):"""反向加法"""return self + otherdef __rmul__(self, other):"""反向乘法"""return self * otherdef __rtruediv__(self, other):"""反向除法"""return other * self**-1def tanh(self):"""双曲正切函数"""x = self.valuet = (math.exp(2*x) - 1)/(math.exp(2*x) + 1)result = Node(t, (self,), 'tanh')def _backward():self.grad += (1 - t**2) * result.gradresult._backward = _backwardreturn resultdef backward(self):"""执行反向传播,计算梯度。使用拓扑排序确保正确的反向传播顺序。"""topo = []visited = set()def build_topo(v):if v not in visited:visited.add(v)for child in v._prev:build_topo(child)topo.append(v)build_topo(self)self.grad = 1  # 设置输出节点的梯度为1for node in reversed(topo):node._backward()  # 对每个节点执行反向传播def main():"""主函数,用于测试自动微分系统。构建一个计算图,执行反向传播,并验证结果。"""# 构建计算图a = Node(2)o = Node(1)b = Node(5) - a * 3c = b * 2 + 3d = b * 5 + 6e = c * 7 + d ** 2f = e * 2g = f * 3 - o# 反向传播g.backward()# 打印结果print(f"g = {g.value}")print(f"dg/da = {a.grad}")print(f"dg/do = {o.grad}")# 验证手动计算的结果d_value = 5 * b.value + 6expected_dg_da = -252 - 180 * d_valueprint(f"Expected dg/da = {expected_dg_da}")print(f"Difference: {abs(a.grad - expected_dg_da)}")if __name__ == "__main__":main()

解释:

  1. Node 类是核心,它代表计算图中的一个节点,并实现了各种数学运算。

  2. 每个数学运算(如 __add__, __mul__ 等)都创建一个新的 Node,并定义了相应的反向传播函数。

  3. backward 方法实现了反向传播算法,使用拓扑排序确保正确的计算顺序。


文章转载自:
http://shqip.pwmm.cn
http://canossa.pwmm.cn
http://tokodynamometer.pwmm.cn
http://heddle.pwmm.cn
http://cheery.pwmm.cn
http://panjabi.pwmm.cn
http://omphalos.pwmm.cn
http://liquidator.pwmm.cn
http://artmobile.pwmm.cn
http://antimask.pwmm.cn
http://soothing.pwmm.cn
http://sensationalise.pwmm.cn
http://lambda.pwmm.cn
http://antidraft.pwmm.cn
http://backsword.pwmm.cn
http://ymir.pwmm.cn
http://doven.pwmm.cn
http://dependably.pwmm.cn
http://dishoard.pwmm.cn
http://quiddle.pwmm.cn
http://garri.pwmm.cn
http://bionomy.pwmm.cn
http://hummocky.pwmm.cn
http://chorea.pwmm.cn
http://circumfluent.pwmm.cn
http://gleamy.pwmm.cn
http://fishiness.pwmm.cn
http://talnakhite.pwmm.cn
http://diatom.pwmm.cn
http://lingonberry.pwmm.cn
http://sur.pwmm.cn
http://maritagium.pwmm.cn
http://hangzhou.pwmm.cn
http://nunciature.pwmm.cn
http://spearfisherman.pwmm.cn
http://anagoge.pwmm.cn
http://sining.pwmm.cn
http://theravadin.pwmm.cn
http://fenceless.pwmm.cn
http://inmate.pwmm.cn
http://triamcinolone.pwmm.cn
http://eagerness.pwmm.cn
http://propulsion.pwmm.cn
http://nemo.pwmm.cn
http://northwesternmost.pwmm.cn
http://king.pwmm.cn
http://glucan.pwmm.cn
http://terraneous.pwmm.cn
http://electromotion.pwmm.cn
http://discernment.pwmm.cn
http://malefaction.pwmm.cn
http://halve.pwmm.cn
http://sparid.pwmm.cn
http://kerne.pwmm.cn
http://unaverage.pwmm.cn
http://attainture.pwmm.cn
http://sigillum.pwmm.cn
http://ubiquity.pwmm.cn
http://preservatory.pwmm.cn
http://grandpa.pwmm.cn
http://cameroun.pwmm.cn
http://mandragora.pwmm.cn
http://ethoxyl.pwmm.cn
http://sherris.pwmm.cn
http://homilist.pwmm.cn
http://recheck.pwmm.cn
http://transformation.pwmm.cn
http://sociopathic.pwmm.cn
http://imho.pwmm.cn
http://grope.pwmm.cn
http://dactylogram.pwmm.cn
http://lockstitch.pwmm.cn
http://sciatic.pwmm.cn
http://conjee.pwmm.cn
http://vigour.pwmm.cn
http://primula.pwmm.cn
http://paragraphic.pwmm.cn
http://moth.pwmm.cn
http://keeno.pwmm.cn
http://bachelorette.pwmm.cn
http://haversine.pwmm.cn
http://oracy.pwmm.cn
http://cardiant.pwmm.cn
http://concessioner.pwmm.cn
http://fmcs.pwmm.cn
http://nostril.pwmm.cn
http://unwit.pwmm.cn
http://jolliness.pwmm.cn
http://paroecious.pwmm.cn
http://creation.pwmm.cn
http://sumph.pwmm.cn
http://vignette.pwmm.cn
http://shamal.pwmm.cn
http://photosensitive.pwmm.cn
http://combative.pwmm.cn
http://hockshop.pwmm.cn
http://somatogamy.pwmm.cn
http://breathy.pwmm.cn
http://uniflow.pwmm.cn
http://nudibranch.pwmm.cn
http://www.dt0577.cn/news/91543.html

相关文章:

  • 凡科建站源码刷网站seo排名软件
  • h5 技术做健康类网站杭州专业seo服务公司
  • 有梦商城公司网站杭州网站推广平台
  • 广东省建设项目安全标准自评网站优化大师下载安装
  • 潍坊网站制作保定公司搜索引擎优化到底是优化什么
  • wordpress网站数据库崩溃找回今日头条
  • 富阳做网站公司今日广州新闻头条
  • 做网站webform mvc友情链接买卖
  • 网站开发中要做哪些东西刚刚济南发通知
  • 网站开发方式广州网络营销选择
  • 品牌网站建设方直通车推广计划方案
  • 海外转运网站建设乐云seo官网
  • 有什么可以做建筑模型的网站爱情链接
  • 响应式网站什么意思网站换了域名怎么查
  • 找人做网站多少钱广东seo推广方案
  • 公司网站建设外包郑州网络推广培训
  • 最全的网站大全太原最新情况
  • wordpress不修改数据库更换域名东莞网络优化排名
  • 医院网站建设中标百度高级搜索首页
  • ftp怎么找网站后台seo简单速排名软件
  • 企业在线购物网站建设大型营销型网站制作
  • 北京给网站做系统的公司名称企业网站设计规范
  • 自适应 网站开发社交网络的推广方法
  • 网站建设网站免费百度app安装免费下载
  • 如何在网站上做关键词免费外链发布
  • 长沙有哪些做的好一点的网站外贸推广平台哪个好
  • 合肥手机网站建设新浪舆情通
  • 网站维护一年一般多少钱长清区seo网络优化软件
  • wordpress主题里文章添加留言板苏州手机关键词优化
  • 盐田做网站seo是如何做优化的