使用来自Hackage的SelfOrganizing map som获取列表列表

时间:2018-09-22 21:10:14

标签: haskell self-organizing-maps

我想使用http://hackage.haskell.org/package/som中的som软件包 用我自己的数据测试一些东西。我查看了示例https://github.com/mhwombat/som/blob/master/examples/housePrices.hs

并且我必须为我的用例重写​​代码,如浮点数据或列表中的双列表

let myData = [[1.2,1.3,4.1],[1.2,1.3,3.1] ...]

我希望为列表列表的其他som软件包提供帮助或提示,以作为输入。

编辑:完整的代码

import Control.Monad (foldM_, forM_, unless, replicateM)
import Control.Monad.Random (evalRandIO, Rand, RandomGen, getRandomR)
import Data.Datamining.Pattern (adjustVector,  euclideanDistanceSquared)
import Data.Datamining.Clustering.SOM (SOM(..), toGridMap, decayingGaussian)
import Data.Datamining.Clustering.Classifier (Classifier, train, trainBatch)

import Data.List (foldl')
import Data.Word (Word8)
import Data.Array.IArray (elems)
import Data.Array.Unboxed (UArray)
import Data.Array.ST (runSTArray)
import GHC.Arr (listArray, readSTArray, thawSTArray, writeSTArray)
import Math.Geometry.Grid 
import Math.Geometry.Grid.Square (RectSquareGrid, rectSquareGrid)
import qualified Math.Geometry.GridMap as GM 
import Math.Geometry.GridMap.Lazy (LGridMap, lazyGridMap)
import Numeric (showHex)
import System.Directory (doesFileExist) 

main :: IO ()
main = do
  c <- evalRandIO $ buildSOM (length myTestDataInput)
  putStr . show . map round . GM.elems . toGridMap $ c
  foldM_ trainAndPrint c myTestDataInput

trainAndPrint c x = do
   let c2 = train c x
   putStr . show . map round . GM.elems . toGridMap $ c2
   putStrLn $ " after training with " ++ show (round x)
   return c2


 buildSOM n = do     
    let g = rectSquareGrid 3 3  
    let gm = lazyGridMap g ownWeights
    let n' = fromIntegral n
    let lrf = decayingGaussian 0.5 0.1 0.3 0.1 n' 
    return $ SOM gm lrf absD adjustNum 0


ownWeights = [[1.2,1.3],[1.2,1.3],[1.2,1.3],[1.2,1.3],[1.2,4.3],[1.2,1.5],[6.2,1.3]]
myTestDataInput  = [[1.2,1.3],[1.2,1.3],[1.3,3.1],[1.2,2.3],[4.3,3.1],[1.5,3.1],[6.2,1.3]]

absD _ [] = []
absD [] _ = []
absD (x:xs) (y:ys) = abs (x-y) : absD xs ys

adjustNum [] _ _ = []
adjustNum (target:tarL) r (x:xs)
  | r < 0     = error "Negative learning rate"
  | r > 1     = error "Learning rate > 1"
  | otherwise = x + r*(target - x) : adjustNum tarL r xs

完整错误:

C:\ NN \ SOM.hs:65:28:错误:

* Occurs check: cannot construct the infinite type: a0 ~ [a0]
  Expected type: [a0] -> [a0] -> [a0] -> [a0]
    Actual type: [a0] -> a0 -> [a0] -> [a0]
* In the fourth argument of `SOM', namely `adjustNum'
  In the second argument of `($)', namely
    `SOM gm lrf absD adjustNum 0'
  In a stmt of a 'do' block: return $ SOM gm lrf absD adjustNum 0
* Relevant bindings include
    lrf :: [a0] -> [a0] -> [a0] (bound at C:\\NN\SOM.hs:64:7)
    n' :: [a0] (bound at C:\\NN\SOM.hs:63:7)
    gm :: LGridMap RectSquareGrid [a0] (bound at C:\\NN\SOM.hs:62:7)
    buildSOM :: Int
                -> Control.Monad.Trans.Random.Lazy.RandT
                     System.Random.StdGen
                     Data.Functor.Identity.Identity
                     (SOM [a0] [a0] (LGridMap RectSquareGrid) [a0] (Int, Int) [a0])
      (bound at C:\\NN\SOM.hs:56:1)
 | 65 | return $ SOM gm lrf absD adjustNum 0 | ^^^^^^^^^ Failed, no modules loaded. Prelude>

1 个答案:

答案 0 :(得分:0)

在检查并尝试了https://github.com/mhwombat/som/blob/master/examples/colours.hs

中给出的另一个示例后,我找到了针对此问题的答案。

使用som lib提供的函数euclideanDistanceSquared和AdjustVector代替我定义的函数为我工作。