how to convert a set of values in tensorflow using tf.case ?

itzik Ben Shabat

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?

benjaminplanche

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.

edited at
0

Comments

0 comments
Login to comment

Related

How to quantize the values of tf.Variables in Tensorflow

How to set values using case statement in a select query?

How to use the tf.case api of TensorFlow correctly?

TensorFlow results are not reproducible despite using tf.random.set_seed

TensorFlow: How convert tf.Tensor string to python datetime datatype?

Using a case statement to set the values of declared variables

how to convert values to title case in jolt transform

How to convert values of a case class into Seq?

TF DATA API: How to produce tensorflow input to object set recognition

How to convert TF Tensor holding value into Tensor holding categorical values

How to set kernel values for Keras convolutional layer in TF 2.0?

how to convert dict_values into a set

How to convert Map values to a set in scala

How to convert Set containing values into string

how to convert from lower case to upper case by using pattern

how to get query values, using Case/eloquent

How to assign a value to a tf.Variable in TensorFlow without using tf.assign

How to convert set of string from map values of string using stream API in java

tensorflow pass numpy array to graph using placeholder vs tf.convert_to_tensor()

How to set values of variable using SET in MySql?

TensorFlow: convert tf.Dataset to tf.Tensor

How to convert the dictionary values to lower case in list comprehension?

How to convert data in a Set[Tuple2] into a case class

Tensorflow: create tf.NodeDef() and set attributes

How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert

How to convert `tf.contrib.lookup.index_table_from_file` into Tensorflow v2

How to do string-style split on tf tensor with string values using TF-1.4

Tensorflow: tf.cond, how to return multi dim tensors instead of simple values?

Convert a set of tuples into values