이 질문은 후속 질문입니다 (Haskell can't deduce type equality). 나는 다음과 같은 코드로 내 앞의 질문에 설명 된 학습 가능한 유형을 사용하여 기본 다항식 approximator 구현하는 시도 :하스켈은 여전히 유형 평등을 추론 할 수 없습니다.
여기module MachineLearning.Polynomial
(polynomial,
polynomialTrf
) where
import MachineLearning.Training
import MachineLearning.Utils
polynomialTrf :: Floating n => [n] -> n -> n
polynomialTrf coeff var = helper 0 coeff var 0 where
helper _ [] var acc = acc
helper 0 (c:cs) var acc = helper 1 cs var c
helper deg (c:cs) var acc = helper (deg+1) cs var (acc+(c*(var^deg)))
polynomialCost :: Floating n => n -> n -> [n] -> n
polynomialCost var target coeff = sqcost (polynomialTrf coeff var) target
polynomialSV :: (Floating n) => Trainable n n
polynomialSV = Trainable polynomialTrf polynomialCost
sqcost
그냥 sqcost a b = (a-b)^2
입니다. 컴파일러에서 다음과 같은 오류 메시지가 나타납니다.
src/MachineLearning/Polynomial.hs:18:26:
Could not deduce (n1 ~ n)
from the context (Floating n)
bound by the type signature for
polynomialSV :: Floating n => Trainable n n
at src/MachineLearning/Polynomial.hs:18:1-53
or from (Floating n1)
bound by a type expected by the context:
Floating n1 => [n1] -> n -> n
at src/MachineLearning/Polynomial.hs:18:16-53
`n1' is a rigid type variable bound by
a type expected by the context: Floating n1 => [n1] -> n -> n
at src/MachineLearning/Polynomial.hs:18:16
`n' is a rigid type variable bound by
the type signature for polynomialSV :: Floating n => Trainable n n
at src/MachineLearning/Polynomial.hs:18:1
Expected type: [n] -> n1 -> n1
Actual type: [n] -> n -> n
In the first argument of `Trainable', namely `polynomialTrf'
In the expression: Trainable polynomialTrf polynomialCost
src/MachineLearning/Polynomial.hs:18:40:
Could not deduce (n ~ n1)
from the context (Floating n)
bound by the type signature for
polynomialSV :: Floating n => Trainable n n
at src/MachineLearning/Polynomial.hs:18:1-53
or from (Floating n1)
bound by a type expected by the context:
Floating n1 => n -> n -> [n1] -> n1
at src/MachineLearning/Polynomial.hs:18:16-53
`n' is a rigid type variable bound by
the type signature for polynomialSV :: Floating n => Trainable n n
at src/MachineLearning/Polynomial.hs:18:1
`n1' is a rigid type variable bound by
a type expected by the context: Floating n1 => n -> n -> [n1] -> n1
at src/MachineLearning/Polynomial.hs:18:16
Expected type: n -> n -> [n1] -> n1
Actual type: n -> n -> [n] -> n
In the second argument of `Trainable', namely `polynomialCost'
In the expression: Trainable polynomialTrf polynomialCost
내 질문은 어디에서 발생합니까? 어떻게 해결할 수 있습니까? 저에게는 두 가지 유형이 동등하다는 것이 명확하게 느껴 지므로 형식 체계에서 어떤 것을 오해 할 가능성이 있습니다.
여기에있는 코드는 원래 있던 유형의 실존 유형을 원합니다. –
'Trainable'에서'n'을 숨길 필요가 있습니까? 그냥 'Trainable n a b'로 만들 수 없습니까? –
'훈련 모듈 (trainable nab) '을 쓸데없이 재 작성했지만 컴파일되지는 않는다.'trainSgdFull'에서'n'과'AD'를 비교할 수 없다. – laci37