5

太棒了……事实证明,从假矩阵hmatrix数据类型的转变是不平凡的:)

序言供参考:

{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ParallelListComp #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}

import           Numeric.LinearAlgebra.HMatrix
import           Numeric.AD

reconstruct :: (Container Vector a, Num (Vector a)) 
            => [a] -> [Matrix a] -> Matrix a
reconstruct as φs = sum [ a `scale` φ | a <- as | φ <- φs ]

preserveInfo :: (Container Vector a, Num (Vector a))
     => Matrix a -> [a] -> [Matrix a] -> a
preserveInfo img as φs = sumElements (errImg * errImg)
    where errImg = img - (reconstruct as φs)

以及对gradientDescent函数的调用:

gradientDescentOverAs :: forall m a. (Floating a, Ord a, Num (Vector a))
                      => Matrix a -> [Matrix a] -> [a] -> [[a]]
gradientDescentOverAs img φs as0 = gradientDescent go as0
  where go as = preserveInfo img as φs

编辑:这不是原始问题中的代码,而是尽可能地归结起来。GHC 需要对子功能进行一些限制go,但链接问题中提出的答案不适用于此处。

edit2,从下面引用我自己的话:

我开始相信这是不可能的。Matrix要求它的元素在Element类中。唯一的元素DoubleFloat以及它们的Complex形式。所有这些都是不被接受的gradientDescent

所以基本上这与上面链接的问题相同,但是对于hmatrix数据类型而不是我的手动处理的问题。

编辑3

Edward Kmett 和 Dominic Steinitz 之间的相关电子邮件对话:https ://mail.haskell.org/pipermail/haskell-cafe/2013-April/107561.html

4

1 回答 1

1

I found this series of blog posts to be very helpful: https://idontgetoutmuch.wordpress.com/2014/09/09/fun-with-extended-kalman-filters-4/ (both HMatrix with static size guarantees and the jacobian function from AD are demonstrated).

HTH

于 2015-05-06T15:13:03.823 回答