I have the following code:
conversion_dictionary = {1: 2, 2: 5, 3: 4, 4: 4}
converted_vals= [conversion_dictionary[label] for label in labels]
It converts the labels from one set of values to another. I want to do the same thing using tensors but I know that tesnors are not iterable , so I get the not iterable error for the following code
labels = tf.constant([1, 1, 2, 4, 3, 1])
conversion_dictionary = {1: 2, 2: 5, 3: 4, 4: 4}
converted_vals = [conversion_dictionary[label] for label in labels]
print(tf.eval(converted_vals))
I found the tf.case
function that might be suitable here but I couldn't figure out how to use it.
So my question is - how to convert between sets of values in tensorflow?
Another approach which may suit your particular use-case (continuous range of uint
labels), converting your dictionary into a vector (dict key ➜ vector index):
conversion_vector = [conversion_dictionary[i + 1] for i in range(len(conversion_dictionary))]
conversion_vector = tf.constant(conversion_vector, dtype=tf.int32)
converted_vals = tf.gather(conversion_vector, (labels - 1))
(Note: i + 1
and labels - 1
are to compensate for your labels starting from 1
, not 0
)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments