将来自 hackage 的 SelfOrganizing 映射 som 用于列表列表
Using SelfOrganizing map som from hackage for List of Lists
我想使用 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>
在检查并尝试 https://github.com/mhwombat/som/blob/master/examples/colours.hs
中给出的另一个例子后,我找到了这个问题的答案
使用 som 库提供的函数 euclideanDistanceSquared 和 adjustVector 代替我定义的函数对我有用。
我想使用 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>
在检查并尝试 https://github.com/mhwombat/som/blob/master/examples/colours.hs
中给出的另一个例子后,我找到了这个问题的答案使用 som 库提供的函数 euclideanDistanceSquared 和 adjustVector 代替我定义的函数对我有用。