Search code examples
theanotensor

How to specify value of theano.tensor.ivector?


I would like to create a theano.tensor.ivector variable and specify its values. In most code examples on the internet, I find v = T.ivector(). This creates the tensor variable but don't specify its value. I tried this :

import theano.tensor as T
val = [1,5]
v = T.ivector(value=val, name='v')

but I get the following error :

  File "<stdin>", line 1, in <module>
TypeError: __call__() got an unexpected keyword argument 'value'

Solution

  • I think you may be a little confused about the use of tensors, as it isn't a traditional variable that you assign a value to on declaration. A tensor is really a placeholder variable with a specified format that you will use in a function later. Extending on your example:

    import theano.tensor as T
    from theano import function
    val = [1, 5]
    v = T.ivector('v')
    f = function([v], [v]) # Create a function that just returns the input
    # Evaluate the function
    f(val)
    

    In the above code we just create a function that takes the tensor v and returns it. The value is not assigned until we call the function f(val)

    You may find the baby steps page of the documentation helpful