TensorFlow 2 ‘hello world’

The following program finds out the cost of chocolate per gram using TensorFlow 2. It takes as input some example chocolate bars, each with a weight and a price.

import tensorflow as tf

NUM_EXAMPLES = 100
TRUE_DOLLARS_PER_GRAM = 0.1
weight_grams = tf.linspace(10., 70., NUM_EXAMPLES)
price_dollars = weight_grams*TRUE_DOLLARS_PER_GRAM + tf.random.normal([NUM_EXAMPLES])

model_dollars_per_gram = tf.Variable(5., name='dollars_per_gram')

optimizer = tf.keras.optimizers.SGD(learning_rate=0.0001)

for i in range(30):
  with tf.GradientTape() as tape:
    loss = tf.reduce_mean(tf.square(model_dollars_per_gram*weight_grams - price_dollars))
  grads = tape.gradient(loss, [model_dollars_per_gram])
  optimizer.apply_gradients(zip(grads, [model_dollars_per_gram]))

print("Final cost per gram: $%s" % model_dollars_per_gram.value().numpy())

I wrote the same thing in 2017 using TensorFlow 1. TensorFlow 2 is really quite different. The biggest difference seems to be the introduction of automatic differentiation. Instead of building an explicit computation graph, we use ordinary Python operators, like * and -. Instead of a static graph, you can use whatever complex Python control flow you like. The autodiff “tape” is made explicit, and lets you ask for gradients to pass to the optimizer.

(I should do a post explaining autodiff soon. It’s just marvelous.)

I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged #tensorflow, #programming, #ml. All content copyright James Fisher 2021. This post is not associated with my employer. Found an error? Edit this page.