{"action":"created","comment":{"url":"https://api.github.com/repos/apache/flink/pulls/comments/45607404","id":45607404,"diff_hunk":"@@ -0,0 +1,148 @@\n+/*\n+ * Licensed to the Apache Software Foundation (ASF) under one\n+ * or more contributor license agreements.  See the NOTICE file\n+ * distributed with this work for additional information\n+ * regarding copyright ownership.  The ASF licenses this file\n+ * to you under the Apache License, Version 2.0 (the\n+ * \"License\"); you may not use this file except in compliance\n+ * with the License.  You may obtain a copy of the License at\n+ *\n+ *     http://www.apache.org/licenses/LICENSE-2.0\n+ *\n+ * Unless required by applicable law or agreed to in writing, software\n+ * distributed under the License is distributed on an \"AS IS\" BASIS,\n+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+ * See the License for the specific language governing permissions and\n+ * limitations under the License.\n+ */\n+\n+package org.apache.flink.ml.classification\n+\n+import org.apache.flink.api.scala.DataSet\n+\n+import org.apache.flink.api.scala._\n+import org.apache.flink.ml.math.{DenseMatrix, DenseVector}\n+import org.apache.flink.ml.optimization.BaumWelch\n+import org.apache.flink.util.Collector\n+import org.slf4j.LoggerFactory\n+\n+class HMM {\n+\n+  /**\n+   * initial probabilities of the states\n+   */\n+  var initialProbabilities: DenseVector = null\n+\n+\n+  /**\n+   * transition probabilities\n+   */\n+  var transitionProbabilities: DenseMatrix = null\n+\n+\n+  /**\n+   * emission probabilities\n+   */\n+  var emissionProbabilities: DenseMatrix = null\n+\n+  val LOG = LoggerFactory.getLogger(this.getClass)\n+\n+  /**\n+   *\n+   * Initializes the parameters of the HMM with labeled training data\n+   *\n+   * @param trainingData labeled training data with the first element of the tuple as\n+   *                     the observation and the second element as the label\n+   * @param numberOfStates number of states\n+   * @param numberOfObVocab number of the observation vocabulary\n+   */\n+  def initializeParameters(trainingData: DataSet[Array[Tuple2[Int, Int]]],\n+                           numberOfStates: Int, numberOfObVocab: Int) = {\n+\n+    val params =\n+\n+      trainingData.flatMap((data, collector: Collector[(String, Int, Int, Long)]) => {\n+\n+      var transitions : Map[String, Long] = Map.empty[String, Long].withDefaultValue(0)\n+      var emissions : Map[String, Long] = Map.empty[String, Long].withDefaultValue(0)\n+\n+      var currentOb = data(0)\n+\n+      //xi -> yi\n+      var emission: String = currentOb._2 + \"->\" + currentOb._1\n+\n+      emissions += emission -> 1\n+\n+      val initialState = (\"i\", currentOb._2, 0, 1L)\n+\n+      for (i <- 1 until data.length) {\n+        val nextOb= data(i)\n+        val transition = currentOb._2 + \"->\" + nextOb._2\n+        currentOb = nextOb\n+        emission = currentOb._2 + \"->\" + currentOb._1\n+        transitions += (transition -> (transitions(transition) + 1L))\n+        emissions += (emission -> (emissions(emission) + 1L))\n+      }\n+\n+      //emit initial state\n+      collector.collect(initialState)\n+\n+      //emit transitions\n+      transitions.foreach(t => {val split = t._1.split(\"->\");\n+\n+        collector.collect((\"t\", split(0).toInt,split(1).toInt, t._2))})\n+\n+        //emit emissions\n+       emissions.foreach(e => {val split = e._1.split(\"->\");\n+       collector.collect((\"e\", split(0).toInt, split(1).toInt, e._2))})\n+    })\n+        //group each tuple types and sum them up\n+      .groupBy(0, 1,2).sum(3).map(tuple => (tuple._1, tuple._2,\n+        List((tuple._3, tuple._4)), tuple._4))\n+        .groupBy(0, 1).reduce((tuple1, tuple2) =>\n+        (tuple1._1, tuple1._2, tuple1._3 ::: tuple2._3, tuple1._4 + tuple2._4)).collect()\n+\n+\n+    initialProbabilities = new DenseVector(new Array[Double](numberOfStates))\n+    transitionProbabilities = new DenseMatrix(numberOfStates, numberOfStates,\n+      new Array[Double](numberOfStates * numberOfStates))\n+    emissionProbabilities = new DenseMatrix(numberOfStates, numberOfObVocab,\n+      new Array[Double](numberOfStates * numberOfObVocab))\n+\n+\n+    val numberOfTrainingInstances: Long = trainingData.count()","path":"flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/classification/HMM.scala","position":113,"original_position":113,"commit_id":"f744236cb10f70dd85293be5f8a411b4e2bcb59d","original_commit_id":"f744236cb10f70dd85293be5f8a411b4e2bcb59d","user":{"login":"tillrohrmann","id":5756858,"avatar_url":"https://avatars.githubusercontent.com/u/5756858?v=3","gravatar_id":"","url":"https://api.github.com/users/tillrohrmann","html_url":"https://github.com/tillrohrmann","followers_url":"https://api.github.com/users/tillrohrmann/followers","following_url":"https://api.github.com/users/tillrohrmann/following{/other_user}","gists_url":"https://api.github.com/users/tillrohrmann/gists{/gist_id}","starred_url":"https://api.github.com/users/tillrohrmann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/tillrohrmann/subscriptions","organizations_url":"https://api.github.com/users/tillrohrmann/orgs","repos_url":"https://api.github.com/users/tillrohrmann/repos","events_url":"https://api.github.com/users/tillrohrmann/events{/privacy}","received_events_url":"https://api.github.com/users/tillrohrmann/received_events","type":"User","site_admin":false},"body":"`count` triggers another execution. This means another pass over all data.","created_at":"2015-11-23T14:22:59Z","updated_at":"2015-11-23T14:22:59Z","html_url":"https://github.com/apache/flink/pull/1350#discussion_r45607404","pull_request_url":"https://api.github.com/repos/apache/flink/pulls/1350","_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/comments/45607404"},"html":{"href":"https://github.com/apache/flink/pull/1350#discussion_r45607404"},"pull_request":{"href":"https://api.github.com/repos/apache/flink/pulls/1350"}}},"pull_request":{"url":"https://api.github.com/repos/apache/flink/pulls/1350","id":50523586,"html_url":"https://github.com/apache/flink/pull/1350","diff_url":"https://github.com/apache/flink/pull/1350.diff","patch_url":"https://github.com/apache/flink/pull/1350.patch","issue_url":"https://api.github.com/repos/apache/flink/issues/1350","number":1350,"state":"open","locked":false,"title":"[Flink-3007] Implemented a parallel version of Hidden Markov Models. It uses a par…","user":{"login":"HilmiYildirim","id":10813604,"avatar_url":"https://avatars.githubusercontent.com/u/10813604?v=3","gravatar_id":"","url":"https://api.github.com/users/HilmiYildirim","html_url":"https://github.com/HilmiYildirim","followers_url":"https://api.github.com/users/HilmiYildirim/followers","following_url":"https://api.github.com/users/HilmiYildirim/following{/other_user}","gists_url":"https://api.github.com/users/HilmiYildirim/gists{/gist_id}","starred_url":"https://api.github.com/users/HilmiYildirim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HilmiYildirim/subscriptions","organizations_url":"https://api.github.com/users/HilmiYildirim/orgs","repos_url":"https://api.github.com/users/HilmiYildirim/repos","events_url":"https://api.github.com/users/HilmiYildirim/events{/privacy}","received_events_url":"https://api.github.com/users/HilmiYildirim/received_events","type":"User","site_admin":false},"body":"Implemented a parallel version of Hidden Markov Models. It uses a parallel version of the Baum-Welch algorithm to train the model and uses the Viterbi algorithm to predict the sequence labels.\r\n\r\nThe prediction quality of my examples is low. The reason for this is probably the small size of the model prameters","created_at":"2015-11-12T15:22:16Z","updated_at":"2015-11-23T14:22:59Z","closed_at":null,"merged_at":null,"merge_commit_sha":"2ae3e3106be003967f8f317e7963d35c053a61eb","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/apache/flink/pulls/1350/commits","review_comments_url":"https://api.github.com/repos/apache/flink/pulls/1350/comments","review_comment_url":"https://api.github.com/repos/apache/flink/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apache/flink/issues/1350/comments","statuses_url":"https://api.github.com/repos/apache/flink/statuses/f744236cb10f70dd85293be5f8a411b4e2bcb59d","head":{"label":"HilmiYildirim:HMM","ref":"HMM","sha":"f744236cb10f70dd85293be5f8a411b4e2bcb59d","user":{"login":"HilmiYildirim","id":10813604,"avatar_url":"https://avatars.githubusercontent.com/u/10813604?v=3","gravatar_id":"","url":"https://api.github.com/users/HilmiYildirim","html_url":"https://github.com/HilmiYildirim","followers_url":"https://api.github.com/users/HilmiYildirim/followers","following_url":"https://api.github.com/users/HilmiYildirim/following{/other_user}","gists_url":"https://api.github.com/users/HilmiYildirim/gists{/gist_id}","starred_url":"https://api.github.com/users/HilmiYildirim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HilmiYildirim/subscriptions","organizations_url":"https://api.github.com/users/HilmiYildirim/orgs","repos_url":"https://api.github.com/users/HilmiYildirim/repos","events_url":"https://api.github.com/users/HilmiYildirim/events{/privacy}","received_events_url":"https://api.github.com/users/HilmiYildirim/received_events","type":"User","site_admin":false},"repo":{"id":35879964,"name":"flink","full_name":"HilmiYildirim/flink","owner":{"login":"HilmiYildirim","id":10813604,"avatar_url":"https://avatars.githubusercontent.com/u/10813604?v=3","gravatar_id":"","url":"https://api.github.com/users/HilmiYildirim","html_url":"https://github.com/HilmiYildirim","followers_url":"https://api.github.com/users/HilmiYildirim/followers","following_url":"https://api.github.com/users/HilmiYildirim/following{/other_user}","gists_url":"https://api.github.com/users/HilmiYildirim/gists{/gist_id}","starred_url":"https://api.github.com/users/HilmiYildirim/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/HilmiYildirim/subscriptions","organizations_url":"https://api.github.com/users/HilmiYildirim/orgs","repos_url":"https://api.github.com/users/HilmiYildirim/repos","events_url":"https://api.github.com/users/HilmiYildirim/events{/privacy}","received_events_url":"https://api.github.com/users/HilmiYildirim/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/HilmiYildirim/flink","description":"Mirror of Apache Flink","fork":true,"url":"https://api.github.com/repos/HilmiYildirim/flink","forks_url":"https://api.github.com/repos/HilmiYildirim/flink/forks","keys_url":"https://api.github.com/repos/HilmiYildirim/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/HilmiYildirim/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/HilmiYildirim/flink/teams","hooks_url":"https://api.github.com/repos/HilmiYildirim/flink/hooks","issue_events_url":"https://api.github.com/repos/HilmiYildirim/flink/issues/events{/number}","events_url":"https://api.github.com/repos/HilmiYildirim/flink/events","assignees_url":"https://api.github.com/repos/HilmiYildirim/flink/assignees{/user}","branches_url":"https://api.github.com/repos/HilmiYildirim/flink/branches{/branch}","tags_url":"https://api.github.com/repos/HilmiYildirim/flink/tags","blobs_url":"https://api.github.com/repos/HilmiYildirim/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/HilmiYildirim/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/HilmiYildirim/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/HilmiYildirim/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/HilmiYildirim/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/HilmiYildirim/flink/languages","stargazers_url":"https://api.github.com/repos/HilmiYildirim/flink/stargazers","contributors_url":"https://api.github.com/repos/HilmiYildirim/flink/contributors","subscribers_url":"https://api.github.com/repos/HilmiYildirim/flink/subscribers","subscription_url":"https://api.github.com/repos/HilmiYildirim/flink/subscription","commits_url":"https://api.github.com/repos/HilmiYildirim/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/HilmiYildirim/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/HilmiYildirim/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/HilmiYildirim/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/HilmiYildirim/flink/contents/{+path}","compare_url":"https://api.github.com/repos/HilmiYildirim/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/HilmiYildirim/flink/merges","archive_url":"https://api.github.com/repos/HilmiYildirim/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/HilmiYildirim/flink/downloads","issues_url":"https://api.github.com/repos/HilmiYildirim/flink/issues{/number}","pulls_url":"https://api.github.com/repos/HilmiYildirim/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/HilmiYildirim/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/HilmiYildirim/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/HilmiYildirim/flink/labels{/name}","releases_url":"https://api.github.com/repos/HilmiYildirim/flink/releases{/id}","created_at":"2015-05-19T12:12:28Z","updated_at":"2015-05-19T12:12:38Z","pushed_at":"2015-11-12T16:17:53Z","git_url":"git://github.com/HilmiYildirim/flink.git","ssh_url":"git@github.com:HilmiYildirim/flink.git","clone_url":"https://github.com/HilmiYildirim/flink.git","svn_url":"https://github.com/HilmiYildirim/flink","homepage":null,"size":65100,"stargazers_count":0,"watchers_count":0,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apache:master","ref":"master","sha":"2063fa12d520818569e5cbb88d4123716f2da249","user":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"repo":{"id":20587599,"name":"flink","full_name":"apache/flink","owner":{"login":"apache","id":47359,"avatar_url":"https://avatars.githubusercontent.com/u/47359?v=3","gravatar_id":"","url":"https://api.github.com/users/apache","html_url":"https://github.com/apache","followers_url":"https://api.github.com/users/apache/followers","following_url":"https://api.github.com/users/apache/following{/other_user}","gists_url":"https://api.github.com/users/apache/gists{/gist_id}","starred_url":"https://api.github.com/users/apache/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apache/subscriptions","organizations_url":"https://api.github.com/users/apache/orgs","repos_url":"https://api.github.com/users/apache/repos","events_url":"https://api.github.com/users/apache/events{/privacy}","received_events_url":"https://api.github.com/users/apache/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apache/flink","description":"Mirror of Apache Flink","fork":false,"url":"https://api.github.com/repos/apache/flink","forks_url":"https://api.github.com/repos/apache/flink/forks","keys_url":"https://api.github.com/repos/apache/flink/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apache/flink/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apache/flink/teams","hooks_url":"https://api.github.com/repos/apache/flink/hooks","issue_events_url":"https://api.github.com/repos/apache/flink/issues/events{/number}","events_url":"https://api.github.com/repos/apache/flink/events","assignees_url":"https://api.github.com/repos/apache/flink/assignees{/user}","branches_url":"https://api.github.com/repos/apache/flink/branches{/branch}","tags_url":"https://api.github.com/repos/apache/flink/tags","blobs_url":"https://api.github.com/repos/apache/flink/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apache/flink/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apache/flink/git/refs{/sha}","trees_url":"https://api.github.com/repos/apache/flink/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apache/flink/statuses/{sha}","languages_url":"https://api.github.com/repos/apache/flink/languages","stargazers_url":"https://api.github.com/repos/apache/flink/stargazers","contributors_url":"https://api.github.com/repos/apache/flink/contributors","subscribers_url":"https://api.github.com/repos/apache/flink/subscribers","subscription_url":"https://api.github.com/repos/apache/flink/subscription","commits_url":"https://api.github.com/repos/apache/flink/commits{/sha}","git_commits_url":"https://api.github.com/repos/apache/flink/git/commits{/sha}","comments_url":"https://api.github.com/repos/apache/flink/comments{/number}","issue_comment_url":"https://api.github.com/repos/apache/flink/issues/comments{/number}","contents_url":"https://api.github.com/repos/apache/flink/contents/{+path}","compare_url":"https://api.github.com/repos/apache/flink/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apache/flink/merges","archive_url":"https://api.github.com/repos/apache/flink/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apache/flink/downloads","issues_url":"https://api.github.com/repos/apache/flink/issues{/number}","pulls_url":"https://api.github.com/repos/apache/flink/pulls{/number}","milestones_url":"https://api.github.com/repos/apache/flink/milestones{/number}","notifications_url":"https://api.github.com/repos/apache/flink/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apache/flink/labels{/name}","releases_url":"https://api.github.com/repos/apache/flink/releases{/id}","created_at":"2014-06-07T07:00:10Z","updated_at":"2015-11-22T22:01:32Z","pushed_at":"2015-11-23T14:20:46Z","git_url":"git://github.com/apache/flink.git","ssh_url":"git@github.com:apache/flink.git","clone_url":"https://github.com/apache/flink.git","svn_url":"https://github.com/apache/flink","homepage":null,"size":85121,"stargazers_count":756,"watchers_count":756,"language":"Java","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":507,"mirror_url":"git://git.apache.org/incubator-flink.git","open_issues_count":76,"forks":507,"open_issues":76,"watchers":756,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apache/flink/pulls/1350"},"html":{"href":"https://github.com/apache/flink/pull/1350"},"issue":{"href":"https://api.github.com/repos/apache/flink/issues/1350"},"comments":{"href":"https://api.github.com/repos/apache/flink/issues/1350/comments"},"review_comments":{"href":"https://api.github.com/repos/apache/flink/pulls/1350/comments"},"review_comment":{"href":"https://api.github.com/repos/apache/flink/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apache/flink/pulls/1350/commits"},"statuses":{"href":"https://api.github.com/repos/apache/flink/statuses/f744236cb10f70dd85293be5f8a411b4e2bcb59d"}}}}