Skip to content
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 4 commits into from
Jan 13, 2024

Conversation

dineshdharme
Copy link
Contributor

Hello David Hall,

This PR adds support for doing Least Squares calculation for Float datatype.
Please review this.


  def NaiveMultiplicationFloat(n: Int, flag: Boolean): Unit = {


    val N_Dim = 4
    val rows = N_Dim + 5
    val columns = N_Dim
    val randomDoubleArray = Array.fill(rows * columns) {math.random}.map(ele => ele.toFloat)
    val sm: DenseMatrix[Float] = DenseMatrix.create(rows, columns, randomDoubleArray)

    val features2DMatrix = sm(::, 1 to -1)
    val targetPPNRVector = sm(::, 0)
   println("sm" +  sm)
    val result = leastSquares(features2DMatrix, targetPPNRVector)
    println("Breeze Linear Equation Solver results")


    println(s"intercept = ${result.coefficients.toArray.slice(1 , 5).mkString("[", ",", "]")}")
   println(s"r^2 = ${result.rSquared}")


  }

This code should allow you to test the Float datatype.

Copy link
Member

@dlwh dlwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @dineshdharme thanks for submitting this!

So, typically in Breeze we aim for compile time errors rather than runtime errors. Here, we would get a runtime error if we tried to use it for Complex or something. so rather than the match, it would be better if there were two implementations of each of matrixVector, matrixVectorSpecifiedWork and matrixVectorSpecifiedWork

@dineshdharme
Copy link
Contributor Author

Thank you, I will update the PR tomorrow and let you know.

@dineshdharme
Copy link
Contributor Author

Hi David,

I made changes as you requested. Please take a look and let me know.

Copy link
Member

@dlwh dlwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost there, thank you


def apply(x: DenseVector[T]): T = ev match {

case _: NumericType[Float] =>
Copy link
Member

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

def apply(x: DenseVector[T])(implicit can_dot: OpMulInner.Impl2[DenseVector[T], DenseVector[T], T]) = {
}

and then you can remove all of the switches and exception stuff.

case _ => throw new UnsupportedOperationException("Unsupported numeric type. Only Float and Double are supported")

}
def apply(X: DenseMatrix[T]): DenseVector[T] = ev match {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly

let's make this method something like

def apply(x: DenseVector[T])(implicit can_dot: OpMulMatrix.Impl2[DenseMatrix[T], DenseVector[T], T]) = {
}

and then you can remove all of the switches and exception stuff.

@dineshdharme
Copy link
Contributor Author

dineshdharme commented Jan 8, 2024

I made the changes you requested. The only difference is for first apply I had to move the implicit can_dot to case class
as scala didn't recognize it to be Function1's apply method.

Please review. thanks!

Copy link
Member

@dlwh dlwh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

@dlwh dlwh merged commit 5c0cd8c into scalanlp:master Jan 13, 2024
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants