Random generator equivalent to np.random.randn?

51 views
Skip to first unread message

marko

unread,
Mar 2, 2018, 5:22:15 PM3/2/18
to Scala Breeze
Hi,

I've some NumPy code that I'm migrating to Breeze and would like to be able to compare the results.
For this reason I'd like to replicate the results of randomly generated matrices. I've the following NumPy code:

np.random.seed
np.random.randn

Is there a way to get results equivalent to the code above with Breeze?


marko

David Hall

unread,
Mar 2, 2018, 6:04:14 PM3/2/18
to scala-...@googlegroups.com
DenseMatrix.rand[Double](rows, cols, Rand.gaussian) for unseeded.

with a seed:

val myRand = RandBasis.withSeed(seed)
DenseMatrix.rand[Double](rows, cols, myRand.gaussian)

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-breeze/cf7c9253-7b5d-4204-afb0-c4313da4a54c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

marko

unread,
Mar 3, 2018, 3:19:09 AM3/3/18
to Scala Breeze
Thanks!

It seems that the random generator doesn't work exactly the same as NumPy.
I get different results from the following pieces of code:

np.random.seed(1)
np
.random.randn(4, 4)

and

val myRand = RandBasis.withSeed(1)
DenseMatrix.rand[Double](4, 4, myRand.gaussian)

Can I somehow get Breeze random generator to generate the same random stream as NumPy?
I've some bugs in my migrated code and would like to be able to use exactly the same
input for troubleshooting. Another option would be to use hardcoded test sets, but random generation with the
same seed would make things simpler.

David Hall

unread,
Mar 5, 2018, 1:57:22 AM3/5/18
to scala-...@googlegroups.com
I don't think so, sorry.

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.

marko

unread,
Mar 8, 2018, 3:54:12 PM3/8/18
to Scala Breeze

Still, I'm puzzled by an issue that seems to be related to random initilisation.
I've two different NN implementations, 1 with Breeze and one with NumPy.
When using the exact same initialisation parameters I get the same
cost after each training iteration from each implementation. So, based on this
I'd infer that the implementations work equivalently.
However, the results look very different when using random initialisation.
With respect to exact cost this is of course expected, but what I find troublesome
is that after N training iterations the cost starts approaching zero with the NumPy
code (most of of the time), whereas with the Breeze based implementation cost fails
to converge (most of the time).

With NumPy I'm simply using the following random initialisation code:

np.random.randn(n_h, n_x) * 0.01

I'm trying to emulate the same behaviour in my Scala code by sampling from a
Gaussian distribution with mean = 0 and std dev = 1 (then multiply with 0.01) as follows:

val RandSampler = breeze.stats.distributions.Rand.gaussian
DenseMatrix.rand[Double](d1, d2, RandSampler) * 0.01

Any ideas why the initialisation seem to work differently?

David Hall

unread,
Mar 14, 2018, 5:34:09 PM3/14/18
to scala-...@googlegroups.com
Sorry, I guess I forgot to respond to this. The underlying random number generators are different: they're definitely seeded with different seeds when used that way, and they probably don't implement exactly the same algorithm that uses the seed in exactly the same way

--
You received this message because you are subscribed to the Google Groups "Scala Breeze" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-breeze+unsubscribe@googlegroups.com.
To post to this group, send email to scala-...@googlegroups.com.

marko

unread,
Mar 14, 2018, 5:52:39 PM3/14/18
to Scala Breeze
Thanks for your reply!

The main problem was just that cost didn't converge during training with the
Breeze-based NN implementation while the NumPy-based one seemed to be working correctly.
I would've liked to be able to compare the results by using the exact same NN initialization.
I ended up serializing NN parameters on disk in order to use the same parameters with both implementations.

It turned out I had a bug in my initialization code - it's working correctly now.
It would've been very convenient, though, if I could've used the exact same random initialization
method with both code bases to compare the results.
Reply all
Reply to author
Forward
0 new messages