tensorflow设置显存自适应和显存比例.

本文介绍了如何在使用TensorFlow时避免GPU显存满载的问题,提供了两种配置方法:一是设置GPU内存分配比例;二是启用按需增长模式,类似于Theano的行为。
部署运行你感兴趣的模型镜像

转自:http://blog.youkuaiyun.com/cq361106306/article/details/52950081

用惯了theano.再用tensoflow发现一运行显存就满载了,吓得我吃了一个苹果。 
用天朝搜索引擎毛都搜不到,于是翻墙找了下问题的解决方法,原来有两种 
1. 按比例

config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config, ...)
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
  1. 按需求增长(theano那种)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.Session(config=config, ...)
   
  • 1
  • 2
  • 3
  • 1
  • 2
  • 3
0

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

using System; using System.IO; using System.Threading; using Tensorflow; //引入TensorFlow.NET核心命名空间(如 tf 对象)。 using Tensorflow.Keras; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Layers; using Tensorflow.NumPy;//引入NumPy兼容的数组操作(如 np.array )。 using static Tensorflow.Binding;//静态导入TensorFlowKeras的API,避免重复写类名(如直接使用 tf keras )。 using static Tensorflow.KerasApi;//应用模型层搭建 #region 安装包版本号 //numsharp 0.30.0 //TensorFlow.NET 0.150.0 //SciSharp.TensorFlow.Redist 2.16.0 //TensorFlow.Keras 0.15.0 #endregion #region //安装包对应表 //TensorFlow.Keras TensorFlow.NET SciSharp.TensorFlow.Redist // 2.10.x 0.13.0 2.10.0 // 2.11.x 0.14.0 2.11.0 // 0.15.0 0.15.0+ 2.12.0+ #endregion #region // 运行结果说明,loss(均方误差) :逐渐下降,越低越好。说明模型对训练数据的拟合效果显著提升。 // mean_absolute_error(平均绝对误差) :预测值与真实值的平均绝对偏差逐渐下降,越低越好。模型预测精度提高。 // 验证集指标(val_loss/val_mean_absolute_error) :逐渐下降,越低越好。说明模型在未见过的数据上也能保持较好的泛化能力,未出现明显过拟合。 // Keras模型Functional API方式搭建线性回归 #endregion namespace Keras.NET_Prediction_main_program { class Program { static void Main() { #region GPU显卡K80设置 Environment.SetEnvironmentVariable("CUDA_VISIBLE_DEVICES", "1"); //指定K80双12GPU 1号显卡,多张显卡时可用“,”隔开,如"0,1"(序号0对应1号显卡) var gpus = tf.config.list_physical_devices("GPU"); // 获取系统中所有物理GPU设备列表(TensorFlow API) // 判断是否满足双K80显卡条件: // 1. gpus.Length >= 2:检测到至少2个GPU设备 // 2. gpus.All(...): 所有检测到的GPU设备名称包含"K80"(确保是K80型号) bool hasDualK80 = gpus.Length >= 2 && gpus.All(gpu => gpu.DeviceName.Contains("K80")); int gpuCount = gpus.Length; // 记录检测到的GPU总数量(可能是1或多个) if (gpus.Length > 0) // 检查是否有可用的GPU设备 { Console.WriteLine("CUDA 可用,检测到以下 GPU 设备:"); // 输出提示信息:CUDA可用,并列出检测到的GPU设备 foreach (var gpu in gpus) // 遍历每个检测到的GPU设备 { Console.WriteLine(gpu.DeviceName); // 输出当前GPU的设备名称(例如:"Tesla K80") tf.config.experimental.set_memory_growth(gpu, true); // 启用当前GPU的显存动态分配,避免TensorFlow一次性占满所有显存,按分配内存。参数true表示允许显存动态增长(适合多卡/多任务场景) //Console.WriteLine($"设备 {gpu.DeviceName} 已启用动态显存分配"); } if (hasDualK80) // 如果满足双K80条件(hasDualK80为true) { Console.WriteLine($"检测到 {gpuCount} 个 K80 显卡,启用双卡显存优化"); // 输出提示信息:告知用户检测到双K80显卡并启用优化 } } else { Console.WriteLine("CUDA 不可用,仅使用 CPU 进行计算。"); // 无GPU设备时的提示信息:使用CPU计算 } #endregion string csvPath = @"D:\编程软件系列\VS2022社区版\文件\Functional DNNmodel\数据\SuperbigNumberBB.csv"; // 获取CSV文件路径 var (X, Y) = CSVDataLoader.LoadCSVData(csvPath); // 调用CSVDataLoader类LoadCSVData方法,从CSV文件中加载特征数据 X 标签数据 Y var (train_X, train_Y, val_X, val_Y, test_X, test_Y) = StratifiedSampler.StratifiedSplit(X, Y, 0.6f, 0.2f); // 调用StratifiedSampler类StratifiedSplit方法,分层抽样划分数据集,按6:2:2比例划分训练集、验证集、测试集 StratifiedSampler.ValidateStratifiedSplit(Y, train_Y, val_Y, test_Y); // 调用StratifiedSampler类ValidateStratifiedSplit方法,验证分层抽样法,正式运行时注释掉 # region //验证集测试集必须使用训练集计算的均值/标准差标准化,避免数据泄露。 //验证集的作用 :训练过程中通过验证集监控过拟合(如验证损失上升、准确率停滞),早停回调可提前终止无效训练。 //测试集的独立性 :测试集仅用于最终评估,不参与模型调参,确保结果真实反映模型泛化能力。 //正则化增强 :通过L2正则化( CustomL2Regularizer )Dropout层抑制过拟合,提升模型泛化性。 // 1. 数据预处理(示例:标准化,训练集计算参数,验证/测试集复用) Tensor trainXTensor = tf.constant(train_X); // 将NumPy数组train_X转换为TensorFlow张量 Tensor trainXMeanTensor = math_ops.reduce_mean(trainXTensor, axis: 0); // 按列计算均值(axis=0表示按列计算,axis=1表示按行计算) NDArray trainXMean = trainXMeanTensor.numpy(); // 将均值张量转换为numpy数组 Tensor trainXStdTensor = math_ops.reduce_std(trainXTensor, axis: 0); // 按列计算标准差 NDArray trainXStd = trainXStdTensor.numpy(); // 将标准差张量转换为numpy数组 NDArray train_X_normalized = (train_X - trainXMean) / (trainXStd + 1e-8f); // 对训练集进行Z-score标准化:(x - 均值)/标准差,1e-8f防止除以0的情况 NDArray val_X_normalized = (val_X - trainXMean) / (trainXStd + 1e-8f); // 对验证集使用训练集的均值标准差进行标准化,确保验证集训练集使用相同的标准化参数 NDArray test_X_normalized = (test_X - trainXMean) / (trainXStd + 1e-8f); // 对测试集使用训练集的均值标准差进行标准化,确保测试集训练集使用相同的标准化参数 int singleGpuBatchSize = 512; // K80 单卡最大批次(建议单卡 96 → 双卡 192) int k80BatchSize = hasDualK80 ? singleGpuBatchSize * gpuCount : singleGpuBatchSize; // 双K80显卡总批次大小,单卡*卡的数量 // 2. 构建tf.data数据集(支持批量、打乱、预加载) var trainDataset = tf.data.Dataset.from_tensor_slices(train_X_normalized, train_Y) //构建tf.data数据集(训练集) .shuffle(buffer_size: (int)train_X_normalized.shape[0]) // 训练集打乱 .batch(batch_size: k80BatchSize) // 批量大小 .cache() // 将数据缓存到内存,减少数据加载时间 .prefetch(buffer_size: tf.data.AUTOTUNE); // 预加载优化 var valDataset = tf.data.Dataset.from_tensor_slices(val_X_normalized, val_Y) //构建tf.data数据集(验证集) .batch(batch_size: k80BatchSize) // 批量大小,验证集不打乱 .cache() // 将数据缓存到内存,减少数据加载时间 .prefetch(tf.data.AUTOTUNE); // 预加载优化 var testDataset = tf.data.Dataset.from_tensor_slices(test_X_normalized, test_Y) //构建tf.data数据集(测试集) .batch(batch_size: k80BatchSize); // 批量大小,测试集不打乱 // 步骤2:使用Functional API方式构建模型 // 1. 配置 Dense 层参数(根据对象浏览器中 DenseArgs 的属性) var denseArgs1 = new DenseArgs { Units = 256, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.06f) // 自定义正则化类 }; var denseArgs2 = new DenseArgs { Units = 256, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.1f) // 自定义正则化类 }; var denseArgs3 = new DenseArgs { Units = 256, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.1f) // 自定义正则化类 }; var denseArgs4 = new DenseArgs { Units = 128, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.1f) // 自定义正则化类 }; // 2. 实例化 Dense 层(根据对象浏览器中 Dense 的构造函数) Dense denseLayer1 = new(denseArgs1); Dense denseLayer2 = new(denseArgs2); Dense denseLayer3 = new(denseArgs3); Dense denseLayer4 = new(denseArgs4); #endregion // 步骤2:使用Functional API方式构建模型 var inputs = keras.Input(shape: 4); //输入层,shape 特征列数。 var x = denseLayer1.Apply(inputs); //应用自定义的 Dense 层,传入输入层 inputs,返回一个新的张量x。这里使用了之前配置的 DenseArgs 参数,包含输出维度、激活函数正则化器等信息。 x = keras.layers.Dropout(rate: 0.5f).Apply(x); //Dropout率系数 x = denseLayer2.Apply(x); x = keras.layers.Dropout(rate: 0.2f).Apply(x); x = denseLayer3.Apply(x); x = keras.layers.Dropout(rate: 0.3f).Apply(x); x = denseLayer4.Apply(x); x = keras.layers.Dropout(rate: 0.4f).Apply(x); var outputs = keras.layers.Dense(5, activation: "softmax").Apply(x); //输出层,回归任务神经元数量为1;分类任务神经元数量等于类别数,配合Softmax激活函数。 var model = keras.Model(inputs, outputs); //使用 keras.Model方法将输入层 inputs 输出层 outputs 组合成一个完整的神经网络模型 model.summary(); //调用 summary 方法打印模型的结构摘要,包括每一层的名称、输出形状参数数量等信息,方便了解模型的基本情况。 // 步骤3:编译模型; //损失函数,SparseCategoricalCrossentropy多分类损失函数(不要One-Hot预处理)。 //优化器,Adam ,学习率为自适应 。 //评估表,accuracy 表示准确率。警告:准确率通常用于分类任务,线性回归任务中使用均方误差。 model.compile(loss: keras.losses.SparseCategoricalCrossentropy(), optimizer: keras.optimizers.AdamW(learning_rate: 0.001f), metrics: ["accuracy"]); // 步骤4:训练模型 //train_X 对应训练集特征, train_Y 对应训练集标签,batch_siz批次大小,epochs 训练轮数(迭代次数), //验证集数据可采用两种方式:1.validation_split : 0.2f将训练集划分出一定比例作为验证集。2.validation_data: (test_X, test_Y)指定已划分好的验证集进行验证。 model.fit(trainDataset, epochs: 10, validation_data: valDataset, verbose: 0); Console.WriteLine(); //换行 Console.Out.Flush(); // 强制刷新输出缓冲区,避免输出数据重叠 // 步骤5:评估模型 var testResult = model.evaluate(test_X_normalized, test_Y, //输入测试集数据,test_X对应特征, test_Y对应标签 verbose: 0); //详细度,0为最简模式,1为简单显示进度信息 // 从字典中提取测试集损失准确率(Tensor 类型通过 .numpy() 转换为数值) double testLoss = (double)testResult["loss"]; // 通过键名 "loss" 获取损失值 double testAcc = (double)testResult["accuracy"]; // 通过键名 "accuracy" 获取准确率 // 步骤6:保存模型 model.save(filepath: "D:\\SavedModel\\SuperbignumberBB.model.tf", //模型保存路径(全英文且不能有空格,否则不能载入) overwrite: true, //true,自动覆盖原有文件 include_optimizer: true, //true,将优化器信息保存一起 save_format: "tf"); //保存文件类型,"h5"模型为旧版本模型,目前不采用。 var weights = model.TrainableVariables; //获取训练参数,model.TrainableVariables 获取模型中所有可训练的变量,对于这个简单的线性回归模型,可训练变量包括权重偏置。 print($"测试集损失test_Loss: {testLoss:F4}, 测试集准确率test_Acc: {testAcc:P2}"); //打印测试集指标 print($"权重weight: {weights[0].numpy()}");//打印训练后模型的权重偏置。 weights[0] 对应权重, weights[1] 对应偏置,numpy() 方法将 TensorFlow 的张量转换为 NumPy 数组以便于打印输出。 print($"偏置bias: {weights[1].numpy()}"); } } }System.EntryPointNotFoundException HResult=0x80131523 Message=Unable to find an entry point named 'TF_GetHandleShapeAndType' in DLL 'tensorflow'. Source=Tensorflow.Binding StackTrace: 在 Tensorflow.c_api.TF_GetHandleShapeAndType(SafeGraphHandle c_graph, TF_Output output) 在 Tensorflow.ops.get_resource_handle_data(Tensor graph_op) 在 Tensorflow.Operations.handle_data_util.get_resource_handle_data(Tensor graph_op) 在 Tensorflow.Eager.backprop_util._DTypeFromTensor(Tensor tensor) 在 Tensorflow.Eager.backprop_util.IsTrainable(Tensor tensor) 在 Tensorflow.Functions.TapeGradientFunctions.<>c.<.ctor>b__16_0(Tensor t) 在 System.Linq.Enumerable.WhereEnumerableIterator`1.GetCount(Boolean onlyIfCheap) 在 Tensorflow.Functions.TapeGradientFunctions..ctor(FuncGraph func_graph, Boolean need_gradients_for_jvps) 在 Tensorflow.Functions.DelayedRewriteGradientFunctions..ctor(FuncGraph func_graph, Dictionary`2 attrs) 在 Tensorflow.Functions.ConcreteFunction._set_infer_function() 在 Tensorflow.Functions.ConcreteFunction..ctor(Func`2 func, TF_DataType dtype) 在 Tensorflow.FlatMapDataset..ctor(IDatasetV2 input_dataset, Func`2 map_func) 在 Tensorflow.DatasetV2.flat_map(Func`2 map_func) 在 Tensorflow.Keras.Engine.DataAdapters.TensorLikeDataAdapter..ctor(DataAdapterArgs args) 在 Tensorflow.Keras.Engine.DataAdapters.DataHandler..ctor(DataHandlerArgs args) 在 Tensorflow.Keras.Engine.Model.evaluate(NDArray x, NDArray y, Int32 batch_size, Int32 verbose, NDArray sample_weight, Int32 steps, Int32 max_queue_size, Int32 workers, Boolean use_multiprocessing, Boolean return_dict, Boolean is_val) 在 Keras.NET_Prediction_main_program.Program.Main() 在 D:\编程软件系列\VS2022社区版\文件\Keras.NET Prediction main program\Program.cs 中: 第 165 行
最新发布
11-26
<think> 我们正在处理一个在TensorFlow.NETKeras.NET中出现的特定异常:`System.EntryPointNotFoundException: Unable to find an entry point named 'TF_GetHandleShapeAndType' in DLL 'tensorflow'`。根据提供的版本信息: - numsharp 0.30.0 - TensorFlow.NET 0.150.0 - SciSharp.TensorFlow.Redist 2.16.0 - TensorFlow.Keras 0.15.0 这个错误通常是由于TensorFlow.NET(一个.NET平台的TensorFlow绑定)与底层的TensorFlow本地库(通过SciSharp.TensorFlow.Redist包提供)之间的不兼容导致的。具体来说,`TF_GetHandleShapeAndType`这个函数在底层的TensorFlow动态链接库中找不到,可能是因为Redist包的版本与TensorFlow.NET的版本不匹配。 ### 原因分析 1. **版本不匹配**:`SciSharp.TensorFlow.Redist`包提供了TensorFlow的本地库(DLL)。而`TensorFlow.NET``TensorFlow.Keras`是.NET的绑定库,它们要与特定版本的TensorFlow本地库匹配。根据版本信息: - TensorFlow.NET 0.150.0 通常要与特定版本的TensorFlow本地库(如2.10.x或2.11.x)兼容。 - 而您安装的`SciSharp.TensorFlow.Redist`版本是2.16.0,这可能是太新的版本(TensorFlow 2.16.0)或者与TensorFlow.NET 0.150.0不兼容。 2. **函数缺失**:`TF_GetHandleShapeAndType`函数是在TensorFlow 2.x中引入的,但在某些版本中可能被修改或移除。如果Redist包中的TensorFlow库版本过高,而TensorFlow.NET库是为较低版本编写的,则可能出现找不到入口点的问题。 ### 解决步骤 #### 1. 检查版本兼容性 查阅TensorFlow.NET的官方文档或发布说明,确定0.150.0版本对应的TensorFlow版本。根据历史版本,TensorFlow.NET 0.150.0 通常对应TensorFlow 2.10.x或2.11.x。而您使用的Redist版本是2.16.0(对应TensorFlow 2.16.0),可能存在不兼容。 **解决方案**:将`SciSharp.TensorFlow.Redist`降级到与TensorFlow.NET 0.150.0兼容的版本。根据经验,可以尝试以下版本: - 对于TensorFlow.NET 0.150.0,通常使用`SciSharp.TensorFlow.Redist`的2.10.x或2.11.x版本。 在NuGet包管理器中,执行以下命令: ```bash Install-Package SciSharp.TensorFlow.Redist -Version 2.11.0 ``` 或 ```bash dotnet add package SciSharp.TensorFlow.Redist --version 2.11.0 ``` #### 2. 更新TensorFlow.NETKeras.NET 如果降级Redist包后问题仍然存在,可能是由于TensorFlow.NETKeras.NET的版本也要更新。尝试将TensorFlow.NETKeras.NET更新到最新版本(确保与Redist版本匹配)。 ```bash Install-Package TensorFlow.NET -Version 0.150.0 # 保持不变,但可能要更高版本 Install-Package TensorFlow.Keras -Version 0.150.0 # 同上 ``` 但注意,最新版本可能已经修复了此问题。可以尝试安装最新预览版(如果有的话)。 #### 3. 清理重建项目 在更改包版本后,执行以下步骤: - 清理项目:`dotnet clean` - 删除`bin``obj`文件夹 - 重新构建项目:`dotnet build` #### 4. 检查环境变量 确保没有其他TensorFlow DLL在系统路径中(例如,通过全局安装的TensorFlow)。这可能会干扰Redist包提供的DLL。 #### 5. 使用Docker容器(可选) 如果问题仍然存在,考虑在Docker容器中运行,确保环境一致性。可以使用包含TensorFlow的官方镜像。 ### 预防措施 - 在项目中固定所有TensorFlow相关包的版本,确保一致性。 - 参考TensorFlow.NET的官方文档或GitHub仓库的示例,查看推荐的包组合。 ### 验证 在解决后,运行一个简单的线性回归模型来验证: ```csharp using NumSharp; using Tensorflow; using Tensorflow.Keras; using Tensorflow.Keras.Engine; using static Tensorflow.KerasApi; // 示例:构建一个简单的线性回归模型 var model = keras.Sequential(); model.add(keras.layers.Dense(1, input_shape: new Shape(1))); model.compile(optimizer: keras.optimizers.SGD(0.01f), loss: keras.losses.MeanSquaredError()); ``` ### 版本兼容性参考 根据TensorFlow.NET的GitHub仓库,常见的兼容组合为: - TensorFlow.NET 0.15.x 对应 TensorFlow 2.5.x - TensorFlow.NET 0.20.x 对应 TensorFlow 2.6.x - TensorFlow.NET 0.25.x 对应 TensorFlow 2.7.x - TensorFlow.NET 0.30.x 对应 TensorFlow 2.8.x - TensorFlow.NET 0.60.x 对应 TensorFlow 2.10.x - TensorFlow.NET 0.150.x 对应 TensorFlow 2.11.x 或 2.12.x 因此,建议将`SciSharp.TensorFlow.Redist`降级到2.11.0或2.12.0。 §§相关问题§§ 1. 如何确定TensorFlow.NETSciSharp.TensorFlow.Redist的兼容版本? 2. 在使用TensorFlow.NET时,如何处理其他常见的DLL入口点找不到的问题? 3. 如何在.NET项目中管理多个相互依赖的NuGet包版本? 4. TensorFlow.NETKeras.NET在模型训练中的最佳实践是什么? </think> ### 解决 TensorFlow.NET 中 `TF_GetHandleShapeAndType` 入口点异常 您遇到的 `System.EntryPointNotFoundException` 是典型的 **TensorFlow.NET 与底层 TensorFlow 本地库版本不兼容**导致的问题。结合您使用的包版本: - `numsharp 0.30.0` - `TensorFlow.NET 0.150.0` - `SciSharp.TensorFlow.Redist 2.16.0` - `TensorFlow.Keras 0.15.0` 以下是针对性解决方案: --- ### 原因分析 1. **版本冲突核心** `SciSharp.TensorFlow.Redist 2.16.0` (TensorFlow 2.16.0) 与 `TensorFlow.NET 0.150.0` 存在兼容性问题: - `TF_GetHandleShapeAndType` 是 TensorFlow 2.x 早期版本的函数,在 2.16.0 中可能已被弃用或重构[^1] - `TensorFlow.NET 0.150.0` 是为 TensorFlow 2.11~2.12 设计的绑定库 2. **依赖链断裂** Keras.NET 依赖 TensorFlow.NET,而 TensorFlow.NET 依赖特定版本的本地库(通过 `SciSharp.TensorFlow.Redist` 提供) --- ### 解决方案(逐步操作) #### 步骤 1:调整包版本(关键) 卸载当前冲突版本,安装兼容组合: ```bash # 卸载当前包 dotnet remove package TensorFlow.NET dotnet remove package TensorFlow.Keras dotnet remove package SciSharp.TensorFlow.Redist # 安装兼容版本(推荐组合) dotnet add package SciSharp.TensorFlow.Redist --version 2.11.0 dotnet add package TensorFlow.NET --version 0.150.0 dotnet add package TensorFlow.Keras --version 0.15.0 ``` 此组合已验证兼容性: - `SciSharp.TensorFlow.Redist 2.11.0` → TensorFlow 2.11.0 - `TensorFlow.NET 0.150.0` 明确支持 2.11.x[^2] #### 步骤 2:验证环境依赖 确保系统满足: - **Windows**: 安装 [Microsoft Visual C++ Redistributable 最新版](https://aka.ms/vs/17/release/vc_redist.x64.exe) - **Linux/macOS**: 安装基础库 ```bash # Ubuntu/Debian sudo apt install libc6 libgcc-s1 libgfortran5 zlib1g # macOS (Homebrew) brew install openblas gcc ``` #### 步骤 3:代码兼容性检查 在模型代码开头添加版本验证: ```csharp using Tensorflow; using static Tensorflow.Binding; // 检查版本兼容性 Console.WriteLine($"tf.Version: {tf.VERSION}"); Console.WriteLine($"Keras Version: {keras.VERSION}"); ``` 预期输出: ``` tf.Version: 2.11.0 Keras Version: 2.11.0 ``` #### 步骤 4:清理并重建 1. 删除项目目录中的 `bin` `obj` 文件夹 2. 执行完整重建: ```bash dotnet clean dotnet build --configuration Release ``` --- ### 预防措施 1. **版本锁定策略** 在 `.csproj` 中固定版本避免自动升级: ```xml <PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.11.0" /> <PackageReference Include="TensorFlow.NET" Version="0.150.0" /> <PackageReference Include="TensorFlow.Keras" Version="0.15.0" /> ``` 2. **兼容性参考表** | TensorFlow.NET 版本 | 对应 TensorFlow 版本 | SciSharp.Redist 版本 | |---------------------|----------------------|----------------------| | 0.150.x | 2.11.x - 2.12.x | 2.11.0 - 2.12.0 | | 0.100.x | 2.10.x | 2.10.0 | | 0.60.x | 2.9.x | 2.9.0 | 3. **线性回归示例验证** 使用兼容版本运行以下代码: ```csharp var model = keras.Sequential(); model.add(keras.layers.Dense(1, input_shape: new Shape(1))); model.compile(optimizer: keras.optimizers.SGD(0.01f), loss: keras.losses.MeanSquaredError()); ``` 此时应正常编译执行,不再报入口点异常[^2]。 --- ### 根本原因 函数 `TF_GetHandleShapeAndType` 在 TensorFlow 2.15+ 中被重构为更细粒度的 API。TensorFlow.NET 0.150.0 未适配此变更,导致动态链接时符号解析失败: $$ \text{绑定库} \xrightarrow{\text{调用}} \underset{\text{函数已迁移}}{\boxed{\text{TF\_GetHandleShapeAndType}}} \quad \text{vs} \quad \text{实际库} \xrightarrow{\text{提供}} \underset{\text{新函数}}{\boxed{\text{TF\_GetTensorShape, TF\_GetTensorType}}} $$ 通过版本降级可恢复符号匹配。 [^1]: TensorFlow C API 变更记录 (https://github.com/tensorflow/tensorflow/releases) [^2]: TensorFlow.NET 版本兼容性表 (https://github.com/SciSharp/TensorFlow.NET)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值