2

Could you tell me what's the difference with the shapes 0, (?,), (1,?), (?,?) in Theano? Why the array I defined as

arr = np.array([1,2,3])

is an array of (3,) ? How could I defined an array of (3,1) ?

Besides, I write the code as below:

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


class hiddenLayer():
    """ Hidden Layer class
    """
    def __init__(self, inputs, n_in, n_out, act_func):
        rng = np.random
        self.W = shared(np.asarray(rng.uniform(low=-4*np.sqrt(6. / (n_in + n_out)),
                                               high=4*np.sqrt(6. / (n_in + n_out)),
                                               size=(n_in, n_out)),
                                   dtype=T.config.floatX),
                        name='W')
        self.inputs = inputs
        self.b = shared(np.zeros(n_out, dtype=T.config.floatX), name='b')
        self.x = T.dvector('x')
        self.z = T.dot(self.x, self.W) + self.b
        self.ac = function([self.x], self.z)

a = hiddenLayer(np.asarray([1, 2, 3], dtype=T.config.floatX), 3, 3, T.tanh)
print a.ac(a.inputs, a.z)

Why it report error:

  'Expected an array-like object, but found a Variable: '
TypeError: ('Bad input argument to theano function at index 1(0-based)', 'Expected an array-like object, but found a Variable: maybe you are trying to call a function on a (possibly shared) variable instead of a numeric array?')

Thank you very much!

BoscoTsang
  • 294
  • 1
  • 13
  • 1
    While not an exact duplicate, [this answer in a related thread](http://stackoverflow.com/a/22074424/1634191) has a very good description of the differences in `numpy.array` shapes. – wflynny May 13 '14 at 14:50
  • Why I pass a (3,) array to theano.function it raise error as above? x is defined as T.dvector whose shape is (?,) and what I pass is a (3,) array. Why it told me it expected an array-like object? – BoscoTsang May 13 '14 at 15:22

1 Answers1

2

You are trying to pass a.z to a.ac(), whereas a.z is actually the result of a.ac(x)!

Instead, you probably want do this:

a.ac(a.inputs)
# array([  8.61379147, -13.0183053 ,  -4.41056323])

The value of the symbolic variable a.z is undetermined until a.x, a.W and a.b can all be evaluated. The syntax for theano.function works like this:

find_x = theano.function([<inputs needed to compute x>], <output x>)

When you actually want to call find_x(), you only need to give it the stuff in the square brackets, and the second argument to theano.function will be the return value of find_x().

ali_m
  • 62,795
  • 16
  • 193
  • 270