Interest in a theano.len?

18 views
Skip to first unread message

Olivier Delalleau

unread,
Nov 19, 2010, 2:34:45 PM11/19/10
to thean...@googlegroups.com
Thinking of filing a ticket to add a theano.len function, which would basically be a symbolic len.
Any objection?
Two goals:
- theano.len(my_vector) is easier to read than my_vector.shape[0] (for me)
- more importantly, it could make sense for some non-tensor-like variables to have a length but not a shape (e.g. a StringVariable)

NB: we cannot use len because __len__ *must* return something that can be converted to an int, otherwise Python is not happy.

-=- Olivier

James Bergstra

unread,
Nov 19, 2010, 3:47:05 PM11/19/10
to thean...@googlegroups.com
Sure,

How about something like

theano.len(v) means v.type.symbolic_len(v)

So types that implement symbolic_len work with theano.len?

James
--
http://www-etud.iro.umontreal.ca/~bergstrj

David Warde-Farley

unread,
Nov 19, 2010, 4:36:34 PM11/19/10
to thean...@googlegroups.com
On Fri, Nov 19, 2010 at 03:47:05PM -0500, James Bergstra wrote:

> How about something like
>
> theano.len(v) means v.type.symbolic_len(v)
>
> So types that implement symbolic_len work with theano.len?

Can we call it something other than theano.len? Maybe theano.length?

Even though it's never a good idea to "from foo import *", people routinely
do it, and this would be a nasty surprise if the len() builtin stopped
working.

Another option would be to make it work like builtin len() for non-TensorType
objects.

David

Olivier Delalleau

unread,
Nov 19, 2010, 4:38:42 PM11/19/10
to thean...@googlegroups.com


2010/11/19 David Warde-Farley <ward...@iro.umontreal.ca>

On Fri, Nov 19, 2010 at 03:47:05PM -0500, James Bergstra wrote:

> How about something like
>
> theano.len(v) means v.type.symbolic_len(v)
>
> So types that implement symbolic_len work with theano.len?

Can we call it something other than theano.len? Maybe theano.length?

Even though it's never a good idea to "from foo import *", people routinely
do it, and this would be a nasty surprise if the len() builtin stopped
working.

Doesn't matter to me, but as far as I'm concerned, I have no problem punishing people who would do "from theano import *" :P
 

Another option would be to make it work like builtin len() for non-TensorType
objects.

Instead of making it work like builtin len, I would have it throw an exception if (as per James' suggested implementation) v.type.symbolic_len(v) cannot be called for some reason. This should be enough to make sure people understand what is going wrong (the exception could suggest not to import * if we detect len is called on something that is not a variable).

-=- Olivier

James Bergstra

unread,
Nov 19, 2010, 4:43:42 PM11/19/10
to thean...@googlegroups.com
Hmm, yeah I think that if len(x) works, then "for i in x" should also work, and loop len(x) times.  Since iteration like that doesn't make sense for theano variables, I don't think it's a good idea to try and piggy back on the len() function.  And python insists len(x) be an int anyway.

--
http://www-etud.iro.umontreal.ca/~bergstrj

Olivier Delalleau

unread,
Nov 22, 2010, 10:03:02 AM11/22/10
to thean...@googlegroups.com
For future reference: added ticket 587 for 0.3.1 (feel free to edit it).

-=- Olivier
Reply all
Reply to author
Forward
0 new messages