2015-11-07 98 views
2

我認爲這可以歸結爲愚蠢的類型錯誤,但我無法弄清楚。我有以下代碼:爲什麼我在這種情況下使用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列,我想利用的(使用M.mmultP)的點積與一維陣列的惹巴表示的隨機數(在0,1之間),列長度等於2D數組的行數。我在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 

這是一個類型錯誤,但我似乎無法弄清楚。有人可以幫我嗎?

+0

'mmultP'不* *並行工作,所以它在IO單子運行。或者將其更改爲'mmultS' - 執行相同的操作,但是按順序執行,或者將'powerIteration'的類型更改爲'R.Array ... - > IO(R.Array ...)' – user2407038

+0

哇,我沒有注意並行和順序之間的不同功能簽名。如果你把它寫成答案,我會接受它作爲答案。 – stian

回答

0

是的,那裏的意見是正確的,IO單子應該有:

{-# 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 
相關問題