gpt4 book ai didi

haskell - Numeric.AD 和打字问题

转载 作者:行者123 更新时间:2023-12-04 12:13:17 25 4
gpt4 key购买 nike

我正在尝试使用 Numeric.AD 和自定义 Expr 类型。我想计算
用户输入表达式的符号梯度。第一次用常数试验
表达效果很好:

calcGrad0 :: [Expr Double]
calcGrad0 = grad df vars
where
df [x,y] = eval (env [x,y]) (EVar "x"*EVar "y")
env vs = zip varNames vs
varNames = ["x","y"]
vars = map EVar varNames

这有效:
>calcGrad0
[Const 0.0 :+ (Const 0.0 :+ (EVar "y" :* Const 1.0)),Const 0.0 :+ (Const 0.0 :+ (EVar "x" :* Const 1.0))]

但是,如果我将表达式作为参数提取出来:
calcGrad1 :: [Expr Double]
calcGrad1 = calcGrad1' (EVar "x"*EVar "y")
calcGrad1' e = grad df vars
where
df [x,y] = eval (env [x,y]) e
env vs = zip varNames vs
varNames = ["x","y"]
vars = map EVar varNames

我明白了
Could not deduce (a ~ AD s (Expr a1))
from the context (Num a1, Floating a)
bound by the inferred type of
calcGrad1' :: (Num a1, Floating a) => Expr a -> [Expr a1]
at Symbolics.hs:(60,1)-(65,29)
or from (Mode s)
bound by a type expected by the context:
Mode s => [AD s (Expr a1)] -> AD s (Expr a1)
at Symbolics.hs:60:16-27
`a' is a rigid type variable bound by
the inferred type of
calcGrad1' :: (Num a1, Floating a) => Expr a -> [Expr a1]
at Symbolics.hs:60:1
Expected type: [AD s (Expr a1)] -> AD s (Expr a1)
Actual type: [a] -> a
In the first argument of `grad', namely `df'
In the expression: grad df vars

我如何让 ghc 接受这个?

最佳答案

我猜你忘记申请 lift转换 ExprAD s Expr .

如果您有兴趣使用 ad符号区分包。 Lennart Augustsson 的 traced包效果很好。

关于haskell - Numeric.AD 和打字问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5938337/

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