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

Cleaner/more efficient breeze.linalg.DenseVector -> scala.collection.immutable.Seq ? #452

Closed
xanderdunn opened this Issue Sep 30, 2015 · 2 comments

Comments

Projects
None yet
2 participants
@xanderdunn

xanderdunn commented Sep 30, 2015

I'm serializing a DenseVector[Float] to a Protobuf class Prediction, which takes scala.collection.immutable.Seq[Float]. This is what I'm currently doing:

  def serializeOutput(output: DenseVector[Float]) : Prediction = {
    val outputSeq = mutable.ArrayBuffer[Float]()
    for {value <- output} {
      outputSeq += value
    }
    Prediction(output.length, immutable.Seq(outputSeq: _*))
  }

It works, but it's not particularly pretty, efficient, or functional. I'm iterating through the entire DenseVector to create a mutable ArrayBuffer and then I'm making an immutable copy of that. I'd prefer to get rid of that mutable ArrayBuffer without sacrificing performance. I wanted to do this:

  def serializeOutput(output: DenseVector[Float]) : Prediction = {
    val outputSeq : immutable.Seq[Float] =
    (for {value <- output}
     yield value)(collection.breakOut)
    Prediction(output.length, outputSeq)
  }

However, this produces:

/local/dev/odin-core/common/src/main/scala/ai/osaro/nn/Hierarchy.scala:114: polymorphic expression cannot be instantiated to expected type;
 found   : [From, T, To]scala.collection.generic.CanBuildFrom[From,T,To]
 required: breeze.linalg.support.CanMapValues[breeze.linalg.DenseVector[Float],Float,Float,scala.collection.immutable.Seq[Float]]
     yield value)(collection.breakOut)

It looks like it's not implemented on the breeze side.

Anyone have better ideas?

@xanderdunn xanderdunn changed the title from Cleaner/more efficient breeze.linalg.DenseVector -> scala.collection.immutable.Seq to Cleaner/more efficient breeze.linalg.DenseVector -> scala.collection.immutable.Seq ? Sep 30, 2015

@dlwh

This comment has been minimized.

Show comment
Hide comment
@dlwh

dlwh Sep 30, 2015

Member

.toScalaVector should do what you want. it's not actually any more efficient, probably, but is it really your bottleneck?

Member

dlwh commented Sep 30, 2015

.toScalaVector should do what you want. it's not actually any more efficient, probably, but is it really your bottleneck?

@xanderdunn

This comment has been minimized.

Show comment
Hide comment
@xanderdunn

xanderdunn Sep 30, 2015

@dlwh Thanks a lot for that! I didn't see it listed in the docs for DenseVector.

I haven't actually measured this is a bottleneck, so mostly I was just looked for less messy implementation. toScalaVector does it!

xanderdunn commented Sep 30, 2015

@dlwh Thanks a lot for that! I didn't see it listed in the docs for DenseVector.

I haven't actually measured this is a bottleneck, so mostly I was just looked for less messy implementation. toScalaVector does it!

@xanderdunn xanderdunn closed this Sep 30, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment