当前位置:   article > 正文

基于昇思MindSpore的同元软控AI系列工具箱正式发布,大幅度降低产品研发成本_mindspore绘制loss曲线

mindspore绘制loss曲线

随着智能时代的到来,同元软控与华为携手合作,以昇思MindSpore为框架底座,打造了MWORKS AI工具箱,并于2023年1月8号正式对外发布。基于昇思MindSpore的MWORKS AI工具箱将原有的仿真建模和AI模型有机融合,可以大幅度减少产品的研发成本。未来该工具箱可以应用到航空、航天、船舶、能源等复杂装备系统的工程建模,实现更高、更智能的数字孪生系统。

01  MWORKS AI工具箱基础架构

MWORKS AI工具箱主要包含深度学习与机器学习工具箱,深度学习工具箱支持前馈神经网络、卷积神经网络、循环神经网络等深度学习网络的设计、模型构建、训练以及深度学习应用。机器学习工具箱支持聚类分析、主成分分析、降维、分类、回归等数据的描述、分析及数据建模。工具架构如图示,主要分为底层、框架核心及前端。

02  模块及功能

本节主要介绍深度学习工具箱的主要模块及功能。深度学习工具箱5大模块,110个基础函数,分别是图像深度学习41个,预训练网络19个,网络训练组件2个,时序、序列和文本深度学习33个,函数逼近与聚类15个。

函数

说明(中文)

trainingOptions

训练深度学习神经网络的选项

trainNetwork

训练深度学习神经网络

squeezenet

SqueezeNet卷积神经网络

googlenet

GoogLeNet卷积神经网络

inceptionv3

Inception-v3卷积神经网络

convolution1dLayer

一维卷积层

mobilenetv2

MobileNet-v2卷积神经网络

resnet18

ResNet-18卷积神经网络

resnet50

ResNet-50卷积神经网络

resnet101

ResNet-101卷积神经网络

xception

Xception卷积神经网络

transposedConv1dLayer

转置一维卷积层

batchNormalization1dLayer

一维批处理规范化层

batchNormalization3dLayer

三维批处理规范化层

shufflenet

预训练的ShuffleNet卷积神经网络

instanceNormalization1dLayer

一维实例规范化层

instanceNormalization3dLayer

三维实例规范化层

averagePooling1dLayer

一维平均池化层

alexnet

AlexNet卷积神经网络

vgg16

VGG-16卷积神经网络

vgg19

VGG-19卷积神经网络

convolution2dLayer

二维卷积层

convolution3dLayer

3D卷积层

groupedConvolution2dLayer

二维分组卷积层

transposedConv2dLayer

转置二维卷积层

transposedConv3dLayer

转置的3D卷积层

fullyConnectedLayer

全连接层

reluLayer

整流线性单元(ReLU)层

leakyReluLayer

泄漏整流线性单元(ReLU)层

clippedReluLayer

修剪整流线性单元(ReLU)层

eluLayer

指数线性单位(ELU)层

tanhLayer

双曲正切(tanh)层

swishLayer

Swish层

batchNormalizationLayer

批处理规范化层

groupNormalizationLayer

组归一化层

instanceNormalizationLayer

实例规范化层

layerNormalizationLayer

层归一化层

crossChannelNormalizationLayer

通道局部响应标准化层

dropoutLayer

丢弃层

crop2dLayer

二维裁剪图层

crop3dLayer

3-D裁剪层

03  应用案例

我们基于同元-昇思MindSpore AI工具箱,在图像、文字和声音等领域展开了研究。

3.1 手写数字识别与倾斜角度预测

手写数字识别(MNIST)数据集是一个大型的手写体数字数据库,通常用于训练各种图像处理系统,也被广泛用于机器学习领域的训练和测试。手写数字识别数据集包含手写数字的合成图像、每张图像对应的数字以及倾斜的角度。该数据集共有60000张训练图片和10000张测试图片,每张图片上数字为0-9中的一种,总共分为9类,其中每张图片的大小为28*28(像素)。

图1:手写数字识别数据集

我们主要利用同元-昇思MindSpore AI工具箱提供的卷积算子(Conv2d)、最大池化算子(MaxPool)等构建一个卷积神经网络,并将图像及其对应的数字作为输入和输出,训练得到一个可识别手写数字的网络(基于同元-昇思MindSpore AI工具箱实现的详细代码见附录(1))。

图2:手写数字识别训练过程

在测试集中随机抽取6张图片展示,并使用训练好的模型进行识别,将结果打印在命令行窗口:

图3:手写数字识别结果

该网络模型的卷积模块可以提取图像的特征,去掉Softmax层并在最后添加新的全连接层,得到一个新的网络模型,随后用数字倾斜角度作为预测目标来训练该网络。

图4:手写数字倾斜角度预测训练过程

随机挑选测试集中的图像进行预测,橙色线条为预测角度,可见与实际数字的倾斜角度基本相符。

图5:手写数字倾斜角度预测结

3.2 日语元音字符分类

本示例说明如何使用长短期记忆 (LSTM) 网络对序列数据进行分类(基于同元-昇思MindSpore AI工具箱实现的详细代码见附录(2))。

要训练深度神经网络以对序列数据进行分类,可以使用 LSTM 网络。LSTM 网络允许您将序列数据输入网络,并根据序列数据的各个时间步进行预测。

本示例使用日语元音数据集。此示例训练一个 LSTM 网络,旨在根据表示连续说出的两个日语元音的时间序列数据来识别说话者。训练数据包含九个说话者的时间序列数据。每个序列有12个特征,且长度不同。该数据集包含270个训练观测值和370个测试观测值。

加载日语元音训练数据。可视化第一个时间序列。每条线对应一个特征。

图6:数据集展示

基于同元-昇思MindSpore AI工具箱提供的LSTM算子、全连接算子、Softmax激活函数等, 我们定义了LSTM网络架构,然后导入日语元音数据集进行训练,并打印Loss曲线。

  1. # 构建网络
  2. layers = SequentialCell([
  3. bilstmLayer(12, 100; NumLayers=3, Batch_First=true),
  4. flattenLayer(),
  5. fullyConnectedLayer(truncation_number * 200, 9),
  6. softmaxLayer(),
  7. ])
  8. options = trainingOptions(
  9. "CrossEntropyLoss", "Adam", "Accuracy", 27, 200, 0.001; Shuffle=true, Plots=true
  10. )
  11. net = trainNetwork(train_data, train_label, layers, options)
  12. YPred = TyDeepLearning.classify(net, test_data)
  13. accuracy = Accuracy(YPred, test_label)
  14. print(accuracy)

图7:Loss曲线

3.3 基于LSTM的人类活动分类任务

该案例所采用的数据集来源于佩戴在人身上的传感器的数据序列。每个序列有三个特征,分别对应三个不同坐标方向下的加速度数据。该数据集包括七位志愿者的加速度数据,其中六组的数据作为训练集,另外一组作为测试集。

某序列的其中一维坐标数据:

图8:训练序列

我们基于同元-昇思MindSpore AI工具箱中的网络层构建一个LSTM神经网络,训练后对测试数据进行测试。训练过程如下(基于同元-昇思MindSpore AI工具箱实现的详细代码见附录(3)):

图9:LSTM网络训练过程

训练完成后,利用已训练的网络对测试集进行测试。下图展示了测试数据集中的三类坐标数据:

图10:测试序列

对以上数据进行预测得到结果:

图11:测试结果

3.4 声纹识别

声纹识别是一个突出的研究领域,具有多种应用,包括取证和生物识别认证。许多声纹识别系统依赖于预先计算的特征,如i向量或MFCC,然后将其输入机器学习或深度学习网络进行分类。其他深度学习语音系统绕过特征提取阶段,将音频信号直接馈送到网络。在这样的端到端系统中,网络直接学习低级音频信号特性(基于同元-昇思MindSpore AI工具箱实现的详细代码见附录(4))

导入数据集,数据集来源中华语料库。可通过采样率调整数据集大小。

train_data, train_label, test_data, test_label = VPR_dataset( 1500)

类似地,基于同元-昇思MindSpore AI工具箱提供的卷积算子(Conv2d)、批标准化算子(BN)、最大池化算子(MaxPool)等定义了如下的网络结构,导入数据集进行训练。并绘制Loss曲线

  1. net = SequentialCell([
  2. convolution2dLayer(1, 80, (1, 251); PaddingMode="valid"),
  3. batchNormalization2dLayer(80),
  4. leakyReluLayer(0.2),
  5. maxPooling2dLayer((1, 3)),
  6. convolution2dLayer(80, 60, (1, 251); PaddingMode="valid"),
  7. batchNormalization2dLayer(60),
  8. leakyReluLayer(0.2),
  9. maxPooling2dLayer((1, 3)),
  10. convolution2dLayer(60, 60, (1, 5); PaddingMode="valid"),
  11. batchNormalization2dLayer(60),
  12. leakyReluLayer(0.2),
  13. maxPooling2dLayer((1, 3)),
  14. flattenLayer(),
  15. fullyConnectedLayer(60 * 2490, 2048),
  16. batchNormalization1dLayer(2048),
  17. leakyReluLayer(0.2),
  18. fullyConnectedLayer(2048, 1024),
  19. batchNormalization1dLayer(1024),
  20. leakyReluLayer(0.2),
  21. fullyConnectedLayer(1024, 256),
  22. batchNormalization1dLayer(256),
  23. leakyReluLayer(0.2),
  24. fullyConnectedLayer(256, 20),
  25. softmaxLayer()
  26. ])
  27. options = trainingOptions(
  28. "CrossEntropyLoss", "Adam", "Accuracy", 100, 150, 0.0005; Plots=true
  29. )
  30. net = trainNetwork(train_data, train_label, net, options)

图12:Loss曲线

3.5 发动机剩余使用寿命预测

本示例说明如何使用深度学习预测发动机的剩余使用寿命 (RUL) (基于同元-昇思MindSpore AI工具箱实现的详细代码见附录(5))。

本示例使用涡轮风扇发动机退化仿真数据集。该示例训练一个CNN 网络,旨在根据表示发动机中各种传感器的时间序列数据来预测发动机的剩余使用寿命(预测性维护,以周期为单位度量)。训练数据包含 100 台发动机的仿真时间序列数据。每个序列的长度各不相同,对应于完整的运行至故障 (RTF) 实例。测试数据包含 100 个不完整序列,每个序列的末尾为相应的剩余使用寿命值。该数据集包含 100 个训练观测值和 100 个测试观测值。

涡轮风扇发动机退化仿真数据集的每个时间序列表示一个发动机。每台发动机启动时的初始磨损程度和制造变差均未知。发动机在每个时间序列开始时运转正常,在到达序列中的某一时刻时出现故障。在训练集中,故障的规模不断增大,直到出现系统故障。

数据集包含26列数值。每一行是在一个运转周期中截取的数据快照,每一列代表一个不同的变量。这些列分别对应于以下数据:

- 第 1 列 - 单元编号

- 第 2 列 - 周期时间

- 第 3-5 列 - 操作设置

- 第 6-26 列 - 传感器测量值 1-21

将3-26列作为特征数据,绘制展示每个特征前100条数据。

图13:特征曲线

由图可知,特征"op_setting_3", Sensor1", "Sensor5", "Sensor10", "Sensor16", "Sensor18", "Sensor19"保持不变,可将其删除。将删除后的剩余17个特征数据进行标准化预处理。

基于同元-昇思MindSpore AI工具箱提供的卷积算子(Conv1d)、Relu激活函数等定义了如下的神经网络结构。

  1. # 网络构建
  2. layers = SequentialCell([
  3. convolution1dLayer(17, 32, 5),
  4. reluLayer(),
  5. convolution1dLayer(32, 64, 7),
  6. reluLayer(),
  7. convolution1dLayer(64, 128, 11),
  8. reluLayer(),
  9. convolution1dLayer(128, 256, 13),
  10. reluLayer(),
  11. convolution1dLayer(256, 512, 15),
  12. reluLayer(),
  13. flattenLayer(),
  14. fullyConnectedLayer(512 * sequence_length, 100),
  15. reluLayer(),
  16. dropoutLayer(0.5),
  17. fullyConnectedLayer(100, 1),
  18. ])
  19. options = trainingOptions("RMSELoss", "Adam", "MSE", 512, 200, 0.001; Plots=true)
  20. net = trainNetwork(XTrain, YTrain, layers, options)

绘制RMSE损失曲线。

图14:RMSE Loss曲线

将训练集和测试集导入训练好的神经网络中进行推理预测,将预测值与真实标签进行对比。

图15:预测值与真实标签对比

图16:测试集效果对比

04  总结与展望

同元软控与华为携手合作,打造了以高性能科学计算语言Julia为用户语言、以华为昇思MindSpore为底层的AI系列工具箱。如深度学习工具箱内含丰富的函数库、应用案例及完善的帮助文档。基于深度学习工具箱,用户可以采用Julia构建卷积神经网络、循环神经网络等各种类型深度学习网络模型,并进行训练计算,深度学习工具箱已在图像识别、语言识别、故障预测等方向开展了应用验证,取得了良好效果。

同元软控与华为的合作开辟了国产软件携手发展的示范应用,未来,我们还会在AI领域继续展开深入合作,全新推出强化学习工具箱,并推进AI系列工具箱在航空、航天、车辆、能源等复杂系统多领域工程建模中的应用,实现机理-数据融合建模,探索智能建模仿真技术,打造新一代智能建模仿真软件平台。我们也希望能够有更多的企业、科研院所参与进来,共同打造国产智能软硬件平台。

苏州同元软控信息技术有限公司:

苏州同元软控信息技术有限公司(简称“同元软控”)是专业从事新一代系统级设计与仿真工业软件产品研发、工程服务及系统工程解决方案的高科技企业。团队历经二十多年技术积累与公司十多年持续研发,研制了国际先进、完全自主的科学计算与系统建模仿真平台MWORKS。

代码附录

(1)手写数字识别与倾斜角度预测

  1. using TyDeepLearning
  2. using TyPlot
  3. using TyImages
  4. # 训练卷积神经网络用于图像分类
  5. XTrain, YTrain = DigitDatasetTrainData()
  6. p = randperm(5000)
  7. index = p[1:20]
  8. figure(1)
  9. for i in eachindex(range(1, 20))
  10. subplot(4, 5, i)
  11. imshow(XTrain[index[i], 1, :, :])
  12. end
  13. index1 = p[1:750]
  14. index2 = p[751:end]
  15. X_train = XTrain[index2, :, :, :]
  16. Y_train = YTrain[index2]
  17. X_Test = XTrain[index1, :, :, :]
  18. Y_Test = YTrain[index1]
  19. options = trainingOptions(
  20. "CrossEntropyLoss", "Momentum", "Accuracy", 128, 50, 0.001; Plots=true
  21. )
  22. layers = SequentialCell([
  23. convolution2dLayer(1, 20, 5),
  24. reluLayer(),
  25. maxPooling2dLayer(2; Stride=2),
  26. flattenLayer(),
  27. fullyConnectedLayer(20 * 14 * 14, 10),
  28. softmaxLayer(),
  29. ])
  30. net = trainNetwork(X_train, Y_train, layers, options)
  31. function preclasses(prob, classes)
  32. ypredclasses = []
  33. for i in eachindex(range(1, size(prob)[1]))
  34. maxindex = 0
  35. maxnum = 0
  36. for k in eachindex(classes)
  37. if prob[i, :][k] > maxnum
  38. maxnum = prob[i, :][k]
  39. maxindex = k
  40. end
  41. end
  42. ypredclasses = append!(ypredclasses, [unique(classes)[maxindex]])
  43. end
  44. return ypredclasses
  45. end
  46. YPred = TyDeepLearning.predict(net, X_Test)
  47. classes = [i - 1 for i in range(1, 10)]
  48. YPred1 = preclasses(YPred, classes)
  49. accuracy = Accuracy(YPred, Y_Test)
  50. figure(3)
  51. p2 = randperm(750)
  52. index = p2[1:9]
  53. for i in eachindex(range(1, 9))
  54. TyPlot.subplot(3, 3, i)
  55. TyImages.imshow(X_Test[index[i], 1, :, :])
  56. title1 = "Prediction Label"
  57. title2 = string(YPred1[index[i]])
  58. title(string(title1, ": ", title2))
  59. end
  60. # 手写数字识别角度预测
  61. using TyDeepLearning
  62. using TyImages
  63. using TyPlot
  64. XTrain, YTrain = DigitTrain4DArrayData()
  65. index = randperm(5000)[1:20]
  66. figure(1)
  67. for i in eachindex(range(1, 20))
  68. subplot(4, 5, i)
  69. imshow(XTrain[index[i], 1, :, :])
  70. end
  71. layers = SequentialCell([
  72. convolution2dLayer(1, 25, 12),
  73. reluLayer(),
  74. flattenLayer(),
  75. fullyConnectedLayer(25 * 28 * 28, 1),
  76. ])
  77. options = trainingOptions("RMSELoss", "Adam", "MSE", 10, 50, 0.0001; Plots=true)
  78. net = trainNetwork(XTrain, YTrain, layers, options)
  79. XTest, YTest = DigitTest4DArrayData()
  80. YPred = TyDeepLearning.predict(net, XTest)
  81. rmse = sqrt(mse(YTest, YPred))
  82. index = randperm(5000)[1:9]
  83. figure(2)
  84. for i in range(1, 9)
  85. subplot(3, 3, i)
  86. hold("on")
  87. imshow(XTest[index[i], 1, :, :])
  88. x = [7:21...]
  89. plot(x, tan((90 + YPred[index[i]]) / 180 * pi) * (x .- 14) .+ 14, "r")
  90. ax = gca()
  91. ax.set_ylim(28,0)
  92. ax.set_xlim(0, 28)
  93. hold("off")
  94. end

(2)日语元音字符分类

  1. using TyDeepLearning
  2. using TyPlot
  3. # 数据导入
  4. path1 = "data/JapaneseVowels/train.csv"
  5. path2 = "data/JapaneseVowels/trainlabels.csv"
  6. path3 = "data/JapaneseVowels/test.csv"
  7. path4 = "data/JapaneseVowels/testlabels.csv"
  8. train_data = Array(CSV.read(path1, DataFrame; header=false))
  9. train_label = Array(CSV.read(path2, DataFrame; header=false))[:,1]
  10. test_data = Array(CSV.read(path3, DataFrame; header=false))
  11. test_label = Array(CSV.read(path4, DataFrame; header=false))[:,1]
  12. figure(1)
  13. plot(train_data[1:12,1:20]')
  14. title("Training Observation 1")
  15. truncation_number = 29
  16. train_data = Array(train_data[:, 1:truncation_number])
  17. for i in range(1, 270 * 12)
  18. for j in range(1, truncation_number)
  19. if train_data[i, j] === missing
  20. train_data[i, j] = 0
  21. end
  22. end
  23. end
  24. train_data = reshape(train_data, (12, 270, truncation_number))
  25. train_data = permutedims(train_data, (2, 3, 1))
  26. train_label = Array(train_label)[:, 1]
  27. test_data = Array(test_data[:, 1:truncation_number])
  28. for i in range(1, 370 * 12)
  29. for j in range(1, truncation_number)
  30. if test_data[i, j] === missing
  31. test_data[i, j] = 0
  32. end
  33. end
  34. end
  35. test_data = reshape(test_data, (12, 370, truncation_number))
  36. test_data = permutedims(test_data, (2, 3, 1))
  37. test_label = Array(test_label)[:, 1]
  38. # 构建网络
  39. layers = SequentialCell([
  40. bilstmLayer(12, 100; NumLayers=3, Batch_First=true),
  41. flattenLayer(),
  42. fullyConnectedLayer(truncation_number * 200, 9),
  43. softmaxLayer(),
  44. ])
  45. options = trainingOptions(
  46. "CrossEntropyLoss", "Adam", "Accuracy", 27, 200, 0.001; Shuffle=true, Plots=true
  47. )
  48. net = trainNetwork(train_data, train_label, layers, options)
  49. YPred = TyDeepLearning.classify(net, test_data)
  50. accuracy = Accuracy(YPred, test_label)
  51. print(accuracy)

(3)基于LSTM的人类活动分类任务

  1. using TyDeepLearning
  2. using TyPlot
  3. train_data_label = CSV.read(
  4. "data/HumanActivity/HumanActivityTrain.csv", DataFrame; header=false
  5. )
  6. test_data_label = CSV.read(
  7. "data/HumanActivity/HumanActivityTest.csv", DataFrame; header=false
  8. )
  9. train_data_label = Array(train_data_label)
  10. test_data_label = Array(test_data_label)
  11. # 可视化
  12. figure(2)
  13. line_color = ["#0072BD", "#D95319", "#EDB120", "#7E2F8E", "#77AC30"]
  14. for i in range(1, 5)
  15. classes = [i - 1 for i in range(1, 5)]
  16. idx = TyBase.find(train_data_label[1:64480, 4] .== classes[i])
  17. hold("on")
  18. plot(idx, train_data_label[idx, 1], line_color[i])
  19. end
  20. hold("off")
  21. xlabel("Time Step")
  22. ylabel("Acceleration")
  23. title("Training Sequence 1, Feature 1")
  24. classes = ["Dancing", "Running", "Sitting", "Standing", "Walking"]
  25. legend(classes)
  26. figure(3)
  27. plot(test_data_label[:, 1:3])
  28. title("Test Data")
  29. xlabel("Time Step")
  30. legend(["Feature 1", "Feature 2", "Feature 3"])
  31. function create_datasets(data, t_window)
  32. out_seq = reshape(data[1:(1 + t_window - 1), :], (1, t_window, size(data)[2]))
  33. L = size(data)[1]
  34. for i in range(2, L - t_window + 1)
  35. train_seq = data[i:(i + t_window - 1), :]
  36. train_seq = reshape(train_seq, (1, t_window, size(data)[2]))
  37. out_seq = cat(out_seq, train_seq; dims=1)
  38. end
  39. return out_seq
  40. end
  41. t_window = 6
  42. train_data = train_data_label[:, 1:3]
  43. train_data = create_datasets(train_data, t_window)
  44. train_label = train_data_label[t_window:end, 4]
  45. test_data = test_data_label[:, 1:3]
  46. test_data = create_datasets(test_data, t_window)
  47. test_label = test_data_label[t_window:end, 4]
  48. numFeatures = 3
  49. numHiddenUnits = 200
  50. numClasses = 5
  51. layers = SequentialCell([
  52. lstmLayer(numFeatures, numHiddenUnits; NumLayers=1),
  53. flattenLayer(),
  54. fullyConnectedLayer(numHiddenUnits * t_window, numClasses),
  55. softmaxLayer(),
  56. ])
  57. options = trainingOptions(
  58. "CrossEntropyLoss", "Adam", "Accuracy", 512, 200, 0.005; Plots=true
  59. )
  60. net = trainNetwork(train_data, train_label, layers, options)
  61. preruslt = TyDeepLearning.classify(net, test_data)
  62. accuracy = Accuracy(preruslt, test_label)
  63. prelabel = Array{Int}(undef, 53883)
  64. for i in 1:53883
  65. prelabel_item = findmax(preruslt[i, :])[2] - 1
  66. prelabel[i] = prelabel_item
  67. end
  68. figure(4)
  69. idx = [1:53883...]
  70. hold("on")
  71. plot(idx, prelabel, ".", idx, test_label, "-")
  72. legend("Predicted")
  73. # plot(idx, prelabel, 0.1, "y", idx, test_label, "-")
  74. hold("off")
  75. xlabel("Time Step")
  76. ylabel("Activity")
  77. title("Predicted Activities")
  78. legend(["Predicted", "Test Data"])

(4)声纹识别

  1. using TyDeepLearning
  2. train_data, train_label, test_data, test_label = VPR_dataset( 1500)
  3. net = SequentialCell([
  4. convolution2dLayer(1, 80, (1, 251); PaddingMode="valid"),
  5. batchNormalization2dLayer(80),
  6. leakyReluLayer(0.2),
  7. maxPooling2dLayer((1, 3)),
  8. convolution2dLayer(80, 60, (1, 251); PaddingMode="valid"),
  9. batchNormalization2dLayer(60),
  10. leakyReluLayer(0.2),
  11. maxPooling2dLayer((1, 3)),
  12. convolution2dLayer(60, 60, (1, 5); PaddingMode="valid"),
  13. batchNormalization2dLayer(60),
  14. leakyReluLayer(0.2),
  15. maxPooling2dLayer((1, 3)),
  16. flattenLayer(),
  17. fullyConnectedLayer(60 * 2490, 2048),
  18. batchNormalization1dLayer(2048),
  19. leakyReluLayer(0.2),
  20. fullyConnectedLayer(2048, 1024),
  21. batchNormalization1dLayer(1024),
  22. leakyReluLayer(0.2),
  23. fullyConnectedLayer(1024, 256),
  24. batchNormalization1dLayer(256),
  25. leakyReluLayer(0.2),
  26. fullyConnectedLayer(256, 20),
  27. softmaxLayer()
  28. ])
  29. options = trainingOptions(
  30. "CrossEntropyLoss", "Adam", "Accuracy", 100, 150, 0.0005; Plots=true
  31. )
  32. net = trainNetwork(train_data, train_label, net, options)
  33. test_pred = TyDeepLearning.predict(net, test_data)
  34. train_pred = TyDeepLearning.predict(net, train_data)
  35. train_acc = Accuracy(train_pred, train_label)
  36. test_acc = Accuracy(test_pred, test_label)

(5)发动机剩余寿命预测

  1. using TyDeepLearning
  2. using DataFrames
  3. using TyPlot
  4. dir = "data/RUL/"
  5. # 训练集数据处理
  6. path1 = dir * "train_FD001.csv"
  7. train_FD001 = CSV.read(path1, DataFrame; header=false)
  8. f1 = figure("Feature Data"; figsize=[7, 8])
  9. color_list = ["r", "g", "b", "c", "m", "y", "k"]
  10. legend_list = [
  11. "op_setting_1",
  12. "op_setting_2",
  13. "op_setting_3",
  14. "Sensor1",
  15. "Sensor2",
  16. "Sensor3",
  17. "Sensor4",
  18. "Sensor5",
  19. "Sensor6",
  20. "Sensor7",
  21. "Sensor8",
  22. "Sensor9",
  23. "Sensor10",
  24. "Sensor11",
  25. "Sensor12",
  26. "Sensor13",
  27. "Sensor14",
  28. "Sensor15",
  29. "Sensor16",
  30. "Sensor17",
  31. "Sensor18",
  32. "Sensor19",
  33. "Sensor20",
  34. "Sensor21",
  35. ]
  36. for i in range(3, 26)
  37. subplot(24, 1, i - 2)
  38. plot(train_FD001[1:100, i], color_list[i % 7 + 1])
  39. yticklabels([])
  40. legend([legend_list[i - 2]]; loc="northeast")
  41. end
  42. # 寻找每个单元的最大cycle
  43. train_count = zeros(Int64, (100))
  44. for i in range(1, size(train_FD001)[1])
  45. for j in range(1, 100)
  46. if train_FD001[i, 1] == j
  47. train_count[j] = train_count[j] + 1
  48. else
  49. continue
  50. end
  51. end
  52. end
  53. RUL = zeros(Int64, (size(train_FD001)[1]))
  54. for i in range(1, size(train_FD001)[1])
  55. for j in range(1, 100)
  56. if train_FD001[i, 1] == j
  57. RUL[i] = train_count[j] - train_FD001[i, 2]
  58. else
  59. continue
  60. end
  61. end
  62. end
  63. # 删除某些在所有步长中保持不变的特征["op_setting_3", Sensor1", "Sensor5", "Sensor10", "Sensor16", "Sensor18", "Sensor19"]
  64. select!(
  65. train_FD001,
  66. Not([:Column5, :Column6, :Column10, :Column15, :Column21, :Column23, :Column24]),
  67. )
  68. f1 = figure("Feature Data"; figsize=[6, 6])
  69. color_list = ["r", "g", "b", "c", "m", "y", "k"]
  70. legend_list = [
  71. "op_setting_1",
  72. "op_setting_2",
  73. "Sensor2",
  74. "Sensor3",
  75. "Sensor4",
  76. "Sensor6",
  77. "Sensor7",
  78. "Sensor8",
  79. "Sensor9",
  80. "Sensor11",
  81. "Sensor12",
  82. "Sensor13",
  83. "Sensor14",
  84. "Sensor15",
  85. "Sensor17",
  86. "Sensor20",
  87. "Sensor21",
  88. ]
  89. for i in range(3, 19)
  90. subplot(17, 1, i - 2)
  91. plot(train_FD001[:, i], color_list[i % 7 + 1])
  92. yticklabels([])
  93. legend([legend_list[i - 2]]; loc="northeast")
  94. end
  95. # 添加一列名为RUL
  96. train_FD001 = insertcols!(train_FD001, ncol(train_FD001) + 1, :RUL => RUL)
  97. # 标准化
  98. feats = Array(train_FD001[!, 3:19])
  99. data_max = maximum(feats; dims=1)
  100. data_min = minimum(feats; dims=1)
  101. feats_norm = (feats .- data_min) ./ (data_max .- data_min)
  102. # 滑动窗口为31
  103. sequence_length = 31
  104. function gen_sequence(data_array, data_label, seq_length)
  105. num_elements = size(data_array)[1]
  106. label = data_label[seq_length:num_elements]
  107. data = reshape(data_array[1:seq_length, :], (1, seq_length, 17))
  108. dict = zip(range(2, num_elements - seq_length + 1), range(seq_length + 1, num_elements))
  109. for (start, stop) in dict
  110. data = vcat(data, reshape(data_array[start:stop, :], (1, seq_length, 17)))
  111. end
  112. return data, label
  113. end
  114. count_sum = zeros(Int64, (101))
  115. for i in range(1, 100)
  116. count_sum[i + 1] = train_count[i] + count_sum[i]
  117. end
  118. data_label = RUL[1:192]
  119. data_array = feats_norm[1:192, :]
  120. data, label = gen_sequence(data_array, data_label, sequence_length)
  121. for j in range(2, 100)
  122. data_array = feats_norm[(count_sum[j] + 1):count_sum[j + 1], :]
  123. data_label = RUL[(count_sum[j] + 1):count_sum[j + 1]]
  124. data_id, data_id_label = gen_sequence(data_array, data_label, sequence_length)
  125. data = cat(data, data_id; dims=1)
  126. label = cat(label, data_id_label; dims=1)
  127. end
  128. # 裁剪响应RUL阈值为150
  129. RUL_Threshold = 150
  130. for i in range(1, size(label)[1])
  131. if label[i] > 150
  132. label[i] = 150
  133. end
  134. end
  135. XTrain = permutedims(data, (1, 3, 2))
  136. YTrain = label
  137. # 网络构建
  138. layers = SequentialCell([
  139. convolution1dLayer(17, 32, 5),
  140. reluLayer(),
  141. convolution1dLayer(32, 64, 7),
  142. reluLayer(),
  143. convolution1dLayer(64, 128, 11),
  144. reluLayer(),
  145. convolution1dLayer(128, 256, 13),
  146. reluLayer(),
  147. convolution1dLayer(256, 512, 15),
  148. reluLayer(),
  149. flattenLayer(),
  150. fullyConnectedLayer(512 * sequence_length, 100),
  151. reluLayer(),
  152. dropoutLayer(0.5),
  153. fullyConnectedLayer(100, 1),
  154. ])
  155. options = trainingOptions("RMSELoss", "Adam", "MSE", 512, 200, 0.001; Plots=true)
  156. net = trainNetwork(XTrain, YTrain, layers, options)
  157. # 测试集数据处理
  158. path2 = dir * "test_FD001.csv"
  159. path3 = dir * "RUL_FD001.csv"
  160. test_FD001 = CSV.read(path2, DataFrame; header=false)
  161. RUL_FD001 = CSV.read(path3, DataFrame; header=false)
  162. select!(
  163. test_FD001,
  164. Not([:Column5, :Column6, :Column10, :Column15, :Column21, :Column23, :Column24]),
  165. )
  166. # 寻找每个单元的最大cycle
  167. test_count = zeros(Int64, (100))
  168. for i in range(1, size(test_FD001)[1])
  169. for j in range(1, 100)
  170. if test_FD001[i, 1] == j
  171. test_count[j] = test_count[j] + 1
  172. else
  173. continue
  174. end
  175. end
  176. end
  177. test_count_sum = zeros(Int64, (101))
  178. for i in range(1, 100)
  179. test_count_sum[i + 1] = test_count[i] + test_count_sum[i]
  180. end
  181. test_RUL = zeros(Int64, (size(test_FD001)[1]))
  182. for i in range(1, size(test_FD001)[1])
  183. for j in range(1, 100)
  184. if test_FD001[i, 1] == j
  185. test_RUL[i] = test_count[j] - test_FD001[i, 2] + RUL_FD001[j, 1]
  186. else
  187. continue
  188. end
  189. end
  190. end
  191. # 标准化处理
  192. data_test = Array(test_FD001[!, 3:19])
  193. data_max = maximum(data_test; dims=1)
  194. data_min = minimum(data_test; dims=1)
  195. test_norm = (data_test .- data_min) ./ (data_max .- data_min)
  196. test_data_array = test_norm[(test_count_sum[1] + 1):test_count_sum[1 + 1], :]
  197. test_data = reshape(
  198. test_data_array[(test_count[1] - sequence_length + 1):end, :], (1, sequence_length, 17)
  199. )
  200. for j in range(2, 100)
  201. test_data_array = test_norm[(test_count_sum[j] + 1):test_count_sum[j + 1], :]
  202. datadata = test_data_array[(test_count[j] - sequence_length + 1):end, :]
  203. data_reshape = reshape(datadata, (1, sequence_length, 17))
  204. test_data = cat(test_data, data_reshape; dims=1)
  205. end
  206. # 裁剪响应RUL阈值为150
  207. YTest = Array(RUL_FD001)
  208. RUL_Threshold = 150
  209. for i in range(1, size(YTest)[1])
  210. if YTest[i] > 150
  211. YTest[i] = 150
  212. end
  213. end
  214. XTest = permutedims(test_data, (1, 3, 2))
  215. Y = TyDeepLearning.predict(net, XTest)
  216. error = sqrt(mse(YTest, Y))
  217. hold("on")
  218. plot(Y, "-o")
  219. plot(YTest, "-v")
  220. legend(["Prediction value", "True value"])
  221. hold("off")
  222. sequence_length = 31
  223. function gen_sequence(data_array, data_label, seq_length)
  224. num_elements = size(data_array)[1]
  225. label = data_label[seq_length:num_elements]
  226. data = reshape(data_array[1:seq_length, :], (1, seq_length, 17))
  227. dict = zip(range(2, num_elements - seq_length + 1), range(seq_length + 1, num_elements))
  228. for (start, stop) in dict
  229. data = vcat(data, reshape(data_array[start:stop, :], (1, seq_length, 17)))
  230. end
  231. return data, label
  232. end
  233. count_sum = zeros(Int64, (101))
  234. for i in range(1, 100)
  235. count_sum[i + 1] = train_count[i] + count_sum[i]
  236. end
  237. data_label = RUL[848:1116]
  238. data_array = feats_norm[848:1116, :]
  239. data, label = gen_sequence(data_array, data_label, sequence_length)
  240. # 裁剪响应RUL阈值为150
  241. RUL_Threshold = 150
  242. for i in range(1, size(label)[1])
  243. if label[i] > 150
  244. label[i] = 150
  245. end
  246. end
  247. XTrain1 = permutedims(data, (1, 3, 2))
  248. YTrain1 = label
  249. Y1 = TyDeepLearning.predict(net, XTrain1)
  250. error = sqrt(mse(YTrain1, Y1))
  251. hold("on")
  252. plot(Y1)
  253. plot(YTrain1,)
  254. legend(["Prediction value", "True value"])
  255. hold("off")

MindSpore官方资料

官方QQ群 : 871543426

官网:https://www.mindspore.cn/

Gitee : https://gitee.com/mindspore/mindspore

GitHub : https://github.com/mindspore-ai/mindspore

论坛:https://www.hiascend.com/forum/forum-0106101385921175002-1.html

Openl启智社区:https://openi.org.cn

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/1011936
推荐阅读
相关标签
  

闽ICP备14008679号