Learn more about Russian war crimes in Ukraine.

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

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.)

What can computers do? What are the limits of mathematics? And just how busy can a busy beaver be? This year, I’m writing Busy Beavers, a unique interactive book on computability theory. You and I will take a practical and modern approach to answering these questions — or at least learning why some questions are unanswerable!

It’s only $19, and you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

After months of secret toil, I and Andrew Carr released Everyday Data Science, a unique interactive online course! You’ll make the perfect glass of lemonade using Thompson sampling. You’ll lose weight with differential equations. And you might just qualify for the Olympics with a bit of statistics!

It’s $29, but you can get 50% off if you find the discount code ... Not quite. Hackers use the console!

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.