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

I am studying 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 he would print the index "4", but the result:

 argmax 

what else when i use T.neq () 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 am really new to this and have no idea if I missed anything, thanks in advance.

+5
source share
1 answer

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

In your code, we are trying to enter a list type in T.argmax (). So, from the above point of view, this will not work. Also note that the type (T.argmax (a)) is the type theano.tensor.var.TensorVariable. Therefore, it expects a TensorVariable input, and it also infers a TensorVariable type. Thus, this will not return the actual argmax.

Ok, so what works? How can we do this calculation in Teano?

Let me first determine the type of data you want to deal with. This will be the starting point of our computational graph, which we will build. In this case, it looks like we want to deal with arrays or vectors. Theano is of type ivector, which is a vector of integers, or of type fvector, which is a vector of float32 values. Let them stick to your data and make an ivector, since we have integer values:

 x = T.ivector('input') 

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

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

 y = T.argmax(x) 

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

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

The syntax of theano.function can be found here .

Think about this function, which now actually performs the calculation that we defined with x and y.

When I execute:

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

It returns:

 array(4) 

So what have we really done? By defining Anano variables and using the anano.tensor functions, we build a computational graph. Then we used theano.function to compile a function that actually performs this calculation on the actual inputs that we specify.

To finish: how to perform the operation is not equal?

 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 a, b as theanano TensorVariables, rather than assigning explicit variables to them.

You will get stuck, just remember that you need to define your calculations in terms of Theano TensorVariables, and then to actually β€œuse it” you have to compile it with theano.function.

+5
source

Source: https://habr.com/ru/post/1245465/


All Articles