flink-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tillrohrmann <...@git.apache.org>
Subject [GitHub] flink pull request: [Flink-3007] Implemented a parallel version of...
Date Mon, 23 Nov 2015 14:18:01 GMT
Github user tillrohrmann commented on a diff in the pull request:

    https://github.com/apache/flink/pull/1350#discussion_r45606884
  
    --- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/HMM.scala
---
    @@ -0,0 +1,148 @@
    +/*
    + * 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 org.apache.flink.ml.classification
    +
    +import org.apache.flink.api.scala.DataSet
    +
    +import org.apache.flink.api.scala._
    +import org.apache.flink.ml.math.{DenseMatrix, DenseVector}
    +import org.apache.flink.ml.optimization.BaumWelch
    +import org.apache.flink.util.Collector
    +import org.slf4j.LoggerFactory
    +
    +class HMM {
    +
    +  /**
    +   * initial probabilities of the states
    +   */
    +  var initialProbabilities: DenseVector = null
    +
    +
    +  /**
    +   * transition probabilities
    +   */
    +  var transitionProbabilities: DenseMatrix = null
    +
    +
    +  /**
    +   * emission probabilities
    +   */
    +  var emissionProbabilities: DenseMatrix = null
    +
    +  val LOG = LoggerFactory.getLogger(this.getClass)
    +
    +  /**
    +   *
    +   * Initializes the parameters of the HMM with labeled training data
    +   *
    +   * @param trainingData labeled training data with the first element of the tuple as
    +   *                     the observation and the second element as the label
    +   * @param numberOfStates number of states
    +   * @param numberOfObVocab number of the observation vocabulary
    +   */
    +  def initializeParameters(trainingData: DataSet[Array[Tuple2[Int, Int]]],
    +                           numberOfStates: Int, numberOfObVocab: Int) = {
    +
    +    val params =
    +
    +      trainingData.flatMap((data, collector: Collector[(String, Int, Int, Long)]) =>
{
    +
    +      var transitions : Map[String, Long] = Map.empty[String, Long].withDefaultValue(0)
    +      var emissions : Map[String, Long] = Map.empty[String, Long].withDefaultValue(0)
    +
    +      var currentOb = data(0)
    +
    +      //xi -> yi
    +      var emission: String = currentOb._2 + "->" + currentOb._1
    +
    +      emissions += emission -> 1
    +
    +      val initialState = ("i", currentOb._2, 0, 1L)
    +
    +      for (i <- 1 until data.length) {
    +        val nextOb= data(i)
    +        val transition = currentOb._2 + "->" + nextOb._2
    +        currentOb = nextOb
    +        emission = currentOb._2 + "->" + currentOb._1
    +        transitions += (transition -> (transitions(transition) + 1L))
    +        emissions += (emission -> (emissions(emission) + 1L))
    --- End diff --
    
    Can't we calculate the aggregates in a parallel fashion by grouping on the run ID and
label and then applying a reduce, for example?


---
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