gpt4 book ai didi

haskell - 如何在hmatrix上进行自动微分?

转载 作者:行者123 更新时间:2023-12-04 21:39:34 25 4
gpt4 key购买 nike

Sooooo ... 原来来自 fake matriceshmatrix数据类型原来是不平凡的:)

前言供引用:

{-# 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,从下面引用我自己:

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.



所以基本上这与上面链接的问题相同,但对于 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 函数)。

HTH

关于haskell - 如何在hmatrix上进行自动微分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30072218/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com