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

做网站个体户经营范围百度收录技巧

做网站个体户经营范围,百度收录技巧,网站建设怎么做,长沙中小企业有哪些公司🎯要点 🎯漂移扩散方程计算微分 | 🎯期权无风险套利公式计算微分 | 🎯实现图结构算法微分 | 🎯实现简单正向和反向计算微分 | 🎯实现简单回归分类和生成对抗网络计算微分 | 🎯几何网格计算微分…

🎯要点

🎯漂移扩散方程计算微分 | 🎯期权无风险套利公式计算微分 | 🎯实现图结构算法微分 | 🎯实现简单正向和反向计算微分 | 🎯实现简单回归分类和生成对抗网络计算微分 | 🎯几何网格计算微分

🍇Python和C++计算微分正反向累积

算法微分在机器学习领域尤为重要。例如,它允许人们在神经网络中实现反向传播,而无需手动计算导数。

计算微分的基础是复合函数偏导数链式法则提供的微分分解。简单结构如:
y = f ( g ( h ( x ) ) ) = f ( g ( h ( w 0 ) ) ) = f ( g ( w 1 ) ) = f ( w 2 ) = w 3 w 0 = x w 1 = h ( w 0 ) w 2 = g ( w 1 ) w 3 = f ( w 2 ) = y \begin{aligned} y & =f(g(h(x)))=f\left(g\left(h\left(w_0\right)\right)\right)=f\left(g\left(w_1\right)\right)=f\left(w_2\right)=w_3 \\ w_0 & =x \\ w_1 & =h\left(w_0\right) \\ w_2 & =g\left(w_1\right) \\ w_3 & =f\left(w_2\right)=y \end{aligned} yw0w1w2w3=f(g(h(x)))=f(g(h(w0)))=f(g(w1))=f(w2)=w3=x=h(w0)=g(w1)=f(w2)=y
由链式法则得出:
∂ y ∂ x = ∂ y ∂ w 2 ∂ w 2 ∂ w 1 ∂ w 1 ∂ x = ∂ f ( w 2 ) ∂ w 2 ∂ g ( w 1 ) ∂ w 1 ∂ h ( w 0 ) ∂ x \frac{\partial y}{\partial x}=\frac{\partial y}{\partial w_2} \frac{\partial w_2}{\partial w_1} \frac{\partial w_1}{\partial x}=\frac{\partial f\left(w_2\right)}{\partial w_2} \frac{\partial g\left(w_1\right)}{\partial w_1} \frac{\partial h\left(w_0\right)}{\partial x} xy=w2yw1w2xw1=w2f(w2)w1g(w1)xh(w0)
通常,存在两种不同的计算微分模式:正向累积和反向累积。

正向累积指定从内到外遍历链式法则(即首先计算 ∂ w 1 / ∂ x \partial w_1 / \partial x w1/x,然后计算 ∂ w 2 / ∂ w 1 \partial w_2 / \partial w_1 w2/w1,最后计算 ∂ y / ∂ w 2 \partial y / \partial w_2 y/w2 ),而反向累积是从外到内的遍历(首先计算 ∂ y / ∂ w 2 \partial y / \partial w_2 y/w2,然后计算 ∂ w 2 / ∂ w 1 \partial w_2 / \partial w_1 w2/w1,最后计算 ∂ w 1 / ∂ x \partial w_1 / \partial x w1/x​)。更简洁地说,

正向累积计算递归关系: ∂ w i ∂ x = ∂ w i ∂ w i − 1 ∂ w i − 1 ∂ x \frac{\partial w_i}{\partial x}=\frac{\partial w_i}{\partial w_{i-1}} \frac{\partial w_{i-1}}{\partial x} xwi=wi1wixwi1 w 3 = y w_3=y w3=y

反向累积计算递归关系: ∂ y ∂ w i = ∂ y ∂ w i + 1 ∂ w i + 1 ∂ w i \frac{\partial y}{\partial w_i}=\frac{\partial y}{\partial w_{i+1}} \frac{\partial w_{i+1}}{\partial w_i} wiy=wi+1ywiwi+1 w 0 = x w_0=x w0=x

正向累积在一次传递中计算函数和导数(但每个仅针对一个独立变量)。相关方法调用期望表达式 Z 相对于变量 V 导出。该方法返回一对已求值的函数及其导数。该方法递归遍历表达式树,直到到达变量。如果请求相对于此变量的导数,则其导数为 1,否则为 0。然后求偏函数以及偏导数。

伪代码:

tuple<float,float> evaluateAndDerive(Expression Z, Variable V) {if isVariable(Z)if (Z = V) return {valueOf(Z), 1};else return {valueOf(Z), 0};else if (Z = A + B){a, a'} = evaluateAndDerive(A, V);{b, b'} = evaluateAndDerive(B, V);return {a + b, a' + b'};else if (Z = A - B){a, a'} = evaluateAndDerive(A, V);{b, b'} = evaluateAndDerive(B, V);return {a - b, a' - b'};else if (Z = A * B){a, a'} = evaluateAndDerive(A, V);{b, b'} = evaluateAndDerive(B, V);return {a * b, b * a' + a * b'};
}

Python实现正向累积:

class ValueAndPartial:def __init__(self, value, partial):self.value = valueself.partial = partialdef toList(self):return [self.value, self.partial]class Expression:def __add__(self, other):return Plus(self, other)def __mul__(self, other):return Multiply(self, other)class Variable(Expression):def __init__(self, value):self.value = valuedef evaluateAndDerive(self, variable):partial = 1 if self == variable else 0return ValueAndPartial(self.value, partial)class Plus(Expression):def __init__(self, expressionA, expressionB):self.expressionA = expressionAself.expressionB = expressionBdef evaluateAndDerive(self, variable):valueA, partialA = self.expressionA.evaluateAndDerive(variable).toList()valueB, partialB = self.expressionB.evaluateAndDerive(variable).toList()return ValueAndPartial(valueA + valueB, partialA + partialB)class Multiply(Expression):def __init__(self, expressionA, expressionB):self.expressionA = expressionAself.expressionB = expressionBdef evaluateAndDerive(self, variable):valueA, partialA = self.expressionA.evaluateAndDerive(variable).toList()valueB, partialB = self.expressionB.evaluateAndDerive(variable).toList()return ValueAndPartial(valueA * valueB, valueB * partialA + valueA * partialB)# Example: Finding the partials of z = x * (x + y) + y * y at (x, y) = (2, 3)
x = Variable(2)
y = Variable(3)
z = x * (x + y) + y * y
xPartial = z.evaluateAndDerive(x).partial
yPartial = z.evaluateAndDerive(y).partial
print("∂z/∂x =", xPartial)  # Output: ∂z/∂x = 7
print("∂z/∂y =", yPartial)  # Output: ∂z/∂y = 8

C++实现正向累积:

#include <iostream>
struct ValueAndPartial { float value, partial; };
struct Variable;
struct Expression {virtual ValueAndPartial evaluateAndDerive(Variable *variable) = 0;
};
struct Variable: public Expression {float value;Variable(float value): value(value) {}ValueAndPartial evaluateAndDerive(Variable *variable) {float partial = (this == variable) ? 1.0f : 0.0f;return {value, partial};}
};
struct Plus: public Expression {Expression *a, *b;Plus(Expression *a, Expression *b): a(a), b(b) {}ValueAndPartial evaluateAndDerive(Variable *variable) {auto [valueA, partialA] = a->evaluateAndDerive(variable);auto [valueB, partialB] = b->evaluateAndDerive(variable);return {valueA + valueB, partialA + partialB};}
};
struct Multiply: public Expression {Expression *a, *b;Multiply(Expression *a, Expression *b): a(a), b(b) {}ValueAndPartial evaluateAndDerive(Variable *variable) {auto [valueA, partialA] = a->evaluateAndDerive(variable);auto [valueB, partialB] = b->evaluateAndDerive(variable);return {valueA * valueB, valueB * partialA + valueA * partialB};}
};
int main () {// Example: Finding the partials of z = x * (x + y) + y * y at (x, y) = (2, 3)Variable x(2), y(3);Plus p1(&x, &y); Multiply m1(&x, &p1); Multiply m2(&y, &y); Plus z(&m1, &m2);float xPartial = z.evaluateAndDerive(&x).partial;float yPartial = z.evaluateAndDerive(&y).partial;std::cout << "∂z/∂x = " << xPartial << ", "<< "∂z/∂y = " << yPartial << std::endl;// Output: ∂z/∂x = 7, ∂z/∂y = 8return 0;
}

反向累积需要两次传递:在正向传递中,首先评估函数并缓存部分结果。在反向传递中,计算偏导数并反向传播先前导出的值。相应的方法调用期望表达式 Z 被导出,并以父表达式的导出值为种子。对于顶部表达式 Z 相对于 Z 导出,这是 1。该方法递归遍历表达式树,直到到达变量并将当前种子值添加到导数表达式。

伪代码:

void derive(Expression Z, float seed) {if isVariable(Z)partialDerivativeOf(Z) += seed;else if (Z = A + B)derive(A, seed);derive(B, seed);else if (Z = A - B)derive(A, seed);derive(B, -seed);else if (Z = A * B)derive(A, valueOf(B) * seed);derive(B, valueOf(A) * seed);
}

Python实现反向累积:

class Expression:def __add__(self, other):return Plus(self, other)def __mul__(self, other):return Multiply(self, other)class Variable(Expression):def __init__(self, value):self.value = valueself.partial = 0def evaluate(self):passdef derive(self, seed):self.partial += seedclass Plus(Expression):def __init__(self, expressionA, expressionB):self.expressionA = expressionAself.expressionB = expressionBself.value = Nonedef evaluate(self):self.expressionA.evaluate()self.expressionB.evaluate()self.value = self.expressionA.value + self.expressionB.valuedef derive(self, seed):self.expressionA.derive(seed)self.expressionB.derive(seed)class Multiply(Expression):def __init__(self, expressionA, expressionB):self.expressionA = expressionAself.expressionB = expressionBself.value = Nonedef evaluate(self):self.expressionA.evaluate()self.expressionB.evaluate()self.value = self.expressionA.value * self.expressionB.valuedef derive(self, seed):self.expressionA.derive(self.expressionB.value * seed)self.expressionB.derive(self.expressionA.value * seed)# Example: Finding the partials of z = x * (x + y) + y * y at (x, y) = (2, 3)
x = Variable(2)
y = Variable(3)
z = x * (x + y) + y * y
z.evaluate()
print("z =", z.value)        # Output: z = 19
z.derive(1)
print("∂z/∂x =", x.partial)  # Output: ∂z/∂x = 7
print("∂z/∂y =", y.partial)  # Output: ∂z/∂y = 8

C++实现反向累积:

#include <iostream>
struct Expression {float value;virtual void evaluate() = 0;virtual void derive(float seed) = 0;
};
struct Variable: public Expression {float partial;Variable(float _value) {value = _value;partial = 0;}void evaluate() {}void derive(float seed) {partial += seed;}
};
struct Plus: public Expression {Expression *a, *b;Plus(Expression *a, Expression *b): a(a), b(b) {}void evaluate() {a->evaluate();b->evaluate();value = a->value + b->value;}void derive(float seed) {a->derive(seed);b->derive(seed);}
};
struct Multiply: public Expression {Expression *a, *b;Multiply(Expression *a, Expression *b): a(a), b(b) {}void evaluate() {a->evaluate();b->evaluate();value = a->value * b->value;}void derive(float seed) {a->derive(b->value * seed);b->derive(a->value * seed);}
};
int main () {// Example: Finding the partials of z = x * (x + y) + y * y at (x, y) = (2, 3)Variable x(2), y(3);Plus p1(&x, &y); Multiply m1(&x, &p1); Multiply m2(&y, &y); Plus z(&m1, &m2);z.evaluate();std::cout << "z = " << z.value << std::endl;// Output: z = 19z.derive(1);std::cout << "∂z/∂x = " << x.partial << ", "<< "∂z/∂y = " << y.partial << std::endl;// Output: ∂z/∂x = 7, ∂z/∂y = 8return 0;
}

👉参阅一:计算思维

👉参阅二:亚图跨际

http://www.dinnco.com/news/12544.html

相关文章:

  • 公众平台申请关键词seo服务
  • 建设网站的必要性怎么查搜索关键词排名
  • 大连制作网站报价互联网营销策略有哪些
  • 苏州专业网站建设设计公司排名线上销售平台都有哪些
  • 室内设计培训班要多少钱seo项目分析
  • 网站建设制作周期seo网络优化专员
  • 东莞手机网站价格便宜如何做好线上营销
  • 做网站需要哪些东西太原百度seo
  • 做网站要固定电话网站案例
  • 做网站商城多少钱宁波seo外包推广公司
  • 中国有名的模版网站百度官方营销推广平台
  • 网站互动功能产品推广活动策划方案
  • 住房和城乡建设部网站官网网络广告文案范文
  • 吃鸡辅助群的购卡链接网站怎么做网站建设策划
  • 企业网站联系我们软文文案
  • 淘宝网店运营seo公司上海
  • 时尚类网站建设网络营销相关工作岗位
  • 网站的后期维护自己怎么做百度搜索引擎下载
  • 做网站费用怎么核算合肥360seo排名
  • 两学一做 山西答题网站百度免费建网站
  • 临汾网络推广站长seo软件
  • 国外被动收入网站做的好的输入关键词自动生成标题
  • 外国网站怎么做b2b外贸接单平台
  • 网站代理备案价格seo建站是什么
  • 工信部网站验证码搜索引擎技术包括哪些
  • 网站内容的重要性磁力链最佳的搜索引擎
  • 金坛网站建设公司外贸公司如何做推广
  • 广安哪里有做网站的公司湖南网络优化服务
  • vps可以做多少网站网站开发工具
  • 大米网站模板软件制作