You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用ML进行NER预测时出现TorchSharp.torch.CUDA字段未找到错误

ML.NET NER训练报错:找不到TorchSharp.torch.CUDA字段

问题概述

在使用ML.NET的命名实体识别(NER)功能训练模型时,执行estimator.Fit(dataView)抛出错误:Field not found: 'TorchSharp.torch.CUDA'。我期望通过Torchsharp-cpu包使用CPU处理,但无法定位CUDA引用的来源,且该错误指向的是字段而非包引用问题。

环境信息

  • 运行架构:x64
  • 依赖NuGet包:
    • Microsoft.ML (3.0.0-preview.23511.1)
    • Microsoft.ML.TorchSharp (0.21.0-preview.23511.1)
    • Torchsharp-cpu (0.101.1)

已尝试无效操作

  • 更换多个NuGet包(包括libtorch包)
  • 切换x86/x64架构
  • 添加代码强制CPU运行:context.FallbackToCpu = true、context.GpuDeviceId = null

相关代码

using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.TorchSharp;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace NerTester
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private class TestSingleSentenceData
        {
            public string Sentence;
            public string[] Label;
        }

        private class Label
        {
            public string Key { get; set; }
        }

        private void startButton_Click(object sender, EventArgs e)
        {
            try
            {
                var context = new MLContext();
                context.FallbackToCpu = true;
                context.GpuDeviceId = null;

                var labels = context.Data.LoadFromEnumerable(
                new[] {
                    new Label { Key = "PERSON" },
                    new Label { Key = "CITY" },
                    new Label { Key = "COUNTRY"  }
                });

                var dataView = context.Data.LoadFromEnumerable(
                    new List<TestSingleSentenceData>(new TestSingleSentenceData[] {
                    new TestSingleSentenceData()
                    {
                        Sentence = "Alice and Bob live in the USA",
                        Label = new string[]{"PERSON", "0", "PERSON", "0", "0", "0", "COUNTRY"}
                    },
                    new TestSingleSentenceData()
                    {
                        Sentence = "Alice and Bob live in the USA",
                        Label = new string[]{"PERSON", "0", "PERSON", "0", "0", "0", "COUNTRY"}
                    },
                }));
                var chain = new EstimatorChain<ITransformer>();
                var estimator = chain.Append(context.Transforms.Conversion.MapValueToKey("Label", keyData: labels))
                   .Append(context.MulticlassClassification.Trainers.NameEntityRecognition(outputColumnName: "outputColumn"))
                   .Append(context.Transforms.Conversion.MapKeyToValue("outputColumn"));

                var transformer = estimator.Fit(dataView);
                transformer.Dispose();
                
                MessageBox.Show("Success!");
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error: {ex.Message}");
            }
        }
    }
}

解决建议

  1. 对齐TorchSharp版本
    Microsoft.ML.TorchSharp预览版依赖特定版本的TorchSharp,当前使用的Torchsharp-cpu (0.101.1)可能和它不兼容。查看Microsoft.ML.TorchSharp的NuGet依赖信息,安装对应版本的Torchsharp-cpu包,不要手动指定版本。

  2. 强制TorchSharp使用CPU
    在初始化MLContext前,添加TorchSharp的CPU强制配置:

    using TorchSharp; // 需要添加这个命名空间引用
    // ...
    torch.backends.cudnn.enabled = false;
    torch.device(torch.CPU);
    
  3. 清理NuGet缓存重装
    删除项目目录下的packages文件夹,执行NuGet缓存清理命令:

    dotnet nuget locals all --clear
    

    之后重新安装所有依赖包,避免包损坏或版本冲突。

  4. 切换到稳定版包
    尝试放弃预览版,使用Microsoft.ML和Microsoft.ML.TorchSharp的稳定版本,预览版可能存在未修复的兼容性问题。


内容的提问来源于stack exchange,提问作者lahbton

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 13:54:54