On Fri, Oct 07, 2016, daniel hernandez wrote:
> To put it simply, my question
> is: how can I myself define a TensorVariable whose eval method takes no
> arguments and returns a value?
>
> For instance, let's say that I want a variable that represents the number
> Pi. In my mind this is no less no more of a constant than the identity
> matrix. So, I should be able to define a variable pi, for which I can call
>
> pi.eval()
>
> and get "3.14..."
So, you are asking how to create a Constant, is that right?
In that case, you can simply do:
>>> pi = theano.tensor.constant(np.pi)
>>> pi
TensorConstant{3.141592653589793}
>>> pi.eval()
array(3.141592653589793)
Another way is to define an expression that only depends on constants.
That way, you do not need to provide any input values, and the returned
value will always be the same. For instance:
>>> c22 = theano.tensor.constant(22.)
>>> c7 = theano.tensor.constant(7.)
>>> pi_approx = c22 / c7
>>> pi_approx
Elemwise{true_div,no_inplace}.0
>>> pi_approx.eval()
array(3.142857074737549, dtype=float32)
This is similar to the identity matrix example: the identity matrix is
the output of a symbolic operation, but all of its inputs are Constant.
> > In that case, you can use the explicit version:
> > >>> f = theano.function(inputs=[x], outputs=x)
> > >>> f(3)
> >
> > or the "eval shortcut"
> > >>> x.eval({x: 3})
> >
>
> This is unsatisfactory as I explained above. When you run x.eval({x : 3}) you
> get array(3.0). In the following line, you could run x.eval({x : 999}) and
> you would obtain array(999.0). This is NOT the behavior I want. The number
> 3 is nor permanently assigned to the variable x, the way the identity
> matrix is assigned to E in my original example. I hope this makes my
> question clearer.
Note that in your case, the identity matrix is not _permanently_
assigned to E either. Graph manipulation and substitution can still
happen later. For instance:
>>> E = theano.tensor.eye(2)
>>> F = E - 1
>>> f = theano.function([], F, givens={E: -theano.tensor.eye(3)})
>>> f()
array([[-2., -1., -1.],
[-1., -2., -1.],
[-1., -1., -2.]])
Or even:
>>> c2 = theano.tensor.constant(2)
>>> c3 = theano.tensor.constant(3)
>>> E = theano.tensor.eye(c2)
>>> E.eval()
array([[ 1., 0.],
[ 0., 1.]])
>>> f = theano.function([], E, givens={c2: c3})
>>> f()
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
--
Pascal
--
---
You received this message because you are subscribed to the Google Groups
"theano-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.