Tensor vs. Scalar
This merits some discussion:
Do we need both Tensor and Scalar?
This was briefly discussed in email thread around May 3 2008.
On Sat, May 03, 2008 at 10:29:39PM -0400, Olivier Breuleux wrote:
> They are not semantically equivalent. Tensor is a container, scalars are
> the things tensors contain. Scalar is different from Tensor in the same
> sense that int is different from vector. Typically, you won't
> really see the difference in Python code because of all the overloading
> and dynamic type dispatching, but the C code for both cases is just not
> the same at all.
>
> The distinction between 0-d tensor and scalar is difficult to gauge in a
> dynamically typed language or a language with overloading, but theano is
> neither.
>
> I agree that the Scalar/Tensor division is a bit awkward right now but I
> think we should just make as_tensor and as_scalar instantiate conversion
> ops when needed.
Be all this as it may, I understand that the code is syntactically quite close
to working if we just replace the Scalar class with the Tensor one. The catch I
anticipate would be having to rename the c_code() functions of Scalar Op
classes, to c_code_elem() or something, so that the broadcasting mechanism can
automatically infer the c_code() implementation which would work on entire
tensors. What else would be necessary?
This would be a win because
- it would eliminate confusion between scalar and 0-d tensors,
- also the confusion between scalar ops and broadcasted scalar ops
- it would cut a file from the project
- the generated C would be just as fast
--------------
The decision at the time was to keep both, and encourage users to use Tensor results. Scalars were to be used internally as necessary.
Do we need both Tensor and Scalar?
This was briefly discussed in email thread around May 3 2008.
On Sat, May 03, 2008 at 10:29:39PM -0400, Olivier Breuleux wrote:
> They are not semantically equivalent. Tensor is a container, scalars are
> the things tensors contain. Scalar is different from Tensor in the same
> sense that int is different from vector. Typically, you won't
> really see the difference in Python code because of all the overloading
> and dynamic type dispatching, but the C code for both cases is just not
> the same at all.
>
> The distinction between 0-d tensor and scalar is difficult to gauge in a
> dynamically typed language or a language with overloading, but theano is
> neither.
>
> I agree that the Scalar/Tensor division is a bit awkward right now but I
> think we should just make as_tensor and as_scalar instantiate conversion
> ops when needed.
Be all this as it may, I understand that the code is syntactically quite close
to working if we just replace the Scalar class with the Tensor one. The catch I
anticipate would be having to rename the c_code() functions of Scalar Op
classes, to c_code_elem() or something, so that the broadcasting mechanism can
automatically infer the c_code() implementation which would work on entire
tensors. What else would be necessary?
This would be a win because
- it would eliminate confusion between scalar and 0-d tensors,
- also the confusion between scalar ops and broadcasted scalar ops
- it would cut a file from the project
- the generated C would be just as fast
--------------
The decision at the time was to keep both, and encourage users to use Tensor results. Scalars were to be used internally as necessary.
Leave a comment