Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't use theano.tensor.argmax and theano.tensor.mean correctly

Tags:

python

theano

I am learning Theano now but there are always some problems.my code is as follows:

import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.argmax(a)

I thought it would print the index of '4',but the result is:

argmax

what's more,when I am using T.neq().just as follows:

import theano 
from numpy import *
import theano.tensor as T
a = [1,2,3,4]
b = [7,8,9,10]
print T.neq(a,b)

the result shows:

Elemwise{neq,no_inplace}.0

I really new to this and have no idea,did I miss anything?thank you in advance..

like image 285
Chris Avatar asked Mar 21 '16 08:03

Chris


People also ask

What is tensor in theano?

Tensor = multi-dimensional array In Theano they are specifically numerical arrays: Theano is a Python library that allows you to define, optimize, and efficiently evaluate mathematical expressions involving multi-dimensional arrays.

How Theano works?

Theano - Functions Theano function acts like a hook for interacting with the symbolic graph. A symbolic graph is compiled into a highly efficient execution code. It achieves this by restructuring mathematical equations to make them faster. It compiles some parts of the expression into C language code.


1 Answers

T.argmax() is expecting a Theano TensorVariable type. Some of the types of Variables used in Theano are listed here. Don't let the name "fully typed constructors" scare you. Think about them more in terms of what type of data you want to use as your input. Are you using float matrices? Then the relevant TensorVariable type is probably "fmatrix." Are you dealing with batches of RGB image data? Then the relevant TensorVariable type is probably "tensor4."

In your code, we are trying to input a list type into T.argmax(). So from the above point of view, that isn't going to work. Also, note that type(T.argmax(a)) is a theano.tensor.var.TensorVariable type. So it is expecting a TensorVariable as input, and it outputs a TensorVariable type as well. So this isn't going to return the actual argmax.

Okay, so what does work? How can we do this computation in Theano?

Let's first identify the type of data you want to deal with. This is going to be the starting point of our computational graph that we will be building. In this case, it looks like we want to deal with arrays or vectors. Theano has an ivector type, which is a vector of integers, or an fvector type which is an vector of float32 values. Let's stick with your data and do ivector since we have integer values:

x = T.ivector('input')

This line just created a TensorVariable x that represents our intended input type, an array of integers.

Now let's define a TensorVariable for the argmax of the elements of x:

y = T.argmax(x)

So far we have built a computational graph, which is expecting an array of integers as input and will output the argmax of that array. However, in order to actually do this, we have to compile this into a function:

get_argmax = theano.function([x], y)

The theano.function syntax can be found here.

Think of this function as now actually performing the computation that we have defined using x and y.

When I execute:

get_argmax([1,2,3,4,19,1])

It returns:

array(4)

So what did we really do? By defining Theano variables and using theano.tensor functions, we build a computational graph. We then used theano.function to compile a function that actually performs that computation on actual inputs that we specify.

To end: how to do the not equals operation?

a = T.ivector('a')
b = T.ivector('b')
out = T.neq(a,b)
get_out = theano.function([a,b], out)
print get_out([1,2,3,4], [7,8,9,10])

will return:

[1,1,1,1]

One of the key conceptual differences is that I treat the a,b as theano TensorVariables, rather than assigning them explicit variables.

You'll get the hang out of it, just remember that you need to define your computation in terms of Theano TensorVariables, and then to actually "use it" you have to compile it using theano.function.

like image 62
Indie AI Avatar answered Oct 06 '22 23:10

Indie AI