How to re-write tensorflows tf.reduce_sum in python using linear algebra operations?

时间:2019-04-16 23:17:10

标签: python tensorflow linear-algebra

I'm trying to rewrite the operation tf.reduce_sum operation for

tf.reduce_sum(tf.square(self.kernel), [0,1,2], keepdims=False)

To use basic linear algebra operations only. The context for doing this is because I'm trying to convert the model to tensorrt which currently doesn't support the reduce_sum operation.

Since the reduce_sum operator is pretty much just a linear operation, there should be a way to write it using much more basic operations. Unfortunately i;m not skilled enough in linear algebra or tensorflow to do this myself and hope to get some help.

Thanks a lot in advance!

1 个答案:

答案 0 :(得分:0)

result = numpy.sum(square_output_tensor.numpy(), axis=(0,1,2), keepdims=False)

This assumes you are working in Eager Mode, otherwise the method call .numpy() will not work. Instead for Graph Mode you would use tf.py_func instead.