コード例 #1
0
    def iterate(self, context):
        evidence, logLikTrain = context.trainSample.evidence(
            context.model.sequenceModel, self.shallUseMaximumApproximation)

        print(('LL train: %s (before)' % logLikTrain), file=context.log)
        context.logLikTrain.append(logLikTrain)

        if (not context.develSample) and (context.iteration >
                                          self.minIterations):
            context.registerNewModel(context.model, logLikTrain)

        order = evidence.maximumHistoryLength()
        print('  evidence order: %s' % order, file=context.log)
        if context.order is not None and order != context.order:
            print('  warning: evidence order changed from %d to %d!' %
                  (context.order, order),
                  file=context.log)
        context.order = order

        print('  evidence types: %s' % evidence.size(), file=context.log)
        print('  evidence total / max: %s / %s' %
              (evidence.total(), evidence.maximum()),
              file=context.log)
        self.showMostEvident(context.log, evidence, 10)  ### TESTING

        newModel = Model(self.sequitur)
        newModel.discount = context.discountAdjuster.adjust(
            context, evidence, order)
        newModel.sequenceModel = self.sequenceModel(evidence,
                                                    newModel.discount)
        print('  model size: %s' % newModel.sequenceModel.size(),
              file=context.log)

        if context.develSample:
            logLikDevel = context.develSample.logLik(
                newModel.sequenceModel, self.shallUseMaximumApproximation)
            print('LL devel: %s' % logLikDevel, file=context.log)
            context.logLikDevel.append(logLikDevel)

        for observer in self.observers:
            observer(context.log, context, newModel)

        if (context.develSample) and (context.iteration >= self.minIterations):
            context.registerNewModel(newModel, logLikDevel)

        shouldStop = False
        if context.bestModel:
            if context.develSample:
                crit = context.logLikDevel
            else:
                crit = context.logLikTrain
            crit = [-ll for ll in crit[-self.convergenceWindow:]]
            if not Minimization.hasSignificantDecrease(crit):
                print('iteration converged.', file=context.log)
                shouldStop = True

        context.model = newModel
        return shouldStop
コード例 #2
0
ファイル: sequitur.py プロジェクト: giuliopaci/sequitur-g2p
    def iterate(self, context):
        evidence, logLikTrain = context.trainSample.evidence(
            context.model.sequenceModel,
            self.shallUseMaximumApproximation)

        print >> context.log, 'LL train: %s (before)' % logLikTrain
        context.logLikTrain.append(logLikTrain)

        if (not context.develSample) and (context.iteration > self.minIterations):
            context.registerNewModel(context.model, logLikTrain)

        order = evidence.maximumHistoryLength()
        print >> context.log, '  evidence order: %s' % order
        if context.order is not None and order != context.order:
            print >> context.log, '  warning: evidence order changed from %d to %d!' % (context.order, order)
        context.order = order

        print >> context.log, '  evidence types: %s' % evidence.size()
        print >> context.log, '  evidence total / max: %s / %s' % (evidence.total(), evidence.maximum())
        self.showMostEvident(context.log, evidence, 10) ### TESTING

        newModel = Model(self.sequitur)
        newModel.discount = context.discountAdjuster.adjust(context, evidence, order)
        newModel.sequenceModel = self.sequenceModel(evidence, newModel.discount)
        print >> context.log, '  model size: %s' % newModel.sequenceModel.size()

        if context.develSample:
            logLikDevel = context.develSample.logLik(newModel.sequenceModel, self.shallUseMaximumApproximation)
            print >> context.log, 'LL devel: %s' % logLikDevel
            context.logLikDevel.append(logLikDevel)

        for observer in self.observers:
            observer(context.log, context, newModel)

        if (context.develSample) and (context.iteration >= self.minIterations):
            context.registerNewModel(newModel, logLikDevel)

        shouldStop = False
        if context.bestModel:
            if context.develSample:
                crit = context.logLikDevel
            else:
                crit = context.logLikTrain
            crit = [ -ll for ll in crit[-self.convergenceWindow:] ]
            if not Minimization.hasSignificantDecrease(crit):
                print >> context.log, 'iteration converged.'
                shouldStop = True

        context.model = newModel
        return shouldStop
コード例 #3
0
    def adjustHigherOrder(self, evidence, order, maximumDiscount):
        def criterion(discount):
            disc = tuple(num.maximum(0.0, discount))
            sm = self.modelFactory.sequenceModel(evidence, disc)
            ll = self.develSample.logLik(sm, self.shallUseMaximumApproximation)
            crit = - ll \
            - sum(num.minimum(discount, 0)) \
            + sum(num.maximum(discount - maximumDiscount, 0))
            print discount, ll, crit  # TESTING
            return crit

        initialGuess = self.discounts[-1]
        firstDirection = None
        if initialGuess is None:
            initialGuess = 0.1 * num.arange(1, order + 2, dtype=num.float64)
        elif len(initialGuess) < order + 1:
            oldGuess = initialGuess
            oldSize = len(initialGuess)
            initialGuess = num.zeros(order + 1, dtype=num.float64)
            initialGuess[:oldSize] = oldGuess
            initialGuess[oldSize:] = oldGuess[-1]
        elif len(initialGuess) > order + 1:
            initialGuess = initialGuess[:order + 1]
        else:
            previous = self.discounts[-2]
            if previous is not None and len(previous) == order + 1:
                firstDirection = initialGuess - previous
                if not num.sometrue(num.abs(firstDirection) > 1e-4):
                    firstDirection = None

        directions = num.identity(order + 1, dtype=num.float64)
        directions = directions[::-1]
        if firstDirection is not None:
            directions = num.concatenate(
                (firstDirection[num.newaxis, :], directions))
        directions *= 0.1
        print directions  # TESTING

        discount, ll = Minimization.directionSetMinimization(criterion,
                                                             initialGuess,
                                                             directions,
                                                             tolerance=1e-4)

        discount = num.maximum(0.0, discount)
        return discount, -ll
コード例 #4
0
ファイル: sequitur.py プロジェクト: giuliopaci/sequitur-g2p
    def adjustHigherOrder(self, evidence, order, maximumDiscount):
        def criterion(discount):
            disc = tuple(num.maximum(0.0, discount))
            sm = self.modelFactory.sequenceModel(evidence, disc)
            ll = self.develSample.logLik(sm, self.shallUseMaximumApproximation)
            crit = - ll \
                   - sum(num.minimum(discount, 0)) \
                   + sum(num.maximum(discount - maximumDiscount, 0))
            print discount, ll, crit # TESTING
            return crit

        initialGuess = self.discounts[-1]
        firstDirection = None
        if initialGuess is None:
            initialGuess = 0.1 * num.arange(1, order+2, dtype=num.float64)
        elif len(initialGuess) < order+1:
            oldGuess = initialGuess
            oldSize = len(initialGuess)
            initialGuess = num.zeros(order+1, dtype=num.float64)
            initialGuess[:oldSize] = oldGuess
            initialGuess[oldSize:] = oldGuess[-1]
        elif len(initialGuess) > order+1:
            initialGuess = initialGuess[:order+1]
        else:
            previous = self.discounts[-2]
            if previous is not None and len(previous) == order+1:
                firstDirection = initialGuess - previous
                if not num.sometrue(num.abs(firstDirection) > 1e-4):
                    firstDirection = None

        directions = num.identity(order+1, dtype=num.float64)
        directions = directions[::-1]
        if firstDirection is not None:
            directions = num.concatenate((firstDirection[num.newaxis,:], directions))
        directions *= 0.1
        print directions # TESTING

        discount, ll = Minimization.directionSetMinimization(
            criterion, initialGuess, directions, tolerance=1e-4)

        discount = num.maximum(0.0, discount)
        return discount, -ll
コード例 #5
0
ファイル: sequitur.py プロジェクト: giuliopaci/sequitur-g2p
    def adjustOrderZero(self, evidence, maximumDiscount):
        def criterion(discount):
            sm = self.modelFactory.sequenceModel(evidence, [max(0.0, discount)])
            ll = self.develSample.logLik(sm, self.shallUseMaximumApproximation)
            crit = - ll - min(discount, 0) + max(discount - maximumDiscount, 0)
            print discount, ll, crit # TESTING
            return crit

        initialGuess = self.discounts[-1]
        previous = self.discounts[-2]
        if initialGuess is None:
            initialGuess = 0.1
        else:
            initialGuess = initialGuess[0]

        discount, ll = Minimization.linearMinimization(
            criterion, initialGuess, tolerance=1e-4)
        discount = max(0.0, discount)
        discount = num.array([discount])
        return discount, -ll