Thursday, 26 September 2019

Move from TensorFlow 1 to TensorFlow 2

TensorFlow was released as beta in 2015 and the first official release 1.0 was in 2016. Until now, 2019, TensorFlow is at 2.0 and having a release candidate.

Why switching to TF 2:
  • TensorFlow 1 will be finally phased out, although might take many years as there are already too many people using 1.x
  • TensorFlow 2 Python is more similar to TensorFlow in C++
  • TensorFlow 2 makes tensor maths is just like numberic maths with default and only eager mode.
What to change in TF 1 code:
  • Bear in mind that there is only eager execution mode, no more session
  • Replace session graph with @tf.function annotation, or just call tf.function to make graph
  • No more tf.placeholder and Session.run or feed_dict, so just use tf.convert_to_tensor to convert regular numeric data to tensors before calling TensorFlow ops.
  • tf.random_uniform is now tf.random.uniform
  • Optimisers in tf.train are all now in tf.keras.optimizers, and no more GD (tf.train.GradientDescentOptimizer), use SGD (tf.keras.optimizers.SGD) instead.
  • No more magical Someoptimiser.minimize, use these together:
    • tf.GradientTape
    • tf.GradientTape.gradient
    • Someoptimiser.apply_gradients
  • More to look in too...
Bottom line: TF 2 Python is good, more like maths, more similar to C/C++.

No comments:

Post a Comment