Some additional utils which can be used to easily manipulate tensorflow's tensors.
tfs.ops.get_top_k() is used to get the top k elements and corresponding positions in a tensor with any shape;
tfs.ops.assign() is used to set some values to a tensor at designated positions;
"""This function would compare the values of input tensor(any shape) in the lowest
dimension(axis=-1),and choose top k values and corresponding positions
Args:
input:a tensor with any shape
k:the number of top_k values
sorted: if true the resulting k elements will be sorted by the values in descending order.
Return:
the top k value and corresponding position.
Example:
value = tf.random_normal(shape=[30,30,30,3], dtype=tf.float32) ##random produce some data
v,p = get_top_k(value,2) ##return the top 2 values and postions
with tf.Session() as sess:
v,p = sess.run([v,p]) ##get the top 2 values and postion
"""
"""This function would assign a value to the input in the specific position
Args:
input: A tensor with any shape.
position: Specify the postions of input where you wanna assign the value.
value: the value you wanna assign in. Must be 1-D array(or tensor,list)
---The length of value must be 1 or the same with the position's.
---Dtype of value must be the same with input's.
Return:
the tensor after modify.
Example:
## bulid the tf graph ##
# random produce some data #
raw_tensor = tf.constant(np.random.normal(size=[10,2, 10, 3]).astype(np.float32))
# get the top 500 values and corresponding positions #
top_k_tensor, position = get_top_k(raw_tensor, k=500)
#replace the raw_tensor with the new value 1. at the designated position.
new_tensor = assign(input=raw_tensor, position=position, value=[1.])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
new_value = sess.run(new_tensor)
"""