我是 Tensorflow 的新手,对于明显的错误深表歉意。
我尝试进行一个评估为 inf 的除法。下面的行产生了问题:
style_loss1_1 = tf.div(tf.reduce_sum(tf.pow(tensor_gen_gram1_1 - tensor_style_gram1_1, 2)),
tf.mul(tf.mul(4.0, 2517630976.0), 9.0))
我知道 i 只乘以数字,但如果这有效,我想用 Tensorflow 变量替换它们。所以我需要这个乘法。 让我发疯的是,当我将这一行更改为这一行时:
style_loss1_1 = tf.div(tf.reduce_sum(tf.pow(tensor_gen_gram1_1 - tensor_style_gram1_1, 2)),
1.0)
一切正常,我得到结果:7.41678e+19
我将感谢任何帮助
The issue you are facing is likely due to an overflow error occurring during the multiplication in your original code:
tf.mul(tf.mul(4.0, 2517630976.0), 9.0)
This calculation results in a very large number (90634715136.0) which, when used as the denominator in your division, can lead to an extremely small number that TensorFlow might approximate as zero. Dividing by a very small number (close to zero) results in an overflow, hence the
inf
(infinity) result.
Here's why your second code snippet works:
tf.div(..., 1.0)
Dividing by 1.0 doesn't change the value, so you're effectively avoiding the overflow issue present in the original calculation.
Here are a few ways to address the overflow issue in your code:
- Scaling: You can try scaling down the numerator before performing the division. This can help prevent the intermediate calculations from becoming too large. For example:
python
scaled_numerator = tf.reduce_sum(tf.pow(tensor_gen_gram1_1 - tensor_style_gram1_1, 2)) / 1e10 # Scale down by 10^10
style_loss1_1 = tf.div(scaled_numerator, tf.mul(tf.mul(4.0, 2517630976.0), 9.0))
Remember to adjust the scaling factor (1e10 in this case) based on your specific data and desired precision.
-
Using TensorFlow's built-in functions for large numbers: If you need to work with extremely large numbers, consider using TensorFlow's
tf.float64
data type for higher precision, or explore using functions liketf.math.reduce_logsumexp
which are designed to handle large numbers and prevent overflow errors. -
Simplifying the calculation: Check if you can simplify the multiplication in your denominator. For example, if the numbers are constants, pre-calculate them outside the TensorFlow graph.
-
Analyzing the values: Print or debug the values of the tensors involved in the calculation to see the magnitude of the numbers and pinpoint the source of the overflow.
By applying one or more of these solutions, you should be able to avoid the overflow error and obtain the correct value for your
style_loss1_1
calculation in TensorFlow.