How to do it...

  1. First, we will load TensorFlow and initialize a graph:
import tensorflow as tf 
sess = tf.Session() 
  1. Then, we will illustrate how to calculate the edit distance between two words, 'bear' and 'beer'. First, we will create a list of characters from our strings with Python's list() function. Next, we will create a sparse 3D matrix from that list. We have to tell TensorFlow the character indices, the shape of the matrix, and which characters we want in the tensor. After that, we can decide whether we would like to go with the total edit distance (normalize=False) or the normalized edit distance (normalize=True), where we divide the edit distance by the length of the second word:
hypothesis = list('bear') truth = list('beers') ...

Get TensorFlow Machine Learning Cookbook - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.