You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by tillrohrmann <gi...@git.apache.org> on 2015/03/26 17:59:07 UTC
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
GitHub user tillrohrmann opened a pull request:
https://github.com/apache/flink/pull/539
[FLINK-1718] Adds sparse matrix and sparse vector
Adds a sparse matrix abstraction using CSC data representation. Adds a sparse vector abstraction using CSC data representation.
Adds convenience functions to cast a Flink matrix/vector into a Breeze matrix/vector and vice versa.
You can merge this pull request into a Git repository by running:
$ git pull https://github.com/tillrohrmann/flink sparseMatrix
Alternatively you can review and apply these changes as the patch at:
https://github.com/apache/flink/pull/539.patch
To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:
This closes #539
----
commit 4c18940bf14f376cdb339d908324e5f2cd4593ad
Author: Till Rohrmann <tr...@apache.org>
Date: 2015-03-25T14:27:58Z
[FLINK-1718] [ml] Adds sparse matrix and sparse vector types
commit 756b2a64643ec888ff9a45ae1a8565e642971996
Author: Till Rohrmann <tr...@apache.org>
Date: 2015-03-26T16:44:17Z
[ml] Adds convenience functions for Breeze matrix/vector conversion
[ml] Adds breeze to flink-dist LICENSE file
----
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:
https://github.com/apache/flink/pull/539
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27321365
--- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/math/SparseMatrix.scala ---
@@ -0,0 +1,235 @@
+/*
+ * 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.math
+
+import scala.util.Sorting
+
+/** Sparse matrix using the compressed sparse column (CSC) representation.
+ *
+ * More details concerning the compressed sparse column (CSC) representation can be found
+ * [http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29].
+ *
+ * @param numRows Number of rows
+ * @param numCols Number of columns
+ * @param rowIndices Array containing the row indices of non-zero entries
+ * @param colPtrs Array containing the starting offsets in data for each column
+ * @param data Array containing the non-zero entries in column-major order
+ */
+class SparseMatrix(
+ val numRows: Int,
+ val numCols: Int,
+ val rowIndices: Array[Int],
+ val colPtrs: Array[Int],
+ val data: Array[Double])
+ extends Matrix {
+
+ /** Element wise access function
+ *
+ * @param row row index
+ * @param col column index
+ * @return matrix entry at (row, col)
+ */
+ override def apply(row: Int, col: Int): Double = {
+
+ val index = locate(row, col)
+
+ if(index < 0){
+ 0
+ } else {
+ data(index)
+ }
+ }
+
+ def toDenseMatrix: DenseMatrix = {
+ val result = DenseMatrix.zeros(numRows, numCols)
+
+ for(row <- 0 until numRows; col <- 0 until numCols) {
+ result(row, col) = apply(row, col)
+ }
+
+ result
+ }
+
+ /** Element wise update function
+ *
+ * @param row row index
+ * @param col column index
+ * @param value value to set at (row, col)
+ */
+ override def update(row: Int, col: Int, value: Double): Unit = {
+ val index = locate(row, col)
+
+ if(index < 0) {
+ throw new IllegalArgumentException("Cannot update zero value of sparse matrix at index " +
+ s"($row, $col)")
+ } else {
+ data(index) = value
+ }
+ }
+
+ override def toString: String = {
+ val result = StringBuilder.newBuilder
+
+ result.append(s"SparseMatrix($numRows, $numCols)\n")
+
+ var columnIndex = 0
+
+ val fieldWidth = math.max(numRows, numCols).toString.length
+ val valueFieldWidth = data.map(_.toString.length).max + 2
+
+ for(index <- 0 until colPtrs.last) {
+ while(colPtrs(columnIndex + 1) <= index){
+ columnIndex += 1
+ }
+
+ val rowStr = rowIndices(index).toString
+ val columnStr = columnIndex.toString
+ val valueStr = data(index).toString
+
+ result.append("(" + " " * (fieldWidth - rowStr.length) + rowStr + "," +
+ " " * (fieldWidth - columnStr.length) + columnStr + ")")
+ result.append(" " * (valueFieldWidth - valueStr.length) + valueStr)
+ result.append("\n")
+ }
+
+ result.toString
+ }
+
+ private def locate(row: Int, col: Int): Int = {
+ require(0 <= row && row < numRows, s"Row $row is out of bounds [0, $numRows).")
+ require(0 <= col && col < numCols, s"Col $col is out of bounds [0, $numCols).")
--- End diff --
Well but actually the string interpolations do not costs anything, because the message parameter is called by-name. Meaning that it is only evaluated in case of a false requirement.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27307815
--- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/math/DenseVector.scala ---
@@ -67,7 +63,25 @@ case class DenseVector(val values: Array[Double]) extends Vector {
* @return Copy of the vector instance
*/
override def copy: Vector = {
- DenseVector(values.clone())
+ DenseVector(data.clone())
+ }
+
+ /** Updates the element at the given index with the provided value
+ *
+ * @param index
+ * @param value
+ */
+ override def update(index: Int, value: Double): Unit = {
+ require(0 <= index && index < data.length, s"Index $index is out of bounds " +
--- End diff --
Might be inefficient.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on the pull request:
https://github.com/apache/flink/pull/539#issuecomment-87060638
I combined some of the sanity checks of the matrix/vector accessors.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by hsaputra <gi...@git.apache.org>.
Github user hsaputra commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27256008
--- Diff: flink-staging/flink-ml/pom.xml ---
@@ -46,9 +46,9 @@
</dependency>
<dependency>
- <groupId>com.github.fommil.netlib</groupId>
--- End diff --
Just curious, why was this deleted?
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27321156
--- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/math/SparseMatrix.scala ---
@@ -0,0 +1,235 @@
+/*
+ * 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.math
+
+import scala.util.Sorting
+
+/** Sparse matrix using the compressed sparse column (CSC) representation.
+ *
+ * More details concerning the compressed sparse column (CSC) representation can be found
+ * [http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29].
+ *
+ * @param numRows Number of rows
+ * @param numCols Number of columns
+ * @param rowIndices Array containing the row indices of non-zero entries
+ * @param colPtrs Array containing the starting offsets in data for each column
+ * @param data Array containing the non-zero entries in column-major order
+ */
+class SparseMatrix(
+ val numRows: Int,
+ val numCols: Int,
+ val rowIndices: Array[Int],
+ val colPtrs: Array[Int],
+ val data: Array[Double])
+ extends Matrix {
+
+ /** Element wise access function
+ *
+ * @param row row index
+ * @param col column index
+ * @return matrix entry at (row, col)
+ */
+ override def apply(row: Int, col: Int): Double = {
+
+ val index = locate(row, col)
+
+ if(index < 0){
+ 0
+ } else {
+ data(index)
+ }
+ }
+
+ def toDenseMatrix: DenseMatrix = {
+ val result = DenseMatrix.zeros(numRows, numCols)
+
+ for(row <- 0 until numRows; col <- 0 until numCols) {
+ result(row, col) = apply(row, col)
+ }
+
+ result
+ }
+
+ /** Element wise update function
+ *
+ * @param row row index
+ * @param col column index
+ * @param value value to set at (row, col)
+ */
+ override def update(row: Int, col: Int, value: Double): Unit = {
+ val index = locate(row, col)
+
+ if(index < 0) {
+ throw new IllegalArgumentException("Cannot update zero value of sparse matrix at index " +
+ s"($row, $col)")
+ } else {
+ data(index) = value
+ }
+ }
+
+ override def toString: String = {
+ val result = StringBuilder.newBuilder
+
+ result.append(s"SparseMatrix($numRows, $numCols)\n")
+
+ var columnIndex = 0
+
+ val fieldWidth = math.max(numRows, numCols).toString.length
+ val valueFieldWidth = data.map(_.toString.length).max + 2
+
+ for(index <- 0 until colPtrs.last) {
+ while(colPtrs(columnIndex + 1) <= index){
+ columnIndex += 1
+ }
+
+ val rowStr = rowIndices(index).toString
+ val columnStr = columnIndex.toString
+ val valueStr = data(index).toString
+
+ result.append("(" + " " * (fieldWidth - rowStr.length) + rowStr + "," +
+ " " * (fieldWidth - columnStr.length) + columnStr + ")")
+ result.append(" " * (valueFieldWidth - valueStr.length) + valueStr)
+ result.append("\n")
+ }
+
+ result.toString
+ }
+
+ private def locate(row: Int, col: Int): Int = {
+ require(0 <= row && row < numRows, s"Row $row is out of bounds [0, $numRows).")
+ require(0 <= col && col < numCols, s"Col $col is out of bounds [0, $numCols).")
--- End diff --
That's right. I'll probably remove the checks and expect the user to know what he's doing.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on the pull request:
https://github.com/apache/flink/pull/539#issuecomment-88414948
FYI: Travis failed only for the last profile because the tests didn't start for some reason. Travis passed for my own repository, though.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27308257
--- Diff: flink-staging/flink-ml/src/main/scala/org/apache/flink/ml/math/SparseMatrix.scala ---
@@ -0,0 +1,235 @@
+/*
+ * 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.math
+
+import scala.util.Sorting
+
+/** Sparse matrix using the compressed sparse column (CSC) representation.
+ *
+ * More details concerning the compressed sparse column (CSC) representation can be found
+ * [http://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_column_.28CSC_or_CCS.29].
+ *
+ * @param numRows Number of rows
+ * @param numCols Number of columns
+ * @param rowIndices Array containing the row indices of non-zero entries
+ * @param colPtrs Array containing the starting offsets in data for each column
+ * @param data Array containing the non-zero entries in column-major order
+ */
+class SparseMatrix(
+ val numRows: Int,
+ val numCols: Int,
+ val rowIndices: Array[Int],
+ val colPtrs: Array[Int],
+ val data: Array[Double])
+ extends Matrix {
+
+ /** Element wise access function
+ *
+ * @param row row index
+ * @param col column index
+ * @return matrix entry at (row, col)
+ */
+ override def apply(row: Int, col: Int): Double = {
+
+ val index = locate(row, col)
+
+ if(index < 0){
+ 0
+ } else {
+ data(index)
+ }
+ }
+
+ def toDenseMatrix: DenseMatrix = {
+ val result = DenseMatrix.zeros(numRows, numCols)
+
+ for(row <- 0 until numRows; col <- 0 until numCols) {
+ result(row, col) = apply(row, col)
+ }
+
+ result
+ }
+
+ /** Element wise update function
+ *
+ * @param row row index
+ * @param col column index
+ * @param value value to set at (row, col)
+ */
+ override def update(row: Int, col: Int, value: Double): Unit = {
+ val index = locate(row, col)
+
+ if(index < 0) {
+ throw new IllegalArgumentException("Cannot update zero value of sparse matrix at index " +
+ s"($row, $col)")
+ } else {
+ data(index) = value
+ }
+ }
+
+ override def toString: String = {
+ val result = StringBuilder.newBuilder
+
+ result.append(s"SparseMatrix($numRows, $numCols)\n")
+
+ var columnIndex = 0
+
+ val fieldWidth = math.max(numRows, numCols).toString.length
+ val valueFieldWidth = data.map(_.toString.length).max + 2
+
+ for(index <- 0 until colPtrs.last) {
+ while(colPtrs(columnIndex + 1) <= index){
+ columnIndex += 1
+ }
+
+ val rowStr = rowIndices(index).toString
+ val columnStr = columnIndex.toString
+ val valueStr = data(index).toString
+
+ result.append("(" + " " * (fieldWidth - rowStr.length) + rowStr + "," +
+ " " * (fieldWidth - columnStr.length) + columnStr + ")")
+ result.append(" " * (valueFieldWidth - valueStr.length) + valueStr)
+ result.append("\n")
+ }
+
+ result.toString
+ }
+
+ private def locate(row: Int, col: Int): Int = {
+ require(0 <= row && row < numRows, s"Row $row is out of bounds [0, $numRows).")
+ require(0 <= col && col < numCols, s"Col $col is out of bounds [0, $numCols).")
--- End diff --
Many ops in this class seem to use this method. I'm not sure how expensive the string interpolation is.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by rmetzger <gi...@git.apache.org>.
Github user rmetzger commented on the pull request:
https://github.com/apache/flink/pull/539#issuecomment-86996068
Looks good to merge.
There are some performance improvements possible.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on the pull request:
https://github.com/apache/flink/pull/539#issuecomment-88123548
I rebased the code on the current master and unified some of the tests. When Travis confirms that everything works and if there are no objections, I'll merge the PR.
---
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.
---
[GitHub] flink pull request: [FLINK-1718] Adds sparse matrix and sparse vec...
Posted by tillrohrmann <gi...@git.apache.org>.
Github user tillrohrmann commented on a diff in the pull request:
https://github.com/apache/flink/pull/539#discussion_r27261446
--- Diff: flink-staging/flink-ml/pom.xml ---
@@ -46,9 +46,9 @@
</dependency>
<dependency>
- <groupId>com.github.fommil.netlib</groupId>
--- End diff --
Because Breeze depends on it and thus it is pulled transitivly.
On Mar 26, 2015 10:05 PM, "Henry Saputra" <no...@github.com> wrote:
> In flink-staging/flink-ml/pom.xml
> <https://github.com/apache/flink/pull/539#discussion_r27256008>:
>
> > @@ -46,9 +46,9 @@
> > </dependency>
> >
> > <dependency>
> > - <groupId>com.github.fommil.netlib</groupId>
>
> Just curious, why was this deleted?
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/apache/flink/pull/539/files#r27256008>.
>
---
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.
---