编程字典keras.layers API方法

本文深入解析Keras中各种层的使用方法与特性,包括卷积层、循环层、归一化层等,为深度学习模型构建提供详实指导。
System.Reflection.TargetInvocationException HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=System.Private.CoreLib StackTrace: 在 System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr) 在 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) 在 Tensorflow.Keras.Utils.generic_utils.deserialize_keras_object(String class_name, JToken config) 在 Tensorflow.Keras.Saving.KerasObjectLoader._revive_layer_or_model_from_config(KerasMetaData metadata, Int32 node_id) 在 Tensorflow.Keras.Saving.KerasObjectLoader._revive_from_config(String identifier, KerasMetaData metadata, Int32 node_id) 在 Tensorflow.Keras.Saving.KerasObjectLoader._load_layer(Int32 node_id, String identifier, String metadata_json) 在 Tensorflow.Keras.Saving.KerasObjectLoader.load_layers(Boolean compile) 在 Tensorflow.Keras.Saving.SavedModel.KerasLoadModelUtils.load(String path, Boolean compile, LoadOptions options) 在 Tensorflow.Keras.Saving.SavedModel.KerasLoadModelUtils.load_model(String filepath, IDictionary`2 custom_objects, Boolean compile, LoadOptions options) 在 Tensorflow.Keras.Models.ModelsApi.load_model(String filepath, Boolean compile, LoadOptions options) 在 Keras.NET_Prediction_main_program.Program.Main() 在 D:\编程软件系列\VS2022社区版\文件\Keras.NET Prediction main program\Program.cs 中: 第 24 行 此异常最初是在此调用堆栈中引发的: [外部代码] 内部异常 1: JsonSerializationException: Could not create an instance of type Tensorflow.Keras.IRegularizer. Type is an interface or abstract class and cannot be instantiated. Path 'kernel_regularizer'.
最新发布
11-25
using System; using System.IO; using System.Reflection.Emit; using Tensorflow; //引入TensorFlow.NET核心命名空间(如 tf 对象)。 using Tensorflow.Keras; using Tensorflow.Keras.ArgsDefinition; using Tensorflow.Keras.Engine; using Tensorflow.Keras.Layers; using Tensorflow.NumPy; using static Tensorflow.Binding; using static Tensorflow.KerasApi; #region 安装包版本号 //Numsharp 0.30.0 //TensorFlow.NET 0.150.0 //TensorFlow.Keras 0.15.0 //SciSharp.TensorFlow.Redist 2.16.0 // CPU专用,CPU安装包和GPU安装包二选一。 //SciSharp.TensorFlow.Redist-Windowa-GPU 2.10.0 // GPU专用,CPU安装包和GPU安装包二选一。 //K80GPU + 463.15驱动 + cuda11.2.0 + cudnn 8.1.0.77 GPU训练专用 #endregion namespace Functional_DNNmodel { 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); // 批量大小,测试集不打乱 #endregion // 步骤2:使用Functional API方式构建模型 // 1. 配置 Dense 层参数(根据对象浏览器中 DenseArgs 的属性) var denseArgs1 = new DenseArgs { Units = 16, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.06f) // 自定义正则化类 }; var denseArgs2 = new DenseArgs { Units = 16, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.1f) // 自定义正则化类 }; var denseArgs3 = new DenseArgs { Units = 16, // 神经元数量 Activation = tf.keras.activations.Relu, // Relu激活函数 KernelRegularizer = new CustomL2Regularizer(0.1f) // 自定义正则化类 }; var denseArgs4 = new DenseArgs { Units = 16, // 神经元数量 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); // 步骤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 方法打印模型的结构摘要,包括每一层的名称、输出形状和参数数量等信息,方便了解模型的基本情况。 // 编译最终模型 model.compile(loss: keras.losses.SparseCategoricalCrossentropy(), optimizer: keras.optimizers.AdamW(learning_rate: 0.01f), metrics: ["accuracy"]); // 训练最终模型 model.fit(trainDataset, epochs: 500, validation_data: valDataset, verbose: 1, max_queue_size: 10, workers: 16, use_multiprocessing: true); Console.WriteLine(); // 换行 Console.Out.Flush(); // 强制刷新输出缓冲区,避免输出数据重叠 // 评估最终模型 var testResult = model.evaluate(test_X_normalized, test_Y, verbose: 1); // 从字典中提取测试集损失和准确率(Tensor 类型需通过 .numpy() 转换为数值) double testLoss = (double)testResult["loss"]; // 通过键名 "loss" 获取损失值 double testAcc = (double)testResult["accuracy"]; // 通过键名 "accuracy" 获取准确率 //保存最终模型(包括架构、权重、优化器状态) model.save(filepath: "D:\\SavedModel\\SuperbignumberBB.model.tf", // 模型保存路径(全英文且不能有空格,否则不能载入) overwrite: true, // true,自动覆盖原有文件 include_optimizer: true, // true,将优化器信息保存一起 save_format: "tf"); // 保存文件类型,"h5"模型为旧版本模型,目前不采用。 // 获取模型全部可训练参数 var weights = model.TrainableVariables; // 输出最优参数、权重、偏置信息 print($"测试集损失test_Loss: {testLoss:F4}, 测试集准确率test_Acc: {testAcc:P2}"); // 打印测试集指标 print($"权重: {weights[0].numpy()}"); print($"偏置: {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) 在 Functional_DNNmodel.Program.Main() 在 C:\Users\Administrator\Desktop\Functional DNNmodel\Program.cs 中: 第 149 行
11-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值