当前位置: 首页> 教育> 幼教 > 苏州网站建设自助建站收费_人员优化方案_百度引流推广费用多少_模板建站价格

苏州网站建设自助建站收费_人员优化方案_百度引流推广费用多少_模板建站价格

时间:2025/7/13 15:37:29来源:https://blog.csdn.net/weixin_51552032/article/details/144822255 浏览次数:2次
苏州网站建设自助建站收费_人员优化方案_百度引流推广费用多少_模板建站价格

报错:
RuntimeError: stack(): functions with out=… arguments don’t support automatic differentiation, but one of the arguments requires grad.

或者

RuntimeError: Trying to backward through the graph a second time (or directly access saved tensors after they have already been freed). Saved intermediate values of the graph are freed when you call .backward() or autograd.grad(). Specify retain_graph=True if you need to backward through the graph a second time or if you need to access saved tensors after calling backward.

原因:

训练模型的时候,输入数据x,y不应该requires_grad,而bert模型输出的embeddings默认是requires_grad的,所以会报错。

解决方法:

提取完embeddings之后,使用 embeddings.detach() 解除绑定就行了。

最后的代码:

from transformers import BertTokenizer, BertModelclass BertFeatureExtractor:def __init__(self):self.tokenizer = BertTokenizer.from_pretrained('bert-base-chinese')self.model = BertModel.from_pretrained('bert-base-chinese')def extract_features(self, text: str):inputs = self.tokenizer(text, return_tensors="pt")if len(inputs["input_ids"]) > 512:inputs["input_ids"] = inputs["input_ids"][:512]inputs["attention_mask"] = inputs["attention_mask"][:512]outputs = self.model(**inputs)return outputs.last_hidden_state[:,0,:]feat = feat.detach()
关键字:苏州网站建设自助建站收费_人员优化方案_百度引流推广费用多少_模板建站价格

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: