import theano
import theano.tensor as T
import numpy as np
from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams

floatX = theano.config.floatX

srng = RandomStreams()

x = T.matrix()
random_shared = theano.shared(np.empty((0, 0), dtype=floatX))

f = theano.function([x], updates=[(random_shared, srng.normal(x.shape))])

print(random_shared.get_value())
f(np.ones((4, 5), dtype=floatX))
print(random_shared.get_value())


On Monday, February 27, 2017 at 9:47:12 AM UTC+1, Asghar Inanlou Asl wrote:
>
> Yes, but that does not create a shared variable.
>
> On Sunday, February 26, 2017 at 4:05:40 AM UTC-8, Kiuhnm Mnhuik wrote:
>>
>> You can generate random tensors in Theano:
>>
>>     from theano.sandbox.rng_mrg import MRG_RandomStreams as RandomStreams
>>
>>     srng = RandomStreams()       # change the seed if you want
>>
>>     .
>>     .
>>     .
>>
>>     R1 = srng.normal(shape)
>>     R2 = srng.normal(shape)
>>     .
>>     .
>>     .
>>
>>
>>
>> On Saturday, February 25, 2017 at 12:49:18 PM UTC+1, Asghar Inanlou Asl 
>> wrote:
>>>
>>> Hi all,
>>> I need to make a shared variable and randomly initialize it. Obviously, 
>>> the way to do it is to use numpy to generate a random matrix and then 
>>> change it to shared variable via theano.shared()
>>> However, I cannot do it because the size of the random matrix is partly 
>>> coming from a TensorVariable so numpy gets stuck in it.
>>> To be particular, I have a function which multiplies the input tensor in 
>>> a random matrix (think about what a fully connected layer does). But the 
>>> point is the shape of the tensor might be changing. And it needs to be a 
>>> shared variable to that I can use it as one of the parameters of my updates.
>>> Any comments?
>>> Thanks!
>>>
>>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to