Permalink
Browse files

Scaled version of forward computation.

  • Loading branch information...
1 parent 11e440b commit 1ce087eba9bfc448e943e25902c5b481c4a41f03 @arnsholt committed Aug 10, 2011
Showing with 15 additions and 1 deletion.
  1. +15 −1 crf.lisp
View
@@ -221,14 +221,28 @@
with Y = (quarks-size (crf-tagset crf))
with psi = (psi crf seq :exp t)
with alpha = (make-array (list L Y) :initial-element 0.0 :element-type 'single-float)
+ with scales = (make-array L :element-type 'single-float)
initially (loop for q below Y do (setf (aref alpha 0 q) (aref psi 0 0 q)))
+ (setf (aref scales 0) (scale alpha 0))
for i from 1 below L
do (loop
for q below Y
for prob = (loop
for q-prime below Y
summing (* (aref alpha (1- i) q-prime) (aref psi i q-prime q)))
do (setf (aref alpha i q) prob))
- finally (return alpha)))
+ (setf (aref scales i) (scale alpha i))
+ finally (return (values alpha scales))))
+
+(defun scale (matrix index)
+ (let ((dimen (array-dimension matrix 1))
+ (sum 0.0))
+ (loop for i below dimen
+ do (incf sum (aref matrix index i)))
+ (loop with scale = (/ 1 sum)
+ for i below dimen
+ for val = (aref matrix index i)
+ do (setf (aref matrix index i) (* val scale))
+ finally (return scale))))
; vim: ts=2:sw=2

0 comments on commit 1ce087e

Please sign in to comment.