我有以下代码,无法编译:
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
我想使用 Trainable 类型来表示可通过梯度下降训练的机器学习系统。第一个参数是传递函数,第二个参数是成本函数,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)'
基本概念对吗?如果是,我怎样才能使代码编译?