在 运行 时间使用张量的值修改另一个张量

Use tensor's value to modify another tensor at run-time

在 TensorFlow 中,我想将 "fake" 量化函数之一应用于我的张量之一。具体来说,我对使用 this 函数感兴趣:

fake_quant_with_min_max_args(
    inputs,
    min=-6,
    max=6,
    num_bits=8,
    narrow_range=False,
    name=None
    )

开箱即用。理想情况下,我想根据输入张量调整 minmax 参数。具体来说,我想使用 99.7% rule 来定义该范围。换句话说,我想使用值的范围,如果将输入张量表示为一维向量,则其 99.7% 的元素将位于 [mean-3*std, mean + 3 *std] 范围。

为此,我执行以下操作:

def smartFakeQuantization(tensor):
    # Convert the input tensor to a 1-d tensor
    t_1d_data = tf.reshape(tensor,[tf.size(tensor), 1])
    # get the moments of that tensor. Now mean and var have shape (1,)
    mean, var = tf.nn.moments(t_1d_data, axes=[0])
    # get a tensor containing the std
    std = tf.sqrt(var)

    < some code to get the values of those tensors at run-time>

    clip_range = np.round([mean_val - 3*std_val, mean_val + 3*stdstd_val], decimals=3)
    return tf.fake_quant_with_min_max_args(tensor, min=clip_range[0], max=clip_range[1])

我知道我可以通过执行以下操作评估图中的任何张量:myTensor.eval()mySession.run(myTensor) 但是如果我在上面的函数中添加这些类型的线,它会在执行图时崩溃.我会收到以下形式的错误:

tensor <...> has been marked as not fetchable.

我遵循的步骤可能不适合 TensorFlow 的 "graph" 性质。任何想法如何做到这一点?总结一下,我想用一个张量在运行-时间的值来修改另一个张量。我会说这个问题比 tf.cond().

可以解决的问题更复杂

我认为没有一种简单的方法可以做您想做的事。 fake_quant_with_min_max_argsminmax 参数被转换为操作属性并用于底层内核的 construction. They cannot be changed at runtime. There are some (seemingly not part of public API) ops(请参阅 LastValueQuantizeMovingAvgQuantize)调整他们的时间间隔取决于他们看到的数据,但他们并没有完全按照你的意愿去做。

您可以编写自己的自定义操作,或者如果您认为这通常很有价值,请在 github 上提交功能请求。

您可以使用 tf.fake_quant_with_min_max_vars,它接受张量作为 min/max 参数:

return tf.fake_quant_with_min_max_vars(tensor, min=mean-3*std, max=mean+3*std)