Code Monkey home page Code Monkey logo

Comments (8)

Qiaojiao-225 avatar Qiaojiao-225 commented on May 13, 2024 3

您好,问题似乎是由于在您的二分类任务上,同时应用Adam优化器和半精度浮点训练数值不稳定,导致clip中bert的word embedding层参数在第一次梯度更新后变为nan。 您可以参考以下任一方式解决问题: 1.将CLIP模型设置为单精度浮点,例如在CLIP模型初始化时设置clip_model = clip_model.float() 2.将Adam优化器的eps参数指定为1e-3,例如optimizer = torch.optim.Adam(model.parameters(), lr=xxx, eps=1e-3) 3.将Adam优化器更换为SGD优化器

好的!我的问题解决了。真的很感谢您在这个问题上的投入和耐心解答,像您这样有经验的开发者为我们提供了无价的帮助,感谢您为开源社区做出的贡献。

from chinese-clip.

DtYXs avatar DtYXs commented on May 13, 2024 2

冻结CLIP训练时是正常的, 解冻CLIP后, model.encode_text(clip_tokens)会输出NaN. 我在forward()中添加了类似assert not torch.isnan(clip_tokens).any(), "Nan"assert not torch.isinf(clip_tokens).any(), "Inf"对输入数据检查, 输入数据中没有包含NaN和Inf. 调小学习率1e-8仍然会输出NaN. 我的任务是文本二分类, 难道是这个任务不适合进行微调吗?

您好,问题似乎是由于在您的二分类任务上,同时应用Adam优化器和半精度浮点训练数值不稳定,导致clip中bert的word embedding层参数在第一次梯度更新后变为nan。
您可以参考以下任一方式解决问题:
1.将CLIP模型设置为单精度浮点,例如在CLIP模型初始化时设置clip_model = clip_model.float()
2.将Adam优化器的eps参数指定为1e-3,例如optimizer = torch.optim.Adam(model.parameters(), lr=xxx, eps=1e-3)
3.将Adam优化器更换为SGD优化器

from chinese-clip.

DtYXs avatar DtYXs commented on May 13, 2024

您好,根据您的描述您应该是在Chinese-CLIP的基础上添加一些层然后在自己的任务上进行训练。请问您一开始冻结CLIP时是正常训练,解冻CLIP之后出现的问题吗?
由于不知道您具体的代码,建议您先检查一下您输入数据中是否存在问题,如果输入数据中包含NaN或无穷大的值,则模型输出也可能为NaN。
另外需要注意使用归一化后的图文特征用于下游任务:
text_features = model.encode_text(text)
text_features /= text_features.norm(dim=-1, keepdim=True)
您还可以尝试适当减小一下对CLIP的学习率,看看能否解决问题

from chinese-clip.

Qiaojiao-225 avatar Qiaojiao-225 commented on May 13, 2024

冻结CLIP训练时是正常的, 解冻CLIP后, model.encode_text(clip_tokens)会输出NaN.
我在forward()中添加了类似assert not torch.isnan(clip_tokens).any(), "Nan"assert not torch.isinf(clip_tokens).any(), "Inf"对输入数据检查, 输入数据中没有包含NaN和Inf.
调小学习率1e-8仍然会输出NaN.
我的任务是文本二分类, 难道是这个任务不适合进行微调吗?

from chinese-clip.

Qiaojiao-225 avatar Qiaojiao-225 commented on May 13, 2024

但是,我调用Huggingface transformers的Chinese CLIP模型可以在我的任务上训练,只有clip模型引入和调用的方式不同,其他代码完全一样,我不知道是什么原因.

from transformers import ChineseCLIPModel
self.clip_model = ChineseCLIPModel.from_pretrained("OFA-Sys/chinese-clip-vit-base-patch16")

from chinese-clip.

DtYXs avatar DtYXs commented on May 13, 2024

您好,请问是训练一段时间后出现Nan的情况吗,您方便提供训练的log以及输出为Nan时的输入文本原始数据吗

from chinese-clip.

Qiaojiao-225 avatar Qiaojiao-225 commented on May 13, 2024

您好,请问是训练一段时间后出现Nan的情况吗,您方便提供训练的log以及输出为Nan时的输入文本原始数据吗

是第1个batch更新参数后, 第2个batch里clip_encode_text()输出Nan.
方便, 给我您的邮箱吧.

from chinese-clip.

DtYXs avatar DtYXs commented on May 13, 2024

您好,请问是训练一段时间后出现Nan的情况吗,您方便提供训练的log以及输出为Nan时的输入文本原始数据吗

是第1个batch更新参数后, 第2个batch里clip_encode_text()输出Nan. 方便, 给我您的邮箱吧.

好的,您可以发送到[email protected]

from chinese-clip.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.