Advertisement
Guest User

Untitled

a guest
May 25th, 2015
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. import numpy as np
  2. import theano
  3. import theano.tensor as T
  4. from theano.tensor.shared_randomstreams import RandomStreams
  5.  
  6. # Example of using scan within scan in Theano
  7.  
  8. numpy_rng = np.random.RandomState(1234).randint(2 ** 30)
  9.  
  10. D = T.fvector("D")
  11. X = T.fmatrix("X")
  12.  
  13. def test(X1,D1):
  14. X_hat=T.nnet.softmax(X1)
  15. components, updates = theano.scan(fn=lambda x1,d: RandomStreams(numpy_rng).multinomial(size=(1,1), n=d, pvals=x1, dtype=theano.config.floatX),
  16. outputs_info=None, sequences=[X_hat,D1])
  17. return [[T.squeeze(components), D1+1],updates] # pass updates as second element in a list. If you have more updates (e.g. more loops) here,
  18. # concatenate them instead of creating the list (i.e. pass updates1+updates2 instead of [update1,update2] as in case of output vars
  19.  
  20. c, up = theano.scan(fn=lambda dd,xx: test(xx,dd), outputs_info=[None, D],non_sequences=X,n_steps=3)
  21.  
  22. f=theano.function([X,D],c,updates=up,allow_input_downcast=True)
  23.  
  24. c=f(np.random.rand(3,5),np.array([15,5,10]))
  25. print c[0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement