如何在hmatrix上自动区分? [英] How to do automatic differentiation on hmatrix?

查看:169
本文介绍了如何在hmatrix上自动区分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Sooooo ...事实证明,从假矩阵 hmatrix 数据类型原来是不平凡的:)



引用的前言:

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

import Numeric.LinearAlgebra.HMatrix
import Numeric.AD

reconstruct ::(容器向量a,Num(向量a))
=> [a] - > [Matrix a] - >矩阵a
重建为φs= sum [a`scale`φ| a< - as | φ< - φs]

preserveInfo ::(容器向量a,Num(向量a))
=>矩阵a - > [a] - > [Matrix a] - > a
preserveInfo img asφs= sumElements(errImg * errImg)
where errImg = img - (reconstruct asφs)

调用 gradientDescent 函数:

  gradientDescentOverAs :: forall m a。 (浮动a,Ord a,Num(矢量a))
=>矩阵a - > [Matrix a] - > [a] - > [[a]]
gradientDescentOverAs imgφsas0 = gradientdecent go as0
go as = preserveInfo img asφs

编辑:这不是原始问题中的代码,而是尽可能地进行了简化。 GHC在 go 子函数上需要一些约束,但链接问题中提出的答案在此不适用。



edit2,从下面引用自己:


我相信这是无法完成的。 Matrix 要求它的元素位于 Element 类中。唯一的元素是 Double , Float 和它们的 Complex 形式。所有这些都不被 gradient gradient 所接受。


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



编辑3



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

解决方案

我发现这个系列的博客文章非常有用:
https://idontgetoutmuch.wordpress.com/2014/09/09/fun-with-extended- kalman-filters-4 /
(这两个HMatrix都具有静态大小保证,并且演示了来自AD的 jacobian 函数)。




Sooooo ... as it turns out going from fake matrices to hmatrix datatypes turns out to be nontrivial :)

Preamble for reference:

{-# 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)

And the call to the gradientDescent function:

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

edit: this is not the code in the original question but boiled down as much as possible. GHC requires some constraints on the go sub-function, but the answer proposed in the linked question doesn't apply here.

edit2, quoting myself from below:

I come to believe it can't be done. Matrix requires it's elements to be in the Element class. The only elements there are Double, Float and their Complex forms. All of these are not accepted by gradientDescent.

So basically this is the same question as the one linked above, but for the hmatrix datatypes instead of my handrolled ones.

edit3

Relevant, email conversation between Edward Kmett and Dominic Steinitz on the topic: https://mail.haskell.org/pipermail/haskell-cafe/2013-April/107561.html

解决方案

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

这篇关于如何在hmatrix上自动区分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆