草庐IT

解决OSError: Error no file named pytorch_model.bin, tf_model.h5 found in directory

Y_soserious 2023-06-21 原文

问题:

OSError: Error no file named pytorch_model.bin, tf_model.h5, model.ckpt.index or flax_model.msgpack found in directory

出现过程:

使用transformers的Bertmodel时

问题代码:

model = Bertmodel.from_pretrained('bert_model')

问题原因:

下载的模型文件夹中没有pytorch_model.bin/tf_model.h5等文件,去原网址中查找也未发现相关文件。继而查看transformers官方使用说明:

from_pretrained(pretrained_model_name_or_path*model_args**kwargs)

  • pretrained_model_name_or_path – either: - a string with the shortcut name of a pre-trained model to load from cache or download, e.g.: bert-base-uncased. - a string with the identifier name of a pre-trained model that was user-uploaded to our S3, e.g.: dbmdz/bert-base-german-cased. - a path to a directory containing model weights saved using save_pretrained(), e.g.: ./my_model_directory/. - a path or url to a PyTorch state_dict save file (e.g. ./pt_model/pytorch_model.bin). In this case, from_pt should be set to True and a configuration object should be provided as config argument. This loading path is slower than converting the PyTorch checkpoint in a TensorFlow model using the provided conversion scripts and loading the TensorFlow model afterwards.

即:如果是加载用save_pretrained保存过的模型,需要加入参数from_pt/from_tf和相应的config

解决方法:

# 模型文件夹中的config文件路径
config = BertConfig.from_json_file('./tf_model/my_tf_model_config.json')  
# 如果下载的是tensorflow模型,则from_tf=True;如果是pytorch模型,则参数设置改为from_pt=True
model = BertModel.from_pretrained('./tf_model/my_tf_checkpoint.ckpt.index', from_pt=True, config=config)  

有关解决OSError: Error no file named pytorch_model.bin, tf_model.h5 found in directory的更多相关文章

  1. ruby-on-rails - rails : keeping DRY with ActiveRecord models that share similar complex attributes - 2

    这似乎应该有一个直截了当的答案,但在Google上花了很多时间,所以我找不到它。这可能是缺少正确关键字的情况。在我的RoR应用程序中,我有几个模型共享一种特定类型的字符串属性,该属性具有特殊验证和其他功能。我能想到的最接近的类似示例是表示URL的字符串。这会导致模型中出现大量重复(甚至单元测试中会出现更多重复),但我不确定如何让它更DRY。我能想到几个可能的方向...按照“validates_url_format_of”插件,但这只会让验证干给这个特殊的字符串它自己的模型,但这看起来很像重溶液为这个特殊的字符串创建一个ruby​​类,但是我如何得到ActiveRecord关联这个类模型

  2. ruby-on-rails - rails : check if the model was really saved in after_save - 2

    ActiveRecord用于在每次调用保存方法时调用after_save回调,即使模型没有更改并且没有生成插入/更新查询也是如此。这实际上是默认行为。在大多数情况下这没问题。但是一些after_save回调对模型是否实际保存的事情很敏感。有没有办法确定模型是否实际保存在after_save中?我正在运行以下测试代码:classStage 最佳答案 ActiveRecordusetocallafter_savecallbackeachtimesavemethodiscalledevenifthemodelwasnotchangedan

  3. ruby-on-rails - 工厂女孩/Rails : Generator to create a factory for existing model? - 2

    我在我的Rails项目中使用rspec_rails和factory_girl_railsgem。所有模型都已创建。是否有我可以运行的生成器来为现有模型创建工厂文件?例如:我已经有了一个Blog模型。RSpec允许我通过简单地运行以下命令在spec/models/blog_spec.rb生成一个模型规范文件:railsgeneraterspec:modelblog是否有我可以在命令行中运行的生成器,它会为这个现有模型生成工厂文件,位于:spec/factories/blogs.rb?我在factory_girl_rails中没有看到任何关于发电机的提及文档。

  4. ruby - 如何修复 RVM gem "[ missing bin/ruby ]" - 2

    执行rvmlist后,我得到以下输出:rvmrubiesgems[missingbin/ruby]=*ruby-2.0.0-p645[x86_64]ruby-2.1.6[x86_64]ruby-2.2.1[x86_64]gems[missingbin/ruby]是什么意思?gems是某种系统gemset吗?它不是我创建的,我不知道我是否可以或应该删除它。 最佳答案 在我跑完之后:rvmfix-permissions然后我能够卸载具有[缺少bin/ruby]的版本。 关于ruby-如何修复

  5. ruby-on-rails - ruby rails : two references with different name to the same model - 2

    我的应用程序有一个名为User的模型(它包括电子邮件地址、用户名……)我想创建一个模型Message它应该有两个字段sender和recipient。两者都是对User模型的引用。我试过这个:railsgeneratemodelMessagesender:referencesrecipient:referencesRails生成了这个:classMessage但我不想要两种不同的模型。这两个字段都应引用User。我正在运行Ruby2.0.0和Rails4.0.2。非常感谢任何帮助。如果您需要有关我的问题的更多信息,请询问我。 最佳答案

  6. ruby-on-rails - NilClass :Class 的 Rails 未定义方法 `model_name' - 2

    我有一个表单,我想出现在每个页面的顶部,所以我将它包含在/app/views/layouts/application.html.erb文件中,但我收到错误undefinedmethodmodel_name'forNilClass:Class`尝试加载页面时。这是application.html.erb中的表单片段Addnewcontact这是我的/app/controllers/user_controller.rbclassUserController我认为我遇到了这个错误,因为表单位于application.html.erb文件中,我需要以某种方式指定路径,但话又说回来,我对Rail

  7. ruby-on-rails - 'bin'中的 '$ bin/rails?'是什么意思 - 2

    我试图按照“引擎入门”Rails指南的教程进行操作,发现了这个:$bin/railspluginnewblorgh--mountable当我在我的终端上输入这个时,我得到:bash:bin/rails:Nosuchfileordirectory当我只尝试$rails时,它工作正常。几个月来我一直在使用命令行,还有Rails。我知道$符号,但我对之后“bin”的含义感到困惑。我习惯于在$后面输入“Rails”,但本教程说使用“bin”...bin/是什么意思? 最佳答案 tl;dr运行bundleinstall--binstubs它将

  8. ruby-on-rails - 启动 Rails 服务器时出错 - 获取 "uninitialized constant Devise::Models::Invitable (NameError)" - 2

    我是Rails的新手,正在尝试按照这个预启动注册教程进行操作-http://railsapps.github.com/tutorial-rails-prelaunch-signup.html使用$railss初始化rails服务器时,我收到以下错误消息:Users/pv/.rvm/gems/ruby-1.9.3-p194@rails326/gems/devise-2.1.2/lib/devise/models.rb:97:in`const_get':未初始化常量设计::模型::邀请(NameError)后面跟着一堆文件路径,比如:from/Users/patrickvihtelic/.

  9. ruby-on-rails - Ruby on Rails : 4. 2.0 rails 和 bin/rails 的区别 - 2

    所以从Rails4.1.x开始,似乎有一种推荐的方法是在应用程序文件夹下使用rails。而不是传统的:railsserverRails官方指南推荐使用bin/railsserver看起来bin/rails正在引用带有附加内容的rails。与rails相比,使用bin/rails的额外好处是什么?第二个问题是——我习惯于使用railsserver、railsconsole等,而不是bin/railsserver、bin/railsconsole。如果不使用bin/rails,我会丢失任何东西吗(比如误加载一些库等)?谢谢。 最佳答案

  10. sql - 如何模拟ActiveRecord Model.count.to_sql - 2

    我想显示一个计数中使用的SQL。但是,Model.count.to_sql将不起作用,因为count返回一个没有to_sql方法的FixNum。我认为最简单的解决方案是这样做:Model.where(nil).to_sql.sub(/SELECT.*FROM/,"SELECTCOUNT(*)FROM")这会创建与Model.count中使用的SQL相同的SQL,但它是否会导致进一步的问题?例如,如果我添加一个复杂的where子句和一些连接。有更好的方法吗? 最佳答案 你可以试试Model.select("count(*)asmode

随机推荐