首页 文章

如何在TensorFlow中仅为训练阶段定义图层?

提问于
浏览
0

我想知道是否可以仅为TensorFlow中的训练阶段定义一个层(卷积,元素求和等) .

例如,我想在我的网络中只有一个元素总和层用于训练阶段,我想在测试阶段忽略这一层 .

这在Caffe中很容易实现,我想知道TensorFlow是否也可以这样做 .

3 回答

  • 0

    您可能希望使用"tf.cond" control_flow操作执行此操作 . https://www.tensorflow.org/api_docs/python/control_flow_ops/control_flow_operations#cond

  • 1

    我认为你可以使用带有tf.cond()的布尔占位符 . 像这样:

    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
    
  • 1

    我想你可以通过 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
    

相关问题