Github user myui commented on a diff in the pull request: https://github.com/apache/incubator-hivemall/pull/71#discussion_r112697171 --- 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); + p_dz.add(p_dz_d); + + final Map<String, float[]> p_dwz_d = new HashMap<String, float[]>(); + p_dwz.add(p_dwz_d); + + for (final String label : _miniBatchDocs.get(d).keySet()) { + // init P(z|d,w) + float[] p_dwz_dw = ArrayUtils.newRandomFloatArray(_K, _rnd); + ArrayUtils.normalize(p_dwz_dw); + p_dwz_d.put(label, p_dwz_dw); + + // insert new labels to P(w|z) + if (!_p_zw.containsKey(label)) { + float[] p_zw_w = ArrayUtils.newRandomFloatArray(_K, _rnd); + ArrayUtils.normalize(p_zw_w); + _p_zw.put(label, p_zw_w); + } + } + } + + this._p_dz = p_dz; + this._p_dwz = p_dwz; + } + + private void eStep(@Nonnegative final int d) { + final Map<String, float[]> p_dwz_d = _p_dwz.get(d); + final float[] p_dz_d = _p_dz.get(d); + + // update P(z|d,w) = P(z|d) * P(w|z) + for (final String label : _miniBatchDocs.get(d).keySet()) { + final float[] p_dwz_dw = p_dwz_d.get(label); + final float[] p_zw_w = _p_zw.get(label); + for (int z = 0; z < _K; z++) { + p_dwz_dw[z] = p_dz_d[z] * p_zw_w[z]; + } + ArrayUtils.normalize(p_dwz_dw); + } + } + + private void mStep(@Nonnegative final int d) { + final Map<String, Float> doc = _miniBatchDocs.get(d); + final Map<String, float[]> p_dwz_d = _p_dwz.get(d); + + // update P(z|d) = n(d,w) * P(z|d,w) + final float[] p_dz_d = _p_dz.get(d); + Arrays.fill(p_dz_d, 0.f); // zero-fill w/ keeping pointer to _p_dz.get(d) + for (Map.Entry<String, Float> e : doc.entrySet()) { + final float[] p_dwz_dw = p_dwz_d.get(e.getKey()); + final float n = e.getValue().floatValue(); + for (int z = 0; z < _K; z++) { + p_dz_d[z] += n * p_dwz_dw[z]; + } + } + ArrayUtils.normalize(p_dz_d); + + // update P(w|z) = n(d,w) * P(z|d,w) + alpha * P(w|z) + for (int z = 0; z < _K; z++) { + double npSumInDoc_zw_w = 0.d; // sum over the labels in the document + double pSumAll_zw_w = 0.d; // sum over the all existing labels + + for (Map.Entry<String, float[]> e : _p_zw.entrySet()) { + final String label = e.getKey(); + final float[] p_zw_w = e.getValue(); + + if (doc.containsKey(label)) { + p_zw_w[z] *= _alpha; // alpha * P(w|z) + float np = doc.get(label) * p_dwz_d.get(label)[z]; // n(d,w) * P(z|d,w) --- End diff -- Avoid autoboxing `doc.get(label)`. ```java Float label_value = doc.get(label); if(label_value == null) { .. } else { p_zw_w[z] *= _alpha; // alpha * P(w|z) float np = label_value.floatValue() * p_dwz_d.get(label)[z]; ```
--- 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 infrastruct...@apache.org or file a JIRA ticket with INFRA. ---