by nl · 6y ago · 0 comments
The number of times I've sat with 2 developers looking over my shoulder trying to workout WTF my TF (or Numpy or Pytorch) manipulations aren't ending up the correct shape is huge. It's probably $50-100K/year of time for the last 4-5 years.
It's always obvious in retrospect.. until next time you have to read that code.
99pi has a great episode on this phenomenon related to autopilot in planes https://99percentinvisible.org/episode/children-of-the-magen... where over automation makes edge case situations much harder to handle (in the case of planes this is deadly)
I don't see any problem in automating the generation of the line-noise that is array manipulations.
Can I write assembly? No. Do I understand what a compiler is doing? Yes. Does this cause problems for me? Maybe twice in my 20 year career and in both cases I've worked it out. Seems like a good trade off, and I see this as similar.
But how will this tool help? If you put the sample you used in a comment, then your confusion is wondering if that's in sync... and whether the input is like what you wrote.
IMO the sweet spot that's easy both for humans & computers is index notation. Instead of `tf.add(X, tf.expand_dims(Y, 1))` (their example, which I think also applies to 4-tensors in ways not trivial to see from the sample) something like "out_{r,c} = X_r + Y_c" is completely unambiguous, and quicker to type than making a sample.
I don't think you can do that in tf.einsum, nor einops. Is there a Python package which does this? In Julia you can write `@cast out[r,c] := X[r] + Y[c]` (with my package, but several others share this notation).
> But how will this tool help?
It's absolutely true that it doesn't help. But that's a second order problem to the hours spend trying to work out how to write the code.
It could also generate comments that explain what is happening..
X + Y[:, tf.newaxis]
For higher dimensions of Y, you can use (but it might be ill-advised)
X + Y[:, tf.newaxis, ...]
Keras tries to help you, but the messages are inscrutable sometimes. "Oh there's this extra dimension that has no size, and this other that's just size 1 but if you try to flatten it everything stops working"