Tensorflow 将_和减少一定的维数
我有两个嵌入张量Tensorflow 将_和减少一定的维数,tensorflow,Tensorflow,我有两个嵌入张量A和B,看起来像 [ [1,1,1], [1,1,1] ] 及 我想做的是计算L2距离d(A,B)元素 首先我做了一个tf.square(tf.sub(lhs,rhs))来获得 [ [1,1,1], [0,0,0] ] 然后我想做一个元素级的reduce,它返回 [ 3, 0 ] 但是tf.reduce\u sum不允许我按行减少。如有任何意见,将不胜感激。谢谢。添加值为1的reduction\u index参数,例如: tf.reduce_sum(
A
和B
,看起来像
[
[1,1,1],
[1,1,1]
]
及
我想做的是计算L2距离d(A,B)
元素
首先我做了一个tf.square(tf.sub(lhs,rhs))
来获得
[
[1,1,1],
[0,0,0]
]
然后我想做一个元素级的reduce,它返回
[
3,
0
]
但是
tf.reduce\u sum
不允许我按行减少。如有任何意见,将不胜感激。谢谢。添加值为1的reduction\u index
参数,例如:
tf.reduce_sum( tf.square( tf.sub( lhs, rhs) ), 1 )
这应该会产生你想要的结果。在reduce\u sum()
上,根据使用四个参数的reduce\u sum
函数
tf.reduce_sum(input_tensor, axis=None, keep_dims=False, name=None, reduction_indices=None).
但是reduction\u index
已被弃用。最好使用axis而不是。如果未设置轴,将减小其所有尺寸
例如,这是从
上述要求可以这样写
import numpy as np
import tensorflow as tf
a = np.array([[1,7,1],[1,1,1]])
b = np.array([[0,0,0],[1,1,1]])
xtr = tf.placeholder("float", [None, 3])
xte = tf.placeholder("float", [None, 3])
pred = tf.reduce_sum(tf.square(tf.subtract(xtr, xte)),1)
# Initializing the variables
init = tf.global_variables_initializer()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
nn_index = sess.run(pred, feed_dict={xtr: a, xte: b})
print nn_index
# 'x' is [[1, 1, 1]
# [1, 1, 1]]
tf.reduce_sum(x) ==> 6
tf.reduce_sum(x, 0) ==> [2, 2, 2]
tf.reduce_sum(x, 1) ==> [3, 3]
tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
tf.reduce_sum(x, [0, 1]) ==> 6
import numpy as np
import tensorflow as tf
a = np.array([[1,7,1],[1,1,1]])
b = np.array([[0,0,0],[1,1,1]])
xtr = tf.placeholder("float", [None, 3])
xte = tf.placeholder("float", [None, 3])
pred = tf.reduce_sum(tf.square(tf.subtract(xtr, xte)),1)
# Initializing the variables
init = tf.global_variables_initializer()
# Launch the graph
with tf.Session() as sess:
sess.run(init)
nn_index = sess.run(pred, feed_dict={xtr: a, xte: b})
print nn_index