使用自组织地图 som 从黑客到列表列表



我想使用 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:\NNSOM.hs:64:7)
n' :: [a0] (bound at C:\NNSOM.hs:63:7)
gm :: LGridMap RectSquareGrid [a0] (bound at C:\NNSOM.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:\NNSOM.hs:56:1)
| 65 | return $ SOM gm lrf absD adjustNum 0 | ^^^^^^^^^ Failed, no modules loaded. Prelude>

在检查并尝试了 https://github.com/mhwombat/som/blob/master/examples/colours.hs 中给出的其他示例后,我找到了这个问题的答案

使用 som lib 提供的函数 euclideanDistanceSquared 和 adjustVector 而不是我定义的函数对我有用。

最新更新