scalanlp / breeze

Breeze is a numerical processing library for Scala.
www.scalanlp.org
Apache License 2.0
3.44k stars 691 forks source link

Norm Matrix across axis #820

Closed eggie5 closed 3 years ago

eggie5 commented 3 years ago

How can I do the L2 norm of a matrix w/ breeze?

numpy

word_vecs...
normd_vecs = (word_vecs.T / np.linalg.norm(word_vecs, axis=1)).T #L2 normalize
return np.mean(normd_vecs, axis=0) #avg across axis 0

breeze

val word_vecs...
val normd_vecs = ??? // L2 normalize
val mean_pool = mean(normd_vecs(::,*)).t //avg across axis 0
dlwh commented 3 years ago

Hrm, the broadcast syntax is indeed broken and I'll endeavour to fix it, but this works fine:

normalize(dm, Axis._1)

On Mon, Aug 23, 2021 at 12:00 PM Alex Egg @.***> wrote:

How can I do the L2 norm of a matrix w/ breeze?

numpy

normd_vecs = (word_vecs.T / np.linalg.norm(word_vecs, axis=1)).T #L2 normalize return np.mean(normd_vecs, axis=0) #avg across axis 0

breeze

val word_vecs = ??? // L2 normalize val mean_pool = mean(normd_vecs(::,*)).t //avg across axis 0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/scalanlp/breeze/issues/820, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACLIKPWUF4SUNYORIBUNDT6KLFBANCNFSM5CVGA64A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

dlwh commented 3 years ago

also normalize(dm(::, *), 2.0) should work, at least in the 2.0 RC's

On Mon, Aug 23, 2021 at 2:44 PM David Hall @.***> wrote:

Hrm, the broadcast syntax is indeed broken and I'll endeavour to fix it, but this works fine:

normalize(dm, Axis._1)

On Mon, Aug 23, 2021 at 12:00 PM Alex Egg @.***> wrote:

How can I do the L2 norm of a matrix w/ breeze?

numpy

normd_vecs = (word_vecs.T / np.linalg.norm(word_vecs, axis=1)).T #L2 normalize return np.mean(normd_vecs, axis=0) #avg across axis 0

breeze

val word_vecs = ??? // L2 normalize val mean_pool = mean(normd_vecs(::,*)).t //avg across axis 0

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/scalanlp/breeze/issues/820, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAACLIKPWUF4SUNYORIBUNDT6KLFBANCNFSM5CVGA64A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&utm_campaign=notification-email .

dlwh commented 3 years ago

I don't see any way to trick Scala's implicits into making normalize(dm, Axis._1) to work without either breaking something else or another overhaul of implicit organization that I don't have the will to do