Behzad
Behzad

Reputation: 1

How to define a layer only for training phase in TensorFlow?

I wanted to know if it's possible to define a layer (convolution, element-wise summation, etc.) only for the training phase in TensorFlow.

For example, I want to have an element-wise summation layer in my network only for the training phase and I want to ignore this layer in the test phase.

This is easily doable in Caffe, I wanted to know if it's possible to do so in TensorFlow as well.

Upvotes: 0

Views: 992

Answers (3)

soloice
soloice

Reputation: 1040

I think you can use a boolean placeholder with tf.cond(). Just like this:

train_phase = tf.placeholder(tf.bool, [])
x = tf.constant(2)
def f1(): return tf.add(x, 1)
def f2(): return tf.identity(x)
r = tf.cond(train_phase, f1, f2)
sess.run(r, feed_dict={train_phase: True})  # training phase, r = tf.add(x, 1) = x + 1
sess.run(r, feed_dict={train_phase: False})  # testing phase, r = tf.identity(x) = x

Upvotes: 1

rmeertens
rmeertens

Reputation: 4451

You might want to do this with the "tf.cond" control_flow operation. https://www.tensorflow.org/api_docs/python/control_flow_ops/control_flow_operations#cond

Upvotes: 1

xxi
xxi

Reputation: 1510

I think you can do this by if

Train = False

x = tf.constant(5.)
y = x + 1
if Train:
    y = y + 2
y = y + 3

with tf.Session() as sess:
    res = sess.run(y) # 11 if Train else 9

Upvotes: -1

Related Questions