pytorch RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long;

博客指出在pytorch中,将np.int64的数传给embedding时会报错,报错内容为期望参数为Long类型,实际为CPUType。解决办法是对该数进行数值转换,转为python内置int类型,可使用np.asscalar(np.int16(a))实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

错误:将一个np.int64的数传给embedding
报错内容
RuntimeError: Expected tensor for argument #1 ‘indices’ to have scalar type Long; but got CPUType instead (while checking arguments for embedding)

解决:对这个数进行数值转换,转为python内置int类型即可,np.asscalar(np.int16(a))即可。

from transformers import AutoTokenizer, AutoModelForVision2Seq import torch # 加载预训练模型和 tokenizer model_path = "E:\\Qwen\\qwen2.5vl" # 转义路径中的反斜杠 tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) model = AutoModelForVision2Seq.from_pretrained(model_path, trust_remote_code=True) # 设置模型为评估模式 model.eval() # 假设图像输入是一个 tensor,形状取决于模型的具体要求 image_input = torch.randn(1, 3, 224, 224) # 示例输入图像,batch_size=1, channel=3, height=224, width=224 # 文本输入部分 text_inputs = tokenizer("This is a sample text input", return_tensors="pt", padding=True, truncation=True) input_ids = text_inputs["input_ids"] attention_mask = text_inputs["attention_mask"] import torch.onnx # 定义输出文件名 onnx_model_path = "qwen2_5_vl.onnx" # 导出模型 dynamic_axes = { 'input_ids': {0: 'batch_size'}, # 动态维度 'attention_mask': {0: 'batch_size'}, 'output': {0: 'batch_size'} } try:torch.onnx.export( model, (image_input.float(), input_ids.long(), attention_mask), onnx_model_path, export_params=True, # 存储训练参数 opset_version=13, # ONNX 算子集版本 do_constant_folding=True, # 优化常量 input_names=['image_input', 'input_ids', 'attention_mask'], # 输入名称 output_names=['output'], # 输出名称 dynamic_axes={ 'image_input': {0: 'batch_size'}, 'input_ids': {0: 'batch_size'}, 'attention_mask': {0: 'batch_size'}, 'output': {0: 'batch_size'} } ) except RuntimeError as e: print(f"错误发生在: {e}") # 注意查看错误栈中提到的具体操作名称 print(f"Model has been converted to ONNX format and saved at {onnx_model_path}") 错误发生在: Expected tensor for argument #1 'indices' to have one of the following scalar types: Long, Int; but got torch.FloatTensor instead (while checking arguments for embedding) Model has been converted to ONNX format and saved at qwen2_5_vl.onnx
最新发布
07-02
评论 21
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值