Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does TensorFlow recommends the "functional style for constructing operations"?

Tags:

In TensorFlow's documentation, it is possible to find the following text:

// Not recommended
MatMul m(scope, a, b);

// Recommended
auto m = MatMul(scope, a, b);

I see no obvious benefit from using the "recommended" style. The first version is shorter at least. Also the "recommended" version might include more actions related to the unnecessary assignment operation.

I have read that documentation page no less than six times and still cannot get the rationale behind their reasoning.

Is this recommendation just a matter of style or may the second version have some benefits?

like image 760
Audrius Meškauskas Avatar asked Apr 30 '19 08:04

Audrius Meškauskas


1 Answers

Also the "recommended" version might include more actions related to the unnecessary assignment operation.

There is no assignment. It's initialization. And any extra object that may exist in principle is elided entirely in any compiler worth using.

That recommendation you cite is inline with Herb Sutter's "Almost Always Auto" advice. The strongest point in favor of this style (which in full disclosure, I do not follow) is that it makes it impossible to leave a variable uninitialized.

auto foo; // ill-formed.
int foo; // indeterminate value
auto foo = int(); // a zero integer

Granted, a good static analysis tool can warn about it too, but it's still a very strong point that requires no additional analysis by a compiler or external tools.

Beyond that, the stylistic argument is that it also keep your code consistent with cases where you use auto for sanity's sake, such as

auto it = myVec.begin();

But YMMV on all counts. It is ultimately going to be a stylistic choice, and regardless of the chosen style, exceptions to both styles exist.

like image 95
StoryTeller - Unslander Monica Avatar answered Nov 02 '22 23:11

StoryTeller - Unslander Monica