gpt4 book ai didi

haskell - 为什么在这种情况下使用 Data.Array.Repa.Algorithms.Matrix.mmultP 会出现类型错误?

转载 作者:行者123 更新时间:2023-12-02 04:45:16 25 4
gpt4 key购买 nike

我相信这归结为一个愚蠢的类型错误,但我无法弄清楚。我有以下代码:

{-# LANGUAGE TypeOperators #-}

import qualified Data.Array.Repa as R
import System.Random
import Data.Random.Normal
import Data.List
import Data.Function
import qualified Data.Array.Repa.Algorithms.Matrix as M

weights:: (Int,Int)->(Double,Double) -> R.Array R.U (R.Z R.:. Int R.:. Int) Double

weights (nodes,features) range = R.fromListUnboxed (R.Z R.:. nodes R.:. features) values
where
values = take (nodes * features) $ randomRs range gen
gen = mkStdGen 1

powerIteration :: R.Array R.U (R.Z R.:. Int R.:. Int) Double -> R.Array R.U (R.Z R.:. Int R.:. Int) Double

powerIteration m = M.mmultP m rb
where
rb = weights (rows,1) (0,1)
(R.Z R.:. _ R.:. rows) = R.extent m


main = do
let matrix = weights (10,3) (0,1)
print $ powerIteration matrix

这将创建一个具有 10 行和 3 列的二维数组的 Repa 表示,我想将(使用 M.mmultP)与一维随机数数组(0,1 之间)的点积与列长度​​等于二维数组的行号。我得到了类似于在 ghci 中工作的东西,但这可能会产生这个错误;

Couldn't match type ‘R.Array R.U R.DIM2 Double’ with ‘Double’
Expected type: R.Array R.U ((R.Z R.:. Int) R.:. Int) Double
Actual type: R.Array
R.U ((R.Z R.:. Int) R.:. Int) (R.Array R.U R.DIM2 Double)
In the expression: M.mmultP m rb
In an equation for ‘powerIteration’:
powerIteration m
= M.mmultP m rb
where
rb = weights (rows, 1) (0, 1)
(R.Z R.:. _ R.:. rows) = R.extent m

这是一个类型错误,但我似乎无法弄清楚。有人可以帮助我吗?

最佳答案

是的,正确的注释,IO monad 应该在那里:

{-# LANGUAGE TypeOperators #-}

import Data.Array.Repa
import System.Random
import Data.List
import Data.Function
import Data.Array.Repa.Algorithms.Matrix

weights :: (Int, Int) -> (Double, Double) -> Array U (Z :. Int :. Int) Double
weights (nodes, features) range = fromListUnboxed (Z :. nodes :. features) values
where
values = take (nodes * features) $ randomRs range gen
gen = mkStdGen 1

powerIteration :: Array U (Z :. Int :. Int) Double -> IO (Array U (Z :. Int :. Int) Double)
powerIteration m = mmultP m rb
where
rb = weights (rows,1) (0,1)
(Z :. _ :. rows) = extent m


main = do
let matrix = weights (10,3) (0,1)
iter <- powerIteration matrix
print iter

关于haskell - 为什么在这种情况下使用 Data.Array.Repa.Algorithms.Matrix.mmultP 会出现类型错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33585077/

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