I'm just starting to play with Theano, and am wondering why the first creation of a shared variable on the gpu seems to effect numpy's random number generator. At times this initial creation seems to advance the random number generator.
I've explored the following test cases in this code:
import numpy
import theano
from theano.compile.sharedvalue import shared
import theano.sandbox.cuda as tcn
def make_cpu_shared():
#Create, but don't return/use shared variable on cpu
shared(theano._asarray(numpy.asarray([.67]), dtype='float32'), 'cpu_shared')
return None
def make_gpu_shared():
#Create, but don't return/use shared variable on gpu
tcn.shared_constructor(theano._asarray(numpy.asarray([.67]), dtype='float32'), 'gpu_shared')
return None
def rand_test0():
#Match - Sanity check - Ensure numpy.random.seed creates repeatable random streams
numpy.random.seed(666) #Note: 666 seems to be seed used in Theano test suite
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
numpy.random.seed(666)
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
def rand_test1():
#Match - Show creation of shared variable on cpu has no effect on random streams
numpy.random.seed(666)
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
numpy.random.seed(666)
make_cpu_shared()
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
def rand_test2():
#No Match - Show creation of shared variable on gpu effects random streams
numpy.random.seed(666)
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
numpy.random.seed(666)
make_gpu_shared()
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
def rand_test3():
#Match - Show effect is only for initial creation of shared gpu variable
make_gpu_shared()
numpy.random.seed(666)
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
numpy.random.seed(666)
make_gpu_shared()
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
def rand_test4():
#No Match - Show initial creation of shared gpu variable effecting random streams
numpy.random.seed(666)
make_gpu_shared()
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
numpy.random.seed(666)
make_gpu_shared()
temp = numpy.random.rand(1)
print "temp[0]=",temp[0]
- rand_test0 - A sanity check to show I can reset a random stream using numpy.random.seed
- rand_test1 - Show creation of shared variable on cpu does nothing unexpected
- rand_test2 - Show creation of shared variable on gpu does have an unexpected effect
- rand_test3 - Show it is only the initial creation of the shared variable on the gpu with an unexpected effect
- rand_test4 - A verification of rand_test3
The results I got were as follows:
(Test 0 - Sanity Check)
me@Bedrock1:~/Projects/Theano/packageTests$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_rand_shared import *
>>> rand_test0()
temp[0]= 0.700437121858
temp[0]= 0.700437121858
>>>
(Test 1 - Shared on CPU OK)
me@Bedrock1:~/Projects/Theano/packageTests$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_rand_shared import *
>>> rand_test1()
temp[0]= 0.700437121858
temp[0]= 0.700437121858
>>>
(Test 2 - Shared on GPU effects random stream)
me@Bedrock1:~/Projects/Theano/packageTests$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_rand_shared import *
>>> rand_test2()
temp[0]= 0.700437121858
Using gpu device 0: GeForce GTX 670MX
temp[0]= 0.859992279406
>>>
(Test 3 - Only initial creation of shared variable on GPU effects random stream)
me@Bedrock1:~/Projects/Theano/packageTests$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_rand_shared import *
>>> rand_test3()
Using gpu device 0: GeForce GTX 670MX
temp[0]= 0.700437121858
temp[0]= 0.700437121858
>>>
(Test 4 - Variation on Test 3)
me@Bedrock1:~/Projects/Theano/packageTests$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from test_rand_shared import *
>>> rand_test4()
Using gpu device 0: GeForce GTX 670MX
temp[0]= 0.859992279406
temp[0]= 0.700437121858
>>>
Does this make sense to anyone? Is it a Theano artifact? Is it a CUDA artifact, caused by my initial access to the GPU (i.e. the fact that I was playing with shared variables is only incidental to what I'm seeing). Or, am I misunderstanding something else?
Aucun commentaire:
Enregistrer un commentaire