为什么在这种情况下 Data.Array.Repa.Algorithms.Matrix.mmultP 会出现类型错误?
why do I get a type error in this case with Data.Array.Repa.Algorithms.Matrix.mmultP?
我相信这归结为一个愚蠢的类型错误,但我无法弄清楚。我有以下代码:
{-# 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 表示,我想将其与一维随机数数组(介于 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
我相信这归结为一个愚蠢的类型错误,但我无法弄清楚。我有以下代码:
{-# 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 表示,我想将其与一维随机数数组(介于 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