在子类化keras层/模型时,如何正确使用`@ tf.function`?

时间:2019-10-04 09:41:52

标签: python tensorflow keras tensorflow2.0

我有一个自定义remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 ); ,它仅使用TF运算符进行某种位解包(将整数转换为布尔值(0或1浮点数))。

      public void setFocus(float x, float y){


            double angle1 = Math.atan2(tanks.get(currentTank).getyPos() - tanks.get(currentTank).getyPos() -100, tanks.get(currentTank).getxPos() - tanks.get(currentTank).getxPos());
            System.out.println(angle1);
            double angle2 = Math.atan2(y - tanks.get(currentTank).getyPos(), x - tanks.get(currentTank).getxPos());
            System.out.println(angle2);
            double angle = angle1-angle2;
            angle = angle +90;
            System.out.println(angle);


      tanks.get(currentTank).setRotation(angle);
}

我试图对这一层进行基准测试,以改善时间性能,如TF guide所示。

tf.keras.layers.Layer
class CharUnpack(keras.layers.Layer):

    def __init__(self, name="CharUnpack", *args, **kwargs):
        super(CharUnpack, self).__init__(trainable=False, name=name, *args, **kwargs)
        # Range [7, 6, ..., 0] to bit-shift integers
        self._shifting_range = tf.reshape(
            tf.dtypes.cast(
                tf.range(7, -1, -1, name='shifter_range'),
                tf.uint8,
                name='shifter_cast'),
            (1, 1, 8),
            name='shifter_reshape')
        # Constant value 0b00000001 to use as bitwise and operator
        self._selection_bit = tf.constant(0x01, dtype=tf.uint8, name='and_selection_bit')

    def call(self, inputs):
        return tf.dtypes.cast(
            tf.reshape(
                tf.bitwise.bitwise_and(
                    tf.bitwise.right_shift(
                        tf.expand_dims(inputs, 2),
                        self._shifting_range,
                    ),
                    self._selection_bit,
                ),
                [x if x else -1 for x in self.compute_output_shape(inputs.shape)]
            ),
            tf.float32
        )

    def compute_output_shape(self, input_shape):
        try:
            if len(input_shape) > 1:
                output_shape = tf.TensorShape(tuple(list(input_shape[:-1]) + [input_shape[-1] * 8]))
            else:
                output_shape = tf.TensorShape((input_shape[0] * 8,))
        except TypeError:
            output_shape = input_shape
        return output_shape

    def compute_output_signature(self, input_signature):
        return tf.TensorSpec(self.compute_output_shape(input_signature.shape), tf.float32)

如您所见,我的速度提高了10倍!!! 因此,我在inputs = tf.zeros([64, 400], dtype=tf.uint8) eager = CharUnpack() @tf.function def fun(x): eager(x) # Warm-up eager(inputs) fun(inputs) print("Function:", timeit.timeit(lambda: fun(inputs), number=100)) print("Eager:", timeit.timeit(lambda: eager(inputs), number=100)) 方法中添加了Function: 0.01062483999885444 Eager: 0.12658399900101358 装饰器:

@tf.function

现在我希望CharUnpack.call+ @tf.function def call(self, inputs): return tf.dtypes.cast( 的通话时间都差不多,但是我没有任何改善。

eager

此外,此SO answer的2.1节指出,默认情况下,模型是图形编译的(应该是逻辑的),但这似乎并非如此...

如何正确使用fun装饰器使我的图层始终进行图形编译?

0 个答案:

没有答案