tf, cond () function

  tensorflow there are too many functions functions exactly like learning a new language, so I plan to adopt, the encounter will not function on record, as their function manual. This talk is tf, cond ().

tf, cond (A, B, C) as a conditional statement: if A: B else: C, for example:

z = tf.multiply(a, b)
result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))

  Examples of this official interpretation of what is, above all, to assign z a simple multiplication function, and then call tf, cond this function, which means that, if x <y, on the implementation of the function tf.add (x, z), otherwise on the implementation of tf.square (y). Here there liangdian Note:

  First, the conditions are determined by way of Run anonymous function;

  Second, because the calculation map is in the form of tensorflow, so in fact, regardless of the size of x and y, the latter two commands are run-off, according to only the last x, y size determines which way data flow diagram.

  tf, cond () function, generally to be able to read control codes, the code can know enough to simplify the code, like to know the specific details, forgets nothing. Here then set out two examples that I encountered:

# remove alpha channel if present
input_image = tf.cond(tf.equal(tf.shape(input_image)[2], 4), lambda: input_image[:,:,:3], lambda: input_image)
# convert grayscale to RGB
input_image = tf.cond(tf.equal(tf.shape(input_image)[2], 1), lambda: tf.image.grayscale_to_rgb(input_image), lambda: input_image)

 

Guess you like

Origin www.cnblogs.com/happy-sir/p/11530881.html