1

Since Theano allows to update the memory on the graphics card DRAM by simply defining which memory area has to be updated and how this should be done, I was wondering if the following is somehoe possible (imho it should be).

I have a 2x5 randomly initialized matrix which first column will be initialized with start-values. I would like to write a function that depends of the preceeding column and updates the next one based on arbitrary calulations.

I think this code explains it very well:

Note: This code is not working, it's just an illustration:

from theano import function, sandbox, shared
import theano.tensor as T
import numpy as np

reservoirSize = 2
samples       = 5

            # To initialize _mat first column
_vec      = np.asarray([1 for i in range(reservoirSize)], np.float32)

            # Random matrix, first column will be initialized correctly (_vec)
_mat      = np.asarray(np.random.rand(reservoirSize, samples), np.float32)
_mat[:,0] = _vec

print "Init:\n",_mat

_mat      = shared(_mat)

idx  = T.iscalar()
test = function([idx], updates= {
            # The indexing causes the problem here. Imho there should be
            # a way to do something like this:
            #    update: _mat[:, idx] = _max[:, idx-1] * 2
            _mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2) 
            })

for i in range(1, samples):
    test(i)

print "Done:\n",_mat

My desired output would be:

Init:
[[ 1. 0.62166548  0.17463242  0.00858122  0.59709388]
 [ 1. 0.52690667  0.20800008  0.86816955  0.43518791]]
Done: 
[[ 1. 2. 4. 8. 16. ]
   1. 2. 4. 8. 16. ]]

but instead I get

Init:
[[ 1. 0.62166548  0.17463242  0.00858122  0.59709388]
 [ 1. 0.52690667  0.20800008  0.86816955  0.43518791]]
Traceback (most recent call last):
  File "/home/snooc/workspace/eclipse-python/Bachelorarbeit/theano/test.py", line 20, in <module>
    _mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2) })
  File "/usr/lib/python2.7/site-packages/theano/compile/function.py", line 223, in function
    profile=profile)
  File "/usr/lib/python2.7/site-packages/theano/compile/pfunc.py", line 490, in pfunc
    no_default_updates=no_default_updates)
  File "/usr/lib/python2.7/site-packages/theano/compile/pfunc.py", line 194, in rebuild_collect_shared
    store_into)
TypeError: ('update target must be a SharedVariable', Subtensor{::, int32}.0)

Can anybody help me here?

Wow: This question is 9 minutes after asking already in the Top 4 of google results for "Theano indexing gpu" for me. O_o

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378

1 Answers1

1

Have a look at: How can I assign/update subset of tensor shared variable in Theano?

For your code this translates to:

from theano import function, sandbox, shared
import theano.tensor as T
import numpy as np

reservoirSize = 2
samples       = 5

# To initialize _mat first column
_vec      = np.asarray([1 for i in range(reservoirSize)], np.float32)

# Random matrix, first column will be initialized correctly (_vec)
_mat      = np.asarray(np.random.rand(reservoirSize, samples), np.float32)
_mat[:,0] = _vec

print "Init:\n",_mat

_mat      = shared(_mat)

idx  = T.iscalar()
test = function([idx], updates= {
            # -> instead of
            #_mat[:,idx]:sandbox.cuda.basic_ops.gpu_from_host(_mat[:,idx-1] * 2)
            # -> do this:
            _mat:T.set_subtensor(_mat[:,idx], _mat[:,idx-1]*2) 
            })

for i in range(1, samples):
    test(i)

print "Done:\n",_mat.get_value()  # use get_value() here to retrieve the data
Community
  • 1
  • 1
pwohlhart
  • 46
  • 2
  • Thank you! Btw I needed to know this for my bachelor thesis which is supervised by Prof. Legenstein. You may know him :) – Stefan Falk Jun 20 '14 at 14:41