草庐IT

一篇普通的bug日志——bug的尽头是next吗?

清风莫追 2024-04-19 原文

文章目录


[bug 1] TypeError: ‘method’ object is not subscriptable

问题代码:

print(item.full_name(), x.shape, item.parameters()[0].shape, item.parameters[1].shape)

原因:
parameters后面少了个括号。


[bug 2] TypeError: unsupported format string passed to numpy.ndarray.format

这段报错可以用如下代码重现出来:

import numpy as np
x = np.array([1.])
print('{:4f}'.format(x))

原因:
传给{:4f}的应该是一个浮点数数值,而 x 是 numpy 的数组,于是类型不匹配。我们只需将 x 转为浮点型即可,正确代码如下:

import numpy as np
x = np.array([1.])
x = float(x)
print('{:4f}'.format(x))

[bug 3] ValueError:Hint: Expected dtype() == paddle::experimental::CppTypeToDataType::Type()

描述
学习预训练模型的 fine-tune 时,将 AI Studio 上能跑的代码拷下来,到本地就报错了,我真的一脸懵。

当时上网查 ValueError,大多说将 ‘float64’ 换成 'float32‘,但我将输入的特征astype('float32')后,还是没用。

报错信息

ValueError: (InvalidArgument) The type of data we are trying to retrieve does not match the type of data currently contained in the container.
  [Hint: Expected dtype() == paddle::experimental::CppTypeToDataType<T>::Type(), but received dtype():5 != paddle::experimental::CppTypeToDataType<T>::Type():7.] (at ..\paddle\phi\core\dense_tensor.cc:137)
  [operator < accuracy > error]

解决

最后发现要改的是这里:

return im, int(grt)

int是 python 内置的数据类型,我将它转成 numpyint64 就好了。真没想到,python 内置的 int 居然不行。

# grt原本是给字符串
grt = np.int64(int(grt))
return im, grt

[bug 4] CondaSSLError: Encountered an SSL error.

使用 pipconda 安装 python 包时,如果开了梯子,可能会出现这样的报错,把梯子关掉就好了。

完整的报错如下:

CondaSSLError: Encountered an SSL error. Most likely a certificate verification issue.
Exception: HTTPSConnectionPool(host='mirrors.tuna.tsinghua.edu.cn', port=443):
 Max retries exceeded with url: /anaconda/pkgs/main/win-64/current_repodata.json
  (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol(_ssl.c:1125)')))

翻译:


CondaSSLError:遇到 SSL 错误。很可能是证书验证问题。 
异常:HTTPSConnectionPool(host='mirrors.tuna.tsinghua.edu.cn', port=443):
超出最大重试次数,网址为:/anaconda/pkgs/main/win-64/current_repodata.json 
(由 SSLError(SSLEOFError(8'EOF 发生违反协议 (_ssl.c:1125)')))

[bug 5] pip install paddleclas失败

描述
一开始看到好像是faiss包的问题,一顿操作之后,我把 faiss 包下下来了,但还是无法下载 paddleclas。这应该是 python 版本的问题,现在的版本是 python3.10.6,但我之前在 python3.8.4 的版本下使用 pip 下载成功了。

处理方案
从 github 上下载 PaddleClas 的代码库后,直接 import 导入,跳过 pip 这一步。

import sys
sys.path.append('D:/code_all/gitCode/PaddleClas')  # 这里是代码库的路径
import ppcls

可能编辑器会提示 "没有名称为'ppcls'的模块”,但不用管,只要运行时不报错就行。因为sys.path.append()在运行的时候才会执行,而在它执行之前你确实没有 ppcls 这个包。


[bug 6] 想删除原来的文件夹后新建一个(逻辑错误)

错误代码片段:

import os
import shutil

isExist = os.path.exists(saveDir)
if isExist and deleteOld:
    shutil.rmtree(saveDir)
if not isExist:
    os.makedirs(saveDir)

但如果原本路径saveDir存在,则isExistTrue,执行删除,但是后面isExist依然为True,并不会重新创建路径。

修改:创建前再插入一段判断。

isExist = os.path.exists(saveDir)
if isExist and deleteOld:
    shutil.rmtree(saveDir)
isExist = os.path.exists(saveDir)
if not isExist:
    os.makedirs(saveDir)

有关一篇普通的bug日志——bug的尽头是next吗?的更多相关文章

  1. ruby - Ruby 中 .next 和 .succ 的区别 - 2

    Ruby中的Fixnum方法.next和.succ有什么区别?看起来它的工作原理是一样的:1.next=>21.succ=>2如果有什么不同,为什么有两种方法做同样的事情? 最佳答案 它们是等价的。Fixnum#succ只是Fixnum#next的同义词。他们甚至在thereferencemanual中共享同一block. 关于ruby-Ruby中.next和.succ的区别,我们在StackOverflow上找到一个类似的问题: https://stacko

  2. ruby - Sinatra 中的全局救援和日志记录异常 - 2

    如何在出现异常时指定全局救援,如果您将Sinatra用于API或应用程序,您将如何处理日志记录? 最佳答案 404可以在not_found方法的帮助下处理,例如:not_founddo'Sitedoesnotexist.'end500s可以通过调用带有block的错误方法来处理,例如:errordo"Applicationerror.Plstrylater."end错误的详细信息可以通过request.env中的sinatra.error访问,如下所示:errordo'Anerroroccured:'+request.env['si

  3. ruby-on-rails - 使用 Ruby 标准 Logger 每天只创建一个日志 - 2

    我正在使用ruby​​标准记录器,我想要每天轮换一次,所以在我的代码中我有:Logger.new("#{$ROOT_PATH}/log/errors.log",'daily')它运行完美,但它创建了两个文件errors.log.20130217和errors.log.20130217.1。如何强制它每天只创建一个文件? 最佳答案 您的代码对于长时间运行的应用程序是正确的。发生的事情是您在给定的一天多次运行代码。第一次运行时,Ruby会创建一个日志文件“errors.log”。当日期改变时,Ruby将文件重命名为“errors.log

  4. ruby - Cucumber/Savon 省略或删除日志输出 - 2

    在运行Cucumber测试时,我得到(除了测试结果)大量调试/日志相关的输出形式:D,[2013-03-06T12:21:38.911829#49031]DEBUG--:SOAPrequest:D,[2013-03-06T12:21:38.911919#49031]DEBUG--:Pragma:no-cache,SOAPAction:"",Content-Type:text/xml;charset=UTF-8,Content-Length:1592W,[2013-03-06T12:21:38.912360#49031]WARN--:HTTPIexecutesHTTPPOSTusingt

  5. ruby-on-rails - faraday如何设置日志级别 - 2

    我最近将我的http客户端切换到faraday,一切都按预期工作。我有以下代码来创建连接:@connection=Faraday.new(:url=>base_url)do|faraday|faraday.useCustim::Middlewarefaraday.request:url_encoded#form-encodePOSTparamsfaraday.request:jsonfaraday.response:json,:content_type=>/\bjson$/faraday.response:loggerfaraday.adapterFaraday.default_ada

  6. 网站日志分析软件--让网站日志分析工作变得更简单 - 2

    网站的日志分析,是seo优化不可忽视的一门功课,但网站越大,每天产生的日志就越大,大站一天都可以产生几个G的网站日志,如果光靠肉眼去分析,那可能看到猴年马月都看不完,因此借助网站日志分析工具去分析网站日志,那将会使网站日志分析工作变得更简单。下面推荐两款网站日志分析软件。第一款:逆火网站日志分析器逆火网站日志分析器是一款功能全面的网站服务器日志分析软件。通过分析网站的日志文件,不仅能够精准的知道网站的访问量、网站的访问来源,网站的广告点击,访客的地区统计,搜索引擎关键字查询等,还能够一次性分析多个网站的日志文件,让你轻松管理网站。逆火网站日志分析器下载地址:https://pan.baidu.

  7. ruby - 如何更改 Sinatra 中的日志级别 - 2

    我正在使用此代码在我的Sinatra应用程序中启用日志记录:log_file=File.new('my_log_file.log',"a")$stdout.reopen(log_file)$stderr.reopen(log_file)$stdout.sync=true$stderr.sync=true实际的日志记录是使用:logger.debug("Startingcall.Params=#{params.inspect}")事实证明,只有INFO或更高级别的日志消息被记录,而DEBUG消息没有被记录。我正在寻找一种将日志级别设置为DEBUG的方法。 最佳

  8. ruby - 带有 grep 远程日志文件的 tail - 2

    我有这段代码来跟踪远程日志文件:defdo_tail(session,file)session.open_channeldo|channel|channel.on_datado|ch,data|puts"[#{file}]->#{data}"endchannel.exec"tail-f#{file}"endNet::SSH.start("host","user",:password=>"passwd")do|session|do_tailsession,"/path_to_log/file.log"session.loop我只想在file.log中检索带有ERROR字符串的行,我正在尝

  9. Ruby 守护进程日志轮换 - 2

    当我为Daemons(1.1.0)gem设置日志记录参数时,我将如何实现与此行类似的行为?logger=Logger.new('foo.log',10,1024000)守护进程选项:options={:ARGV=>['start'],:dir_mode=>:normal,:dir=>log_dir,:multiple=>false,:ontop=>false:mode=>:exec,:backtrace=>true,:log_output=>true} 最佳答案 不幸的是,Daemonsgem不使用Logger。它将STDOUT和S

  10. ruby-on-rails - 在 Rails 应用程序的前端获取实时日志 - 2

    在Rails3.x应用程序中,我正在使用net::ssh并向远程pc运行一些命令。我想向用户的浏览器显示实时日志。比如,如果两个命令在net中运行::ssh执行即echo"Hello",echo"Bye"被传递然后"Hello"应该在执行后立即显示在浏览器中。这是代码我在ruby​​onrails应用程序中使用ssh连接和运行命令Net::SSH.start(@servers['local'],@machine_name,:password=>@machine_pwd,:timeout=>30)do|ssh|ssh.open_channeldo|channel|channel.requ

随机推荐