我偶然发现了一些奇怪的张量流行为。在 tf.print 无处不在之后,它导致我出现以下代码所示的原因,但不知道为什么会发生这种情况,除非线程竞争条件或图形构造省略了代码段。看不到它们中的任何一个都应该发生。
# Ragged tensor may have empty rows. So, for tensor arithmetic operation,
# we need to create zero-padded tensors to replace them.
# This implementation only keeps the first entry of each row.
# So, the output tensor is a normal tensor.
def pad_empty_ragged_tensor(ragtensor):
tf.print("Ragged tensor padding empty tensor...", output_stream=sys.stdout)
batch_size = ragtensor.shape[0]
n_rows = ragtensor.row_lengths()
tf.print("row_lengths(): ", n_rows, output_stream=sys.stdout)
new_tensor = []
for i in range(batch_size):
tf.print("n_rows[i]: ", n_rows[i], output_stream=sys.stdout)
if tf.equal(n_rows[i], 0): # Tried n_rows[i] == 0 too
tf.print("Create zero padded tensor...", output_stream=sys.stdout)
num_zeros = ragtensor.shape[-1]
tensor = tf.tile([[0]], [1, num_zeros])
tensor = tf.cast(tensor, dtype=ragtensor.dtype)
else:
tf.print("Take first entry from the row", output_stream=sys.stdout)
tensor = ragtensor[i,0:1]
new_tensor.append(tensor)
tensor = tf.stack(new_tensor, axis=0) # [batch, 1, [y, x, h, w]]
tensor.set_shape([batch_size, 1, ragtensor.shape[-1]])
tf.print("The padded tensor shape: ", tensor.shape, output_stream=sys.stdout)
return tensor
这是打印跟踪的一部分:
row_lengths(): [1 1 0 ... 1 1 1]
n_rows[i]: 1
Take first entry from the row
n_rows[i]: 1
Take first entry from the row
n_rows[i]: 0
Take first entry from the row
n_rows[i]: 1
Take first entry from the row
如图所示,if tf.equal(n_rows[i], 0): # Tried n_rows[i] == 0 too
条件块从未被调用。即使满足相等条件,它也每次都落入“其他”条件。谁能提示我出了什么问题?顺便说一句,调试 tensorflow 运行时也很困难。一旦图形执行运行,VSCode 中的断点就没有命中。tfdbg 也不适用于急切执行。对此的建议对我也非常有益。
我的开发环境:
- 操作系统:Ubuntu18.04
- 蟒蛇:3.6
- 张量流GPU:1.14
- 显卡:RTX2070
- 库达:10.1
- cudnn: 7.6
- IDE:VS 代码
- TensorFlow 模式:渴望执行
提前致谢