tensorflow打印出L2规范

时间:2017-12-20 03:43:54

标签: tensorflow norm

我是tensorflow的新手,希望你能帮助我。

我想打印出两个向量之间的l2差异(x [0] - y [0])*(x [0] - y [0])+(x [1] - y [1])* (x [1] - y [1])+ ...

这是我的代码:

import tensorflow as tf;

sess=tf.InteractiveSession()

xx = [[1.0, 2.0, 3.0]];
yy = [[2.0, 3.0, 4.0]];
x = tf.placeholder(tf.float32, shape=[1, 3], name='x')   
y = tf.placeholder(tf.float32, shape=[1, 3], name='y') 
cost = tf.nn.l2_loss(x - y, name='cost')

sess.run([cost, y], feed_dict={x: xx, y:yy})
print(cost, y);

But here is the output 

Tensor("cost:0", shape=(), dtype=float32) Tensor("y:0", shape=(1, 3), dtype=float32). 

如何打印实际值?

谢谢,

1 个答案:

答案 0 :(得分:1)

您需要从sess.run解压缩返回的值:

cost_, y_ = sess.run([cost, y], feed_dict={x: xx, y:yy})
print(cost_, y_);

# 1.5 [[ 2.  3.  4.]]