Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select different modes by string in Tensorflow

I am trying to build a VAE network in which I want the model to do different things in different modes. I have three modes: "train", "same" and "different" and a function named interpolation(mode) that does different things depend on the mode. My code looks like:

import tensorflow as tf

### some code here

mode = tf.placeholder(dtype = tf.string, name = "mode")

def interpolation(mode):
  if mode == "train":
    # do something
    print("enter train mode")
  elif mode == "same":
    # do other things
    print("enter same mode")
  else:
    # do other things
    print("enter different mode")

# some other code here

sess.run(feed_dict = {mode: "train"})
sess.run(feed_dict = {mode: "same"})
sess.run(feed_dict = {mode: "different"})

But the output looks like:

enter different mode
enter different mode
enter different mode

which means the mode that gets passed in doesn't change the condition. What have I done wrong? How do I select mode by string argument?

like image 378
Steven Chan Avatar asked Sep 14 '26 08:09

Steven Chan


1 Answers

First approach: You can select a different mode by using native Tensorflow switch-case. For example, I assume you have three cases, then you can do:

import tensorflow as tf

mode = tf.placeholder(tf.string, shape=[], name="mode")


def cond1():
    return tf.constant('same')


def cond2():
    return tf.constant('train')


def cond3():
    return tf.constant('diff')


def cond4():
    return tf.constant('default')


y = tf.case({tf.equal(mode, 'same'): cond1,
             tf.equal(mode, 'train'): cond2,
             tf.equal(mode, 'diff'): cond3},
            default=cond4, exclusive=True)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(y, feed_dict={mode: "train"}))
    print(sess.run(y, feed_dict={mode: "same"}))

Second approach: here is another way to do this with new AutoGraph API:

import tensorflow as tf
from tensorflow.contrib import autograph as ag

m = tf.placeholder(dtype=tf.string, name='mode')


def interpolation(mode):
    if mode == "train":
        return 'I am train'
    elif mode == "same":
        return 'I am same'
    else:
        return 'I am different'


cond_func = ag.to_graph(interpolation)(m)
with tf.Session() as sess:
    print(sess.run(cond_func, feed_dict={m: 'same'}))
like image 181
Amir Avatar answered Sep 15 '26 23:09

Amir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!