How to Caculate Cosin Distance in TensorFlow

Here is an example:

import tensorflow as tf
import numpy as np

x = np.array([[2,1],[1,1]])
y = np.array([[0,1],[2,2]])

xx = tf.convert_to_tensor(x,dtype=tf.float32)
yy = tf.convert_to_tensor(y,dtype=tf.float32)

x_norm = tf.sqrt(tf.reduce_sum(tf.square(xx), axis=1))
y_norm = tf.sqrt(tf.reduce_sum(tf.square(yy), axis=1))

z = tf.reduce_sum(xx*yy,axis=1)

m = x_norm * y_norm
cosin = z / m

init = tf.global_variables_initializer() 
init_local = tf.local_variables_initializer()

with tf.Session() as sess:
    sess.run([init, init_local])
    print(sess.run([x_norm, y_norm, z, cosin]))

, ,