简体   繁体   English

如何在 Tensorflow2 中快速计算平方和?

[英]How to calculate squared sum quickly in Tensorflow2?

I wanna calculate this with huge input data.我想用大量的输入数据来计算这个。

I did it with tensorflow.我用张量流做到了。 but it is calculated one by one.但它是一一计算的。 so it is not fast enough.所以它不够快。 I tried to make fixed_mat and input_np like (1000, 101, 1088).我试图让 fixed_mat 和 input_np 像 (1000, 101, 1088)。 it didn't work... is there any suggestions to calculate huge input???它没有用......有什么建议来计算巨大的输入吗??? Thanks for advance.感谢提前。

fixed_mat.shape

(101, 1088) (101, 1088)

input_np.shape

(1000, 1088) (1000, 1088)

import tensorflow as tf
num = 2
res = tf.reduce_sum(tf.math.squared_difference(fixed_mat, 
np.array([input_np[1]] * fixed_mat.shape[0])), 1)
vals, indice = tf.nn.top_k(tf.negative(res), num)
print(list(indice.numpy()), list(- vals.numpy()))

[13, 90] [1.3422034332504837, 1.8790145150615656] [13, 90] [1.3422034332504837, 1.8790145150615656]

If you want to speed up tensorflow code, you should not use the eager mode.如果你想加速 tensorflow 代码,你不应该使用 Eager 模式。 It is best to use the @tf.function decorator最好使用@tf.function装饰器

See this guide: https://www.tensorflow.org/guide/function请参阅本指南: https ://www.tensorflow.org/guide/function

This code works:此代码有效:

@tf.function
def my_tf_function(fixed_mat, input_np, num):
    res = tf.reduce_sum(tf.math.squared_difference(
        fixed_mat,
        tf.gather(input_np, 1)), 1)
    vals, indice = tf.nn.top_k(tf.negative(res), num)
    return vals, indice

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM