tensorflow索引如何工作

Kei*_*ith 16 tensorflow

我无法理解张量流的基本概念.索引如何用于张量读/写操作?为了使这一点具体化,如何将以下numpy示例转换为tensorflow(使用张量来分配数组,索引和值):

x = np.zeros((3, 4))
row_indices = np.array([1, 1, 2])
col_indices = np.array([0, 2, 3])
x[row_indices, col_indices] = 2
x
Run Code Online (Sandbox Code Playgroud)

输出:

array([[ 0.,  0.,  0.,  0.],
       [ 2.,  0.,  2.,  0.],
       [ 0.,  0.,  0.,  2.]])
Run Code Online (Sandbox Code Playgroud)

......而且......

x[row_indices, col_indices] = np.array([5, 4, 3])
x
Run Code Online (Sandbox Code Playgroud)

输出:

array([[ 0.,  0.,  0.,  0.],
       [ 5.,  0.,  4.,  0.],
       [ 0.,  0.,  0.,  3.]])
Run Code Online (Sandbox Code Playgroud)

......最后......

y = x[row_indices, col_indices]
y
Run Code Online (Sandbox Code Playgroud)

输出:

array([ 5.,  4.,  3.])
Run Code Online (Sandbox Code Playgroud)

Yar*_*tov 10

github问题#206很好地支持了这个问题,同时你必须采用冗长的解决方法

第一个例子可以tf.select通过从一个或另一个中选择每个元素来组合两个相同形状的张量来完成

tf.reset_default_graph()
row_indices = tf.constant([1, 1, 2])
col_indices = tf.constant([0, 2, 3])
x = tf.zeros((3, 4))
sess = tf.InteractiveSession()

# get list of ((row1, col1), (row2, col2), ..)
coords = tf.transpose(tf.pack([row_indices, col_indices]))

# get tensor with 1's at positions (row1, col1),...
binary_mask = tf.sparse_to_dense(coords, x.get_shape(), 1)

# convert 1/0 to True/False
binary_mask = tf.cast(binary_mask, tf.bool)

twos = 2*tf.ones(x.get_shape())

# make new x out of old values or 2, depending on mask 
x = tf.select(binary_mask, twos, x)

print x.eval()
Run Code Online (Sandbox Code Playgroud)

[[ 0.  0.  0.  0.]
 [ 2.  0.  2.  0.]
 [ 0.  0.  0.  2.]]
Run Code Online (Sandbox Code Playgroud)

第二个可以完成scatter_update,除了scatter_update只支持线性索引和处理变量.所以你可以创建一个临时变量并像这样使用重塑.(为了避免你可以使用的变量dynamic_stitch,请参阅结尾)

# get linear indices
linear_indices = row_indices*x.get_shape()[1]+col_indices

# turn 'x' into 1d variable since "scatter_update" supports linear indexing only
x_flat = tf.Variable(tf.reshape(x, [-1]))

# no automatic promotion, so make updates float32 to match x
updates = tf.constant([5, 4, 3], dtype=tf.float32)

sess.run(tf.initialize_all_variables())
sess.run(tf.scatter_update(x_flat, linear_indices,  updates))

# convert back into original shape
x = tf.reshape(x_flat, x.get_shape())

print x.eval()
Run Code Online (Sandbox Code Playgroud)

[[ 0.  0.  0.  0.]
 [ 5.  0.  4.  0.]
 [ 0.  0.  0.  3.]]
Run Code Online (Sandbox Code Playgroud)

最后,第三个例子已经支持了gather_nd,你写的

print tf.gather_nd(x, coords).eval()
Run Code Online (Sandbox Code Playgroud)

要得到

[ 5.  4.  3.]
Run Code Online (Sandbox Code Playgroud)

编辑,5月6日

更新x[cols,rows]=newvals可以在不使用变量(在会话运行调用之间占用内存)的情况下完成,方法是使用select带有sparse_to_dense稀疏值向量的变量,或者依赖于dynamic_stitch

sess = tf.InteractiveSession()
x = tf.zeros((3, 4))
row_indices = tf.constant([1, 1, 2])
col_indices = tf.constant([0, 2, 3])

# no automatic promotion, so specify float type
replacement_vals = tf.constant([5, 4, 3], dtype=tf.float32)

# convert to linear indexing in row-major form
linear_indices = row_indices*x.get_shape()[1]+col_indices
x_flat = tf.reshape(x, [-1])

# use dynamic stitch, it merges the array by taking value either
# from array1[index1] or array2[index2], if indices conflict,
# the later one is used 
unchanged_indices = tf.range(tf.size(x_flat))
changed_indices = linear_indices
x_flat = tf.dynamic_stitch([unchanged_indices, changed_indices],
                           [x_flat, replacement_vals])
x = tf.reshape(x_flat, x.get_shape())
print x.eval()
Run Code Online (Sandbox Code Playgroud)

  • 同事建议使用`dynamic_stitch`而不是Variables,更新配方 (2认同)