自动微分 - Haskell无法推导出类型相等



我有以下代码,它不编译:

  import Numeric.AD
  data Trainable a b = forall n . Floating n =>  Trainable ([n] -> a -> b) (a -> b -> [n] -> n) 
  trainSgdFull :: (Floating n, Ord n) => Trainable a b -> [n] -> a -> b -> [[n]]
  trainSgdFull (Trainable _ cost) init input target =  gradientDescent (cost input target) init

我想使用可训练类型来表示可通过梯度下降训练的机器学习系统。第一个自变量是传递函数,第二个自变量是成本函数,a是输入类型,b是输出/目标类型,列表中包含可学习的参数。编译器抱怨:

 src/MachineLearning/Training.hs:12:73:
Could not deduce (n1 ~ ad-3.3.1.1:Numeric.AD.Internal.Types.AD s n)
from the context (Floating n, Ord n)
  bound by the type signature for
             trainSgdFull :: (Floating n, Ord n) =>
                             Trainable a b -> [n] -> a -> b -> [[n]]
  at src/MachineLearning/Training.hs:12:3-95
or from (Floating n1)
  bound by a pattern with constructor
             Trainable :: forall a b n.
                          Floating n =>
                          ([n] -> a -> b) -> (a -> b -> [n] -> n) -> Trainable a b,
           in an equation for `trainSgdFull'
  at src/MachineLearning/Training.hs:12:17-32
or from (Numeric.AD.Internal.Classes.Mode s)
  bound by a type expected by the context:
             Numeric.AD.Internal.Classes.Mode s =>
             [ad-3.3.1.1:Numeric.AD.Internal.Types.AD s n]
             -> ad-3.3.1.1:Numeric.AD.Internal.Types.AD s n
  at src/MachineLearning/Training.hs:12:56-95
  `n1' is a rigid type variable bound by
       a pattern with constructor
         Trainable :: forall a b n.
                      Floating n =>
                      ([n] -> a -> b) -> (a -> b -> [n] -> n) -> Trainable a b,
       in an equation for `trainSgdFull'
       at src/MachineLearning/Training.hs:12:17
Expected type: [ad-3.3.1.1:Numeric.AD.Internal.Types.AD s n1]
               -> ad-3.3.1.1:Numeric.AD.Internal.Types.AD s n1
  Actual type: [n] -> n
In the return type of a call of `cost'
In the first argument of `gradientDescent', namely
  `(cost input target)'

基本概念正确吗?如果是,我该如何编译代码?

问题是

data Trainable a b = forall n . Floating n =>  Trainable ([n] -> a -> b) (a -> b -> [n] -> n)

意味着在中

Trainable transfer cost

所使用的类型CCD_ 1丢失。所有已知的是,存在具有Floating实例的某种类型的Guessme,使得

transfer :: [Guessme] -> a -> b
cost :: a -> b -> [Guessme] -> Guessme

您可以使用仅适用于Complex FloatDouble或…的函数来构建Trainable。。。

但在

trainSgdFull :: (Floating n, Ord n) => Trainable a b -> [n] -> a -> b -> [[n]]
trainSgdFull (Trainable _ cost) init input target =  gradientDescent (cost input target) init

您正试图将cost与作为参数提供的任何Floating类型一起使用。

Trainable是为使用类型n0而构建的,用户提供类型n1,这些可能相同,也可能不同。因此编译器无法推断它们是相同的。

如果你不想让n成为Trainable的类型参数,你需要让它包装与一起工作的多态函数,调用方提供的每个Floating类型

data Trainable a b
    = Trainable (forall n. Floating n => [n] -> a -> b)
                (forall n. Floating n => a -> b -> [n] -> n)

(需要Rank2Types,或者,由于正在被弃用,需要RankNTypes)。

相关内容

  • 没有找到相关文章

最新更新