colada 0.5.3 → 0.5.5
raw patch · 3 files changed
+38/−32 lines, 3 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
+ Colada.WordClass: instance Selector S1_0_14Options
+ Colada.WordClass: pre :: Arrow arr => Lens arr Options Bool
Files
- Colada/WordClass.hs +32/−31
- colada.cabal +1/−1
- colada.hs +5/−0
Colada/WordClass.hs view
@@ -75,6 +75,7 @@ , initPasses , exponent , progressive+ , pre , lambda ) where @@ -146,6 +147,7 @@ , _initPasses :: !Int , _exponent :: !(Maybe Double) , _progressive :: !Bool+ , _pre :: !Bool , _lambda :: !Double } deriving (Eq, Show, Typeable, Data, Generic)@@ -168,17 +170,18 @@ , _initPasses = 100 , _exponent = Nothing , _progressive= False- , _lambda = 0.5+ , _pre = False + , _lambda = 1.0 } -- | @learn options xs@ runs the LDA Gibbs sampler for word classes -- with @options@ on sentences @xs@, and returns the resulting model--- together progressive class the assignments+-- together with progressive class assignments learn :: Options -> [CoNLL.Sentence] -> (WordClass, [V.Vector (U.Vector Double)]) learn opts xs = - let ((sbs_init, sbs_rest), atomTabD, atomTabW) = + let ((bs_init, bs_rest), atomTabD, atomTabW) = Symbols.runSymbols prepare Symbols.empty Symbols.empty prepare = do let (xs_init, xs_rest) = List.splitAt (get initSize opts) xs@@ -198,40 +201,45 @@ (get alphasum opts) (get beta opts) (get exponent opts)- let loop t z i = do- r <- st $ Trav.forM z $ \b -> do- Trav.forM b $ \s -> do- LDA.pass t m s- M.when (get progressive opts && i == 1) $ do- let b = V.head z - Fold.forM_ b $ \s -> do - ls <- st $ V.mapM (interpWordClasses m (get lambda opts)) s- tell [ls]+ let loop t i_last batch i = do+ let label_prog = do + Fold.forM_ batch $ \rep -> do + let sent = V.head rep+ ls <- st $ V.mapM (interpWordClasses m (get lambda opts)) sent+ tell [ls]+ -- Either label before sampling (--pre)+ M.when (get progressive opts && i == 1 && get pre opts) label_prog + r <- st $ Trav.forM batch $ \rep -> do+ Trav.forM rep $ \sent -> do+ LDA.pass t m sent+ -- Or label after sampling + M.when (get progressive opts && i == i_last && not (get pre opts)) label_prog return $! r -- Initialize with batch sampler on prefix sbs_init - Fold.forM_ sbs_init $ \sb -> do - Fold.foldlM (loop 1) sb [1..get initPasses opts] + Fold.forM_ bs_init $ \batch -> do + Fold.foldlM (loop 1 $ get initPasses opts) batch [1..get initPasses opts] -- Continue sampling- Fold.forM_ (zip [1..] sbs_rest) $ \(t,sb) -> do- Fold.foldlM (loop t) sb [1..get passes opts]+ Fold.forM_ (zip [1..] bs_rest) $ \(t, batch) -> do+ Fold.foldlM (loop t $ get passes opts) batch [1..get passes opts] st $ LDA.finalize m (lda, labeled) = LST.runST (runWriterT sampler) in (WordClass lda atomTabD atomTabW opts, labeled) type Symb = Symbols.Symbols (U.Vector Char) (U.Vector Char) type Sent = V.Vector LDA.Doc-type Batch = V.Vector Sent-type SuperBatch = V.Vector Batch+type Repeat = V.Vector Sent+type Batch = V.Vector Repeat+ -- | Convert a stream of sentences into a stream of batches ready for -- sampling.-prepareData :: Int -- ^ batch size +prepareData :: Int -- ^ batch size -> Int -- ^ no. repeats -> [Int] -- ^ feature indices -> [CoNLL.Sentence] -- ^ stream of sentences- -> Symb [SuperBatch] -- ^ stream of superbatches+ -> Symb [Batch] -- ^ stream of batches prepareData bsz rep is ss = do ss' <- mapM symbolize . map (featurize is) $ ss- return $! map (multiply rep) . batchup bsz $ ss'+ return $! (map V.fromList . Split.chunksOf bsz . map (V.replicate rep) $ ss') -- | Extract features from a sentence featurize :: [Int] @@ -254,14 +262,6 @@ was <- mapM (Symbols.toAtomB . compress) ws return (da, U.fromList $ zip was (repeat Nothing)) --- | Chunk sentences stream into batches-batchup :: Int -> [Sent] -> [Batch]-batchup bsz = map V.fromList . Split.chunk bsz---- | Replicate and flatten a batch of sentences-multiply :: Int -> Batch -> SuperBatch-multiply rep = V.replicate rep- -- | @summary m@ returns a textual summary of word classes found in -- model @m@ summary :: WordClass -> Text.Text@@ -313,8 +313,9 @@ where normalize x = let uniform = U.replicate (U.length x) (1 / (fromIntegral (U.length x))) in case U.sum x of- 0 -> uniform- s -> U.map (/s) x+ 0 -> uniform+ s | s >= 1/0 -> uniform + s -> U.map (/s) x -- | @wordTypeClasses m@ returns a Map from word types to unnormalized -- distributions over word classes
colada.cabal view
@@ -1,5 +1,5 @@ Name: colada-Version: 0.5.3+Version: 0.5.5 Synopsis: Colada implements incremental word class class induction using online LDA Description: Colada implements incremental word class class induction using
colada.hs view
@@ -126,6 +126,11 @@ (\p -> maybe p id . M.set (C.progressive . options) True $ p) "Label progressively" + + , flagNone ["pre"] + (\p -> + maybe p id . M.set (C.pre . options) True $ p)+ "Progressive labeling done before running a pass" , flagReq ["lambda"] (setOption C.lambda) "FLOAT" "Interpolation parameter for progressive labeling"