Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to swich theano.tensor to numpy.array?

I have simple codes as shown below:

class testxx(object):
    def __init__(self, input):
        self.input = input
        self.output = T.sum(input)
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32)
classfier = testxx(a)
outxx = classfier.output
outxx = np.asarray(outxx, dtype = np.float32)

However, I get the following error information:

ValueError: setting an array element with a sequence.

Furthermore, when I use the function of theano.tensor, it seems that what it returns is called "tensor", and I can't simply switch it to the type numpy.array, even though what the result should shape like a matrix.

So that's my question:how can I switch outxx to type numpy.array?

like image 819
user3633343 Avatar asked May 14 '14 00:05

user3633343


People also ask

How do I turn a Numpy array into a tensor?

a NumPy array is created by using the np. array() method. The NumPy array is converted to tensor by using tf. convert_to_tensor() method.

What is the difference between Numpy array and tensor?

The difference between a NumPy array and a tensor is that the tensors are backed by the accelerator memory like GPU and they are immutable, unlike NumPy arrays.

What is theano tensor?

Theano is a Python library for efficiently handling mathematical expressions involving multi-dimensional arrays (also known as tensors). It is a common choice for implementing neural network models. Theano has been developed in University of Montreal, in a group led by Yoshua Bengio, since 2008.


2 Answers

Theano "tensor" variable are symbolic variable. What you build with them are like a programme that you write. You need to compile a Theano function to execute what this program do. There is 2 ways to compile a Theano function:

f = theano.function([testxx.input], [outxx])
f_a1 = f(a)

# Or the combined computation/execution
f_a2 = outxx.eval({testxx.input: a})

When you compile a Theano function, your must tell what the input are and what the output are. That is why there is 2 parameter in the call to theano.function(). eval() is a interface that will compile and execute a Theano function on a given symbolic inputs with corresponding values.

like image 178
nouiz Avatar answered Sep 30 '22 04:09

nouiz


Since testxx uses sum() from theano.tensor and not from numpy, it probably expects a TensorVariable as input, and not a numpy array.

=> Replace a = np.array(...) with a = T.matrix(dtype=theano.config.floatX).

Before your last line, outxx will then be a TensorVariable that depends on a. So you can evaluate it by giving the value of a.

=> Replace your last line outxx = np.asarray(...) with the following two lines.

f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))

The following code runs without errors.

import theano
import theano.tensor as T
import numpy as np

class testxx(object):
    def __init__(self, input):
        self.input = input
        self.output = T.sum(input)
a = T.matrix(dtype=theano.config.floatX)
classfier = testxx(a)
outxx = classfier.output
f = theano.function([a], outxx)
outxx = f(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = np.float32))

Theano documentation on adding scalars gives other similar examples.

like image 44
xagg Avatar answered Sep 30 '22 04:09

xagg