首页 > 编程问答 >Tensorflow 除法计算结果为无穷大

Tensorflow 除法计算结果为无穷大

时间:2024-07-30 06:01:48浏览次数:11  
标签:python tensorflow division

我是 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:

  1. 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.

  1. 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 like tf.math.reduce_logsumexp which are designed to handle large numbers and prevent overflow errors.

  2. 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.

  3. 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.

标签:python,tensorflow,division
From: 41403694

相关文章

  • 如何修复我的 Python Azure Function DevOps Pipeline 上的“找到 1 个函数(自定义)加载
    我正在尝试使用AzureDevOps构建管道将PythonAzureFunction部署到Azure门户。由于某种原因,代码被部署到服务器,但我在尝试访问端点时收到404错误。我收到一个错误,显示1functionsfound(Custom)0functionsloaded,以及在服务器上显示ModuleNotFound......
  • 使用 kivy 从 python 脚本的 buildozer 构建 android apk 时出错
    我想从使用kivy包构建的Python脚本构建apk为此,我使用googlecollab.这里是main.py脚本:importyoutube_dlfromkivy.appimportAppfromkivy.uix.boxlayoutimportBoxLayoutfromkivy.uix.buttonimportButtonfromkivy.uix.tex......
  • 自动解码并检索 S/MIME 加密电子邮件的正文 (python)
    我如何:用python代码连接我的邮件收件箱以自动获取未读电子邮件的加密内容;解码S/MIME加密电子邮件(我有密钥);检索电子邮件正文纯文本;检查正文(或主题)是否与某个关键字(现在为“test”)匹配,并在匹配时打印一些内容;在树莓派上使用此代码,无需手动......
  • Python 3 写入 DBF(带有 Memo 的 dBase IV)
    我需要在Python3中写入带有备注字段的dBaseIVdbf文件,但找不到合适的模块来执行此操作。我尝试过的包:Simpledbf-只读dbf-不支持dBaseIVdbfpy-不支持Python3dbfpy3-不支持dBaseIVYDbf-不支持备注字段pyshp-无法仅使用dbf文件......
  • Robin-Stocks Python 中的 order_buy_fractional_by_price 问题
    我在Robin-StocksPython包中的order_buy_fractional_by_price函数中遇到问题。在正常市场交易时间内下达以美元为基础的买入订单时,该订单被错误地设置为限价订单。对我来说看起来有问题的代码似乎是导致此问题的原因。我尝试在包管理器中本地修改或删除有问题的代码,但遇......
  • 在python中使用turtle绘制图案(带点)
    我正在尝试使用python中的海龟制作一幅赫斯特画(点图案)。我设法实现了它。Hirst_painting_dot_pattern但是我的for循环没有按照我预期的方式工作。它省略了最后一次迭代。在下面的代码中,我的for循环没有生成最后一个点。因此,我在循环末尾添加了一行来制作最后......
  • 使用 Python 进行 QuantLib Vanilla 掉期定价 - 错误
    我真的需要帮助...我有一个使用QuantLib构建自己的VanillaSwapPricer的项目。我想根据ois掉期的市场价格进行计算以进行贴现,并根据Euribor6M掉期+FRA进行预测固定。总而言之,我的目标是尽可能接近彭博社对标准Euribor6M掉期的定价(贴现ois)-fwdEuribor6M)。......
  • 我正在制作一个可以打开wav文件的python程序,我想知道wav文件的格式是什么
    因此,我已经通过此网站的研究编写了验证并读取wav标头的代码。但我想知道,data段中的数据是如何存储的?它们位于16位部分中,彼此相邻放置。我认为在Audacity中制作440hz正弦波,然后导出它,会显示一些结果,并且字节确实看起来更整齐,但仍然像废话一样接缝。相信我,我已经......
  • python - 面板库 - PasswordInput 不会对回车做出反应
    我试图避免需要提交按钮。以下代码当前正在远程jupyter实验室运行。仅当光标焦点从密码小部件中移除后,才会打印该消息。我想要回车来触发消息打印。有什么线索吗?frompanel.widgetsimportPasswordInput,TextInputpn.extension()defon_enter(event=None):message_p......
  • 即使安装了软件包,也找不到 python 模块句子转换器
    对于我的python脚本(如下所示),我使用句子转换器包,其中包含SBERT模型。即使在执行“piplist”时明确列出该软件包,并且还安装了torch,所有内容都更新到最新版本,但仍然找不到该模块。脚本:importnumpyasnpimportpandasaspdfromsentence_transformersimportSenten......