hivemall-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From myui <...@git.apache.org>
Subject [GitHub] incubator-hivemall pull request #71: [WIP][HIVEMALL-74] Implement pLSA
Date Fri, 21 Apr 2017 14:24:09 GMT
Github user myui commented on a diff in the pull request:

    https://github.com/apache/incubator-hivemall/pull/71#discussion_r112696086
  
    --- Diff: core/src/main/java/hivemall/topicmodel/IncrementalPLSAModel.java ---
    @@ -0,0 +1,300 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package hivemall.topicmodel;
    +
    +import hivemall.model.FeatureValue;
    +import hivemall.utils.lang.ArrayUtils;
    +
    +import java.util.Arrays;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Random;
    +import java.util.SortedMap;
    +import java.util.TreeMap;
    +
    +import javax.annotation.Nonnegative;
    +import javax.annotation.Nonnull;
    +
    +public final class IncrementalPLSAModel {
    +
    +    // ---------------------------------
    +    // HyperParameters
    +
    +    // number of topics
    +    private final int _K;
    +
    +    // control how much P(w|z) update is affected by the last value
    +    private final float _alpha;
    +
    +    // check convergence of P(w|z) for a document
    +    private final double _delta;
    +
    +    // ---------------------------------
    +
    +    // random number generator
    +    @Nonnull
    +    private final Random _rnd;
    +
    +    // optimized in the E step
    +    private List<Map<String, float[]>> _p_dwz; // P(z|d,w) probability of
topics for each document-label pair
    +
    +    // optimized in the M step
    +    @Nonnull
    +    private List<float[]> _p_dz; // P(z|d) probability of topics for documents
    +    private Map<String, float[]> _p_zw; // P(w|z) probability of labels for each
topic
    +
    +    @Nonnull
    +    private final List<Map<String, Float>> _miniBatchDocs;
    +    private int _miniBatchSize;
    +
    +    public IncrementalPLSAModel(int K, float alpha, double delta) {
    +        this._K = K;
    +        this._alpha = alpha;
    +        this._delta = delta;
    +
    +        this._rnd = new Random(1001);
    +
    +        this._p_zw = new HashMap<String, float[]>();
    +
    +        this._miniBatchDocs = new ArrayList<Map<String, Float>>();
    +    }
    +
    +    public void train(@Nonnull final String[][] miniBatch) {
    +        initMiniBatch(miniBatch, _miniBatchDocs);
    +
    +        this._miniBatchSize = _miniBatchDocs.size();
    +
    +        initParams();
    +
    +        final List<float[]> pPrev_dz = new ArrayList<float[]>();
    +
    +        for (int d = 0; d < _miniBatchSize; d++) {
    +            do {
    +                pPrev_dz.clear();
    +                pPrev_dz.addAll(_p_dz);
    +
    +                // Expectation
    +                eStep(d);
    +
    +                // Maximization
    +                mStep(d);
    +            } while (!isPdzConverged(d, pPrev_dz, _p_dz)); // until get stable value
of P(z|d)
    +        }
    +    }
    +
    +    private static void initMiniBatch(@Nonnull final String[][] miniBatch,
    +            @Nonnull final List<Map<String, Float>> docs) {
    +        docs.clear();
    +
    +        final FeatureValue probe = new FeatureValue();
    +
    +        // parse document
    +        for (final String[] e : miniBatch) {
    +            if (e == null || e.length == 0) {
    +                continue;
    +            }
    +
    +            final Map<String, Float> doc = new HashMap<String, Float>();
    +
    +            // parse features
    +            for (String fv : e) {
    +                if (fv == null) {
    +                    continue;
    +                }
    +                FeatureValue.parseFeatureAsString(fv, probe);
    +                String label = probe.getFeatureAsString();
    +                float value = probe.getValueAsFloat();
    +                doc.put(label, Float.valueOf(value));
    +            }
    +
    +            docs.add(doc);
    +        }
    +    }
    +
    +    private void initParams() {
    +        final List<float[]> p_dz = new ArrayList<float[]>();
    +        final List<Map<String, float[]>> p_dwz = new ArrayList<Map<String,
float[]>>();
    +
    +        for (int d = 0; d < _miniBatchSize; d++) {
    +            // init P(z|d)
    +            float[] p_dz_d = ArrayUtils.newRandomFloatArray(_K, _rnd);
    +            ArrayUtils.normalize(p_dz_d);
    --- End diff --
    
    `normalize` should be moved to `MathUtils`, not a normal array operation.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message