通过TensorFlow中的索引和值操纵Tensor

时间:2018-04-11 18:11:05

标签: python tensorflow machine-learning indices tensor

要求

给出张量如:

SparseTensorValue(indices=array([[0, 0], [1, 0], [1, 1], [1, 2]]),
                  values=array([2, 0, 2, 5]),
                  dense_shape=array([2, 3]))

形状为2x3

| 2 na na |
| 0  2  5 |

索引中需要一个具有值的新张量,如下所示:

请注意,总数值为6(设置为[0,1,2,3,4,5]) 形状是2x6

| 0 0 1 0 0 0 |
| 1 0 1 0 0 1 |

可以通过以下代码创建张量:

SparseTensorValue(indices=array([[0, 2], [1, 0], [1, 2], [1, 5]]),
                  values=array([1, 1, 1, 1]),
                  dense_shape=array([2, 6]))

如何以TensorFlow方式执行此操作?以下两种方法都不起作用

import tensorflow as tf

tags = tf.SparseTensor(indices=[[0, 0], [1, 0], [1, 1], [1, 2]],
                       values=[2, 0, 2, 5],
                       dense_shape=[2, 3])

print(type(tags.indices))

# approach 1:  the TensorFlow way to implement the python logic
new_indices = [[tags.indices[i], tags.values[i]]
               for i in range(tags.values.shape[0])]  # syntax incorrect

# approach 2:
indice_idx = tf.map_fn(lambda x : x[0], tags.indices)
value_idx = tf.map_fn(lambda x : x[1], tags.indices)
value_arr = tf.gather(tags.values, value_idx)

with tf.Session() as s1:
    print(indice_idx.eval())
    print(tags.values.eval())
    print('value_arr', value_arr.eval())


"""
[0 0 1 2]   <-- value_idx, which is the index of tags.values

want to combine
[0 1 1 1]   <-- indice_idx
[2 2 0 2]   <-- value_arr, which is the value of tags.values
==>
[[0,2], [1,2], [1,0], [1,2]]
"""
new_indices = tf.concat(indice_idx, value_arr)  # syntax incorrect

with tf.Session() as s:
    s.run([tf.global_variables_initializer(), tf.tables_initializer()])
    print(s.run(value_arr))
    print(s.run(tags.values))
    print(s.run(new_indices))
    print(s.run(tags.indices[3, 1]))

1 个答案:

答案 0 :(得分:0)

<强>答案

方法2中的

new_indices = tf.stack([indice_idx, value_arr], axis=1)

完整版代码

import tensorflow as tf

tags = tf.SparseTensor(indices=[[0, 0], [1, 0], [1, 1], [1, 2]],
                       values=[2, 0, 2, 5],
                       dense_shape=[2, 3])

print(type(tags.indices))

# # approach 1:  any TensorFlow way to implement the Python logic below?
# new_indices = [[tags.indices[i], tags.values[i]]
#                for i in range(tags.values.shape[0])]  # syntax incorrect

# approach 2:
indice_idx = tf.map_fn(lambda x : x[0], tags.indices)
value_idx = tf.map_fn(lambda x : x[1], tags.indices)
value_arr = tf.cast(tf.gather(tags.values, value_idx), tf.int64)

with tf.Session() as s1:
    print(indice_idx.eval())
    print(tags.values.eval())
    print('value_arr', value_arr.eval())


"""
[0 0 1 2]   <-- value_idx, which is the index of tags.values

tf.stack does:
[0 1 1 1]   <-- indice_idx
[2 2 0 2]   <-- value_arr, which is the value of tags.values
==>
[[0,2], [1,2], [1,0], [1,2]]
"""
new_indices = tf.stack([indice_idx, value_arr], axis=1)

with tf.Session() as s:
    s.run([tf.global_variables_initializer(), tf.tables_initializer()])
    print(s.run(value_arr))
    print(s.run(tags.values))
    print(s.run(new_indices))
    print(s.run(tags.indices[3, 1]))

这个问题本身已经解决了。

分离的相关问题

P.S。如果阅读文件,它无法正常工作,请参阅:

create multi-hot SparseTensor by categorical feature array column from CSV in TensorFlow