【问题标题】:Check if Tensor is Placeholder?检查张量是否是占位符?
【发布时间】:2019-02-27 12:19:10
【问题描述】:

占位符在 TensorFlow 中被识别为张量。

isinstance(tf.placeholder("float", []), tf.Tensor) 返回True

有没有办法专门检查张量是否是占位符?比如:

isinstance(tf.placeholder("float", []), tf.Placeholder)

不幸的是,对于我正在构建的 API,tf.Placeholder 不是 TensorFlow 中的实际实例类型。

【问题讨论】:

  • 这是我设法做到的一种丑陋的方式:import tensorflow as tf pl = tf.placeholder("float", []) sess = tf.Session() try: sess.run(pl) except tf.errors.InvalidArgumentError: print(pl.shape)

标签: python tensorflow tensor


【解决方案1】:

您可以通过op.type查看:

assert tf.placeholder("float", []).op.type == 'Placeholder'

【讨论】:

  • @SamLerman 没问题。如果它解决了您的问题,请考虑将答案标记为已接受。
猜你喜欢
  • 2013-06-13
  • 1970-01-01
  • 2017-12-01
  • 2012-01-06
  • 2013-03-01
  • 2016-05-24
  • 2012-05-15
  • 2021-05-14
  • 1970-01-01
相关资源
最近更新 更多