-
Notifications
You must be signed in to change notification settings - Fork 690
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
single precision support added for LeastSquares calculation #857
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a1273f5
single precision support added for LeastSquares calculation
dineshdharme 2b1dedd
separate methods for float and double
dineshdharme 87ae865
LeastSquaresRegressionResult has been modified to properly handle dou…
dineshdharme 5842e7b
removed unnecessary switches and exception handling
dineshdharme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,27 @@ | ||
package breeze.stats.regression | ||
|
||
|
||
import breeze.generic.UFunc | ||
import breeze.linalg._ | ||
import org.netlib.util.intW | ||
import dev.ludovic.netlib.lapack.LAPACK.{getInstance => lapack} | ||
|
||
import java.util.Arrays | ||
|
||
|
||
trait NumericType[T] | ||
|
||
object NumericType { | ||
implicit object FloatIsNumeric extends NumericType[Float] | ||
implicit object DoubleIsNumeric extends NumericType[Double] | ||
} | ||
private object leastSquaresImplementation { | ||
def doLeastSquares( | ||
|
||
|
||
def doLeastSquaresDouble( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double], | ||
workArray: Array[Double]): LeastSquaresRegressionResult = { | ||
workArray: Array[Double]): LeastSquaresRegressionResult[Double] = { | ||
require(data.rows == outputs.size) | ||
require(data.rows > data.cols + 1) | ||
require(workArray.length >= 2 * data.rows * data.cols) | ||
|
@@ -37,69 +48,203 @@ private object leastSquaresImplementation { | |
for (i <- 0 until (data.rows - data.cols)) { | ||
r2 = r2 + math.pow(outputs.data(data.cols + i), 2) | ||
} | ||
LeastSquaresRegressionResult(coefficients, r2) | ||
LeastSquaresRegressionResult[Double](coefficients, r2) | ||
} | ||
|
||
def doLeastSquaresFloat( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float], | ||
workArray: Array[Float]): LeastSquaresRegressionResult[Float] = { | ||
require(data.rows == outputs.size) | ||
require(data.rows > data.cols + 1) | ||
require(workArray.length >= 2 * data.rows * data.cols) | ||
|
||
val info = new intW(0) | ||
lapack.sgels( | ||
"N", | ||
data.rows, | ||
data.cols, | ||
1, | ||
data.data, | ||
data.rows, | ||
outputs.data, | ||
data.rows, | ||
workArray, | ||
workArray.length, | ||
info) | ||
if (info.`val` < 0) { | ||
throw new ArithmeticException("Least squares did not converge.") | ||
} | ||
|
||
val coefficients = new DenseVector[Float](Arrays.copyOf(outputs.data, data.cols)) | ||
var r2 = 0.0 | ||
for (i <- 0 until (data.rows - data.cols)) { | ||
r2 = r2 + math.pow(outputs.data(data.cols + i), 2) | ||
} | ||
LeastSquaresRegressionResult[Float](coefficients, r2.toFloat) | ||
} | ||
} | ||
|
||
case class LeastSquaresRegressionResult(coefficients: DenseVector[Double], rSquared: Double) | ||
extends RegressionResult[DenseVector[Double], Double] { | ||
def apply(x: DenseVector[Double]): Double = coefficients.dot(x) | ||
|
||
def apply(X: DenseMatrix[Double]): DenseVector[Double] = X * coefficients | ||
|
||
|
||
|
||
case class LeastSquaresRegressionResult[T](coefficients: DenseVector[T], rSquared: T)(implicit ev: NumericType[T]) | ||
extends RegressionResult[DenseVector[T], T] { | ||
|
||
def apply(x: DenseVector[T]): T = ev match { | ||
|
||
case _: NumericType[Float] => | ||
val coeffs = coefficients.asInstanceOf[DenseVector[Float]] | ||
val x_ins = x.asInstanceOf[DenseVector[Float]] | ||
(coeffs .dot(x_ins)).asInstanceOf[T] | ||
case _: NumericType[Double] => | ||
val coeffs = coefficients.asInstanceOf[DenseVector[Double]] | ||
val x_ins = x.asInstanceOf[DenseVector[Double]] | ||
(coeffs .dot(x_ins)).asInstanceOf[T] | ||
case _ => throw new UnsupportedOperationException("Unsupported numeric type. Only Float and Double are supported") | ||
|
||
} | ||
def apply(X: DenseMatrix[T]): DenseVector[T] = ev match { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly let's make this method something like
and then you can remove all of the switches and exception stuff. |
||
case _: NumericType[Float] => | ||
val coeffs = coefficients.asInstanceOf[DenseVector[Float]] | ||
val mat = X.asInstanceOf[DenseMatrix[Float]] | ||
(mat * coeffs).asInstanceOf[DenseVector[T]] | ||
|
||
case _: NumericType[Double] => | ||
val coeffs = coefficients.asInstanceOf[DenseVector[Double]] | ||
val mat = X.asInstanceOf[DenseMatrix[Double]] | ||
(mat * coeffs).asInstanceOf[DenseVector[T]] | ||
|
||
case _ => throw new UnsupportedOperationException("Unsupported numeric type. Only Float and Double are supported") | ||
} | ||
} | ||
|
||
|
||
object leastSquares extends UFunc { | ||
implicit val matrixVectorWithWorkArray | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] { | ||
implicit val matrixVectorWithWorkArrayDouble | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult[Double]] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double], | ||
workArray: Array[Double]): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares(data.copy, outputs.copy, workArray) | ||
workArray: Array[Double]): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble(data.copy, outputs.copy, workArray) | ||
} | ||
|
||
implicit val matrixVectorSpecifiedWork | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] { | ||
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double], workSize: Int): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares(data.copy, outputs.copy, new Array[Double](workSize)) | ||
implicit val matrixVectorWithWorkArrayFloat | ||
: Impl3[DenseMatrix[Float], DenseVector[Float], Array[Float], LeastSquaresRegressionResult[Float]] = | ||
new Impl3[DenseMatrix[Float], DenseVector[Float], Array[Float], LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float], | ||
workArray: Array[Float]): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat(data.copy, outputs.copy, workArray) | ||
} | ||
|
||
implicit val matrixVector: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] = | ||
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] { | ||
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double]): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares( | ||
implicit val matrixVectorSpecifiedWorkDouble | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult[Double]] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double], | ||
workSize: Int): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble(data.copy, outputs.copy, new Array[Double](workSize)) | ||
} | ||
|
||
implicit val matrixVectorSpecifiedWorkFloat | ||
: Impl3[DenseMatrix[Float], DenseVector[Float], Int, LeastSquaresRegressionResult[Float]] = | ||
new Impl3[DenseMatrix[Float], DenseVector[Float], Int, LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float], | ||
workSize: Int): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat(data.copy, outputs.copy, new Array[Float](workSize)) | ||
} | ||
|
||
implicit val matrixVectorDouble: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult[Double]] = | ||
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double]): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble( | ||
data.copy, | ||
outputs.copy, | ||
new Array[Double](math.max(1, data.rows * data.cols * 2))) | ||
} | ||
|
||
implicit val matrixVectorFloat: Impl2[DenseMatrix[Float], DenseVector[Float], LeastSquaresRegressionResult[Float]] = | ||
new Impl2[DenseMatrix[Float], DenseVector[Float], LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float]): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat( | ||
data, | ||
outputs, | ||
new Array[Float](math.max(1, data.rows * data.cols * 2))) | ||
} | ||
} | ||
|
||
object leastSquaresDestructive extends UFunc { | ||
implicit val matrixVectorWithWorkArray | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] { | ||
implicit val matrixVectorWithWorkArrayDouble | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult[Double]] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double], | ||
workArray: Array[Double]): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares(data, outputs, workArray) | ||
workArray: Array[Double]): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble(data, outputs, workArray) | ||
} | ||
|
||
implicit val matrixVectorSpecifiedWork | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] { | ||
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double], workSize: Int): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares(data, outputs, new Array[Double](workSize)) | ||
implicit val matrixVectorWithWorkArrayFloat | ||
: Impl3[DenseMatrix[Float], DenseVector[Float], Array[Float], LeastSquaresRegressionResult[Float]] = | ||
new Impl3[DenseMatrix[Float], DenseVector[Float], Array[Float], LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float], | ||
workArray: Array[Float]): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat(data, outputs, workArray) | ||
} | ||
|
||
implicit val matrixVector: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] = | ||
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] { | ||
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double]): LeastSquaresRegressionResult = | ||
leastSquaresImplementation.doLeastSquares( | ||
implicit val matrixVectorSpecifiedWorkDouble | ||
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult[Double]] = | ||
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double], | ||
workSize: Int): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble(data, outputs, new Array[Double](workSize)) | ||
} | ||
|
||
implicit val matrixVectorSpecifiedWorkFloat | ||
: Impl3[DenseMatrix[Float], DenseVector[Float], Int, LeastSquaresRegressionResult[Float]] = | ||
new Impl3[DenseMatrix[Float], DenseVector[Float], Int, LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float], | ||
workSize: Int): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat(data, outputs, new Array[Float](workSize)) | ||
} | ||
|
||
implicit val matrixVectorDouble: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult[Double]] = | ||
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult[Double]] { | ||
def apply( | ||
data: DenseMatrix[Double], | ||
outputs: DenseVector[Double]): LeastSquaresRegressionResult[Double] = | ||
leastSquaresImplementation.doLeastSquaresDouble( | ||
data, | ||
outputs, | ||
new Array[Double](math.max(1, data.rows * data.cols * 2))) | ||
} | ||
|
||
implicit val matrixVectorFloat: Impl2[DenseMatrix[Float], DenseVector[Float], LeastSquaresRegressionResult[Float]] = | ||
new Impl2[DenseMatrix[Float], DenseVector[Float], LeastSquaresRegressionResult[Float]] { | ||
def apply( | ||
data: DenseMatrix[Float], | ||
outputs: DenseVector[Float]): LeastSquaresRegressionResult[Float] = | ||
leastSquaresImplementation.doLeastSquaresFloat( | ||
data, | ||
outputs, | ||
new Array[Float](math.max(1, data.rows * data.cols * 2))) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's make this method something like
and then you can remove all of the switches and exception stuff.