使用MindSpore的线性神经网络拟合非线性函数
技术背景
在前面的几篇博客中,我们分别介绍了MindSpore的CPU版本在Docker下的安装与配置方案、MindSpore的线性函数拟合以及MindSpore后来新推出的GPU版本的Docker编程环境解决方案。这里我们在线性拟合的基础上,再介绍一下MindSpore中使用线性神经网络来拟合多变量非线性函数的解决方案。
环境配置
在按照这篇博客中的方法进行安装和配置之后,可以在本地的docker镜像仓库中找到一个mindspore的镜像:
[dechin-manjaro gitlab]# docker images
REPOSITORY TAG IMAGE ID
mindspore latest 3a6951d9b900
由于运行Docker容器的时候最好是可以绑定本地的一个目录,所以可以用pwd
先看一下本地的执行目录:
[dechin@dechin-manjaro mindspore]$ pwd
/home/dechin/projects/2021-python/mindspore
然后可以根据上述的名称和路径信息,创建一个新的docker容器环境:
[dechin-manjaro mindspore]# docker run -it --rm -v /home/dechin/projects/2021-python/mindspore:/home mindspore
这里运行的时候加了--rm
选项,表示关闭容器后就删除容器,是一个一次性容器的选项。如果不加这个选项的话,会在docker ps -a
下看到众多的被保存下来的容器环境,这会浪费不必要的资源。因此如果不对环境进行变更的话,建议还是加上--rm
选项。
非线性函数拟合
在前面这篇博客中我们所拟合的是一个简单的线性函数:
\]
那么在这里我们先考虑一个最简单的非线性函数的场景:
\]
同样的还是两个参数,需要注意的是,如果要用线性神经网络来拟合非线性的函数,那么在给出参数的时候就要给出非线性的入参,以下是完整的代码(如果需要展示结果更好看的话可以参考上面提到的线性函数拟合的博客,这里我们为了提速,删除了很多非比要的模块):
# test_nonlinear.py
from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
import numpy as np
from mindspore import dataset as ds
from mindspore import nn, Tensor, Model
import time
from mindspore.train.callback import Callback, LossMonitor
def get_data(num, a=2.0, b=3.0):
for _ in range(num):
x = np.random.uniform(-1.0, 1.0)
noise = np.random.normal(0, 0.03)
z = a * x ** 2 + b + noise
# 返回数据的时候就返回数据的平方
yield np.array([x**2]).astype(np.float32), np.array([z]).astype(np.float32)
def create_dataset(num_data, batch_size=16, repeat_size=1):
input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z'])
input_data = input_data.batch(batch_size)
input_data = input_data.repeat(repeat_size)
return input_data
data_number = 1600 # 一共产生1600组数据
batch_number = 16 # 分为16组分别进行优化
repeat_number = 2 # 重复2次,可以取到更低的损失函数值
ds_train = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number)
dict_datasets = next(ds_train.create_dict_iterator())
class LinearNet(nn.Cell):
def __init__(self):
super(LinearNet, self).__init__()
self.fc = nn.Dense(1, 1, 0.02, 0.02)
def construct(self, x):
x = self.fc(x)
return x
net = LinearNet()
model_params = net.trainable_params()
print ('Param Shape is: {}'.format(len(model_params)))
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
net_loss = nn.loss.MSELoss()
# 设定优化算法,常用的是Momentum和ADAM
optim = nn.Momentum(net.trainable_params(), learning_rate=0.005, momentum=0.9)
model = Model(net, net_loss, optim)
epoch = 1
# 设定每8个batch训练完成后就播报一次,这里一共播报25次
model.train(epoch, ds_train, callbacks=[LossMonitor(8)], dataset_sink_mode=False)
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
在容器内直接用python来运行这个代码:
root@9f9358769573:/home# python test_nonlinear.py
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.
[WARNING] ME(818:140122680680576,MainProcess):2021-05-15-01:59:46.275.616 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
Param Shape is: 2
Parameter (name=fc.weight) [[0.02]]
Parameter (name=fc.bias) [0.02]
epoch: 1 step: 8, loss is 7.727301
epoch: 1 step: 16, loss is 0.9204254
epoch: 1 step: 24, loss is 0.051439833
epoch: 1 step: 32, loss is 0.40419307
epoch: 1 step: 40, loss is 0.24850279
epoch: 1 step: 48, loss is 0.0988866
epoch: 1 step: 56, loss is 0.0244435
epoch: 1 step: 64, loss is 0.05162067
epoch: 1 step: 72, loss is 0.043285627
epoch: 1 step: 80, loss is 0.013224524
epoch: 1 step: 88, loss is 0.014601769
epoch: 1 step: 96, loss is 0.0086541185
epoch: 1 step: 104, loss is 0.014336493
epoch: 1 step: 112, loss is 0.007790237
epoch: 1 step: 120, loss is 0.007644728
epoch: 1 step: 128, loss is 0.0090831155
epoch: 1 step: 136, loss is 0.009039238
epoch: 1 step: 144, loss is 0.0061546825
epoch: 1 step: 152, loss is 0.006427808
epoch: 1 step: 160, loss is 0.004290967
epoch: 1 step: 168, loss is 0.003127655
epoch: 1 step: 176, loss is 0.0044714734
epoch: 1 step: 184, loss is 0.003788504
epoch: 1 step: 192, loss is 0.0025329243
epoch: 1 step: 200, loss is 0.0034840193
Parameter (name=fc.weight) [[1.8488435]]
Parameter (name=fc.bias) [3.0522664]
最后优化的结果为\(f(x)=1.8488435x+3.0522664\),可以看到距离我们所预期的\(f(x)=2x+3\)已经是非常接近的,但是最后具体能训练到多少的精度,其实很大程度上跟训练集有关,这里因为训练集的范围取的比较小,可能会有一定的影响。
多变量函数拟合
不论是前面提到的线性函数拟合的场景,或者是上一个章节中单变量非线性函数的拟合,其实都只有1个输入参数,本章节介绍的场景有2个入参,这里我们要拟合的函数模型是:
\]
以下是完整的代码实例:
# test_nonlinear.py
from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
import numpy as np
from mindspore import dataset as ds
from mindspore import nn, Tensor, Model
import time
from mindspore.train.callback import Callback, LossMonitor
def get_data(num, a=2.0, b=3.0, c=5.0):
for _ in range(num):
x = np.random.uniform(-1.0, 1.0)
y = np.random.uniform(-1.0, 1.0)
noise = np.random.normal(0, 0.03)
z = a * x ** 2 + b * y ** 3 + c + noise
# 返回参数的时候压缩在一个数组内
yield np.array([x**2,y**3]).astype(np.float32), np.array([z]).astype(np.float32)
def create_dataset(num_data, batch_size=16, repeat_size=1):
input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z'])
input_data = input_data.batch(batch_size)
input_data = input_data.repeat(repeat_size)
return input_data
data_number = 1600
batch_number = 16
repeat_number = 2
ds_train = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number)
dict_datasets = next(ds_train.create_dict_iterator())
class LinearNet(nn.Cell):
def __init__(self):
super(LinearNet, self).__init__()
# 神经网络的input和output维度设置为2,1
self.fc = nn.Dense(2, 1, 0.02, 0.02)
def construct(self, x):
x = self.fc(x)
return x
net = LinearNet()
model_params = net.trainable_params()
print ('Param Shape is: {}'.format(len(model_params)))
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
net_loss = nn.loss.MSELoss()
optim = nn.Momentum(net.trainable_params(), learning_rate=0.005, momentum=0.9)
model = Model(net, net_loss, optim)
epoch = 1
model.train(epoch, ds_train, callbacks=[LossMonitor(8)], dataset_sink_mode=False)
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
然后用同样的方法来运行:
root@9f9358769573:/home# python test_nonlinear.py
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.
[WARNING] ME(1256:140024122265728,MainProcess):2021-05-15-02:03:11.597.713 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
Param Shape is: 2
Parameter (name=fc.weight) [[0.02 0.02]]
Parameter (name=fc.bias) [0.02]
epoch: 1 step: 8, loss is 23.713902
epoch: 1 step: 16, loss is 4.097528
epoch: 1 step: 24, loss is 0.6977275
epoch: 1 step: 32, loss is 1.6294805
epoch: 1 step: 40, loss is 1.6045574
epoch: 1 step: 48, loss is 0.33872497
epoch: 1 step: 56, loss is 0.2145875
epoch: 1 step: 64, loss is 0.15970881
epoch: 1 step: 72, loss is 0.18631926
epoch: 1 step: 80, loss is 0.093276486
epoch: 1 step: 88, loss is 0.030935105
epoch: 1 step: 96, loss is 0.065783374
epoch: 1 step: 104, loss is 0.036098357
epoch: 1 step: 112, loss is 0.021177795
epoch: 1 step: 120, loss is 0.022446468
epoch: 1 step: 128, loss is 0.024930779
epoch: 1 step: 136, loss is 0.025752623
epoch: 1 step: 144, loss is 0.019025242
epoch: 1 step: 152, loss is 0.012783089
epoch: 1 step: 160, loss is 0.008119936
epoch: 1 step: 168, loss is 0.010284519
epoch: 1 step: 176, loss is 0.0034211911
epoch: 1 step: 184, loss is 0.003315867
epoch: 1 step: 192, loss is 0.0035076286
epoch: 1 step: 200, loss is 0.0032196175
Parameter (name=fc.weight) [[1.9629341 2.9065483]]
Parameter (name=fc.bias) [5.004186]
这里得到的结果\(z(x,y)=1.9629341x^2+2.9065483y^3+5.004186\)跟我们所预期的结果\(z(x,y)=2x^2+3y^3+5\)也是非常接近的。
其他的函数类型
使用上一章节中所介绍的方法,不仅可以拟合多参数、多幂次的函数,同样的可以拟合一些其他的初等函数,比如:
\]
完整的代码如下所示:
# test_nonlinear.py
from mindspore import context
context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
import numpy as np
from mindspore import dataset as ds
from mindspore import nn, Tensor, Model
import time
from mindspore.train.callback import Callback, LossMonitor
def get_data(num, a=4.0, b=2.0, c=7.0):
for _ in range(num):
x = np.random.uniform(-1.0, 1.0)
y = np.random.uniform(-1.0, 1.0)
noise = np.random.normal(0, 0.03)
z = a * x ** 2 + b * np.sin(y) + c + noise
yield np.array([x**2,np.sin(y)]).astype(np.float32), np.array([z]).astype(np.float32)
def create_dataset(num_data, batch_size=16, repeat_size=1):
input_data = ds.GeneratorDataset(list(get_data(num_data)), column_names=['x','z'])
input_data = input_data.batch(batch_size)
input_data = input_data.repeat(repeat_size)
return input_data
data_number = 1600
batch_number = 16
repeat_number = 2
ds_train = create_dataset(data_number, batch_size=batch_number, repeat_size=repeat_number)
dict_datasets = next(ds_train.create_dict_iterator())
class LinearNet(nn.Cell):
def __init__(self):
super(LinearNet, self).__init__()
self.fc = nn.Dense(2, 1, 0.02, 0.02)
def construct(self, x):
x = self.fc(x)
return x
net = LinearNet()
model_params = net.trainable_params()
print ('Param Shape is: {}'.format(len(model_params)))
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
net_loss = nn.loss.MSELoss()
optim = nn.Momentum(net.trainable_params(), learning_rate=0.005, momentum=0.9)
model = Model(net, net_loss, optim)
epoch = 1
model.train(epoch, ds_train, callbacks=[LossMonitor(8)], dataset_sink_mode=False)
for net_param in net.trainable_params():
print(net_param, net_param.asnumpy())
还是同样的执行方法:
root@9f9358769573:/home# python test_nonlinear.py
WARNING: 'ControlDepend' is deprecated from version 1.1 and will be removed in a future version, use 'Depend' instead.
[WARNING] ME(2132:139783326998656,MainProcess):2021-05-15-02:07:23.220.797 [mindspore/ops/operations/array_ops.py:2302] WARN_DEPRECATED: The usage of Pack is deprecated. Please use Stack.
Param Shape is: 2
Parameter (name=fc.weight) [[0.02 0.02]]
Parameter (name=fc.bias) [0.02]
epoch: 1 step: 8, loss is 33.49754
epoch: 1 step: 16, loss is 5.38931
epoch: 1 step: 24, loss is 0.6019592
epoch: 1 step: 32, loss is 2.3307486
epoch: 1 step: 40, loss is 1.4274476
epoch: 1 step: 48, loss is 0.48646024
epoch: 1 step: 56, loss is 0.15924525
epoch: 1 step: 64, loss is 0.20410316
epoch: 1 step: 72, loss is 0.21361557
epoch: 1 step: 80, loss is 0.09532051
epoch: 1 step: 88, loss is 0.06947755
epoch: 1 step: 96, loss is 0.05294941
epoch: 1 step: 104, loss is 0.04545658
epoch: 1 step: 112, loss is 0.032882713
epoch: 1 step: 120, loss is 0.04866419
epoch: 1 step: 128, loss is 0.033258867
epoch: 1 step: 136, loss is 0.023843747
epoch: 1 step: 144, loss is 0.024475154
epoch: 1 step: 152, loss is 0.015055884
epoch: 1 step: 160, loss is 0.016890783
epoch: 1 step: 168, loss is 0.019164825
epoch: 1 step: 176, loss is 0.014314213
epoch: 1 step: 184, loss is 0.010082506
epoch: 1 step: 192, loss is 0.009690857
epoch: 1 step: 200, loss is 0.008122671
Parameter (name=fc.weight) [[3.6814337 1.9820771]]
Parameter (name=fc.bias) [7.106203]
得到的结果是\(z(x,y)=3.6814337x^2+1.9820771sin(y)+7.106203\),而我们所预期的结果为:\(z(x,y)=4x^2+2sin(y)+7\)。
总结概要
基于前面所介绍的MindSpore框架使用线性神经网络来拟合线性函数的一个方案,这里我们将其推广到多参数、多幂次的函数,甚至是一些初等的函数。但是对于更加复杂的嵌套函数,可能还要思考一下更加普适的解决方案。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/nonlinear.html
作者ID:DechinPhy
更多原著文章请参考:https://www.cnblogs.com/dechinphy/
使用MindSpore的线性神经网络拟合非线性函数的更多相关文章
- tensorflow神经网络拟合非线性函数与操作指南
本实验通过建立一个含有两个隐含层的BP神经网络,拟合具有二次函数非线性关系的方程,并通过可视化展现学习到的拟合曲线,同时随机给定输入值,输出预测值,最后给出一些关键的提示. 源代码如下: # -*- ...
- 最小二乘法拟合非线性函数及其Matlab/Excel 实现(转)
1.最小二乘原理 Matlab直接实现最小二乘法的示例: close x = 1:1:100; a = -1.5; b = -10; y = a*log(x)+b; yrand = y + 0.5*r ...
- 最小二乘法拟合非线性函数及其Matlab/Excel 实现
1.最小二乘原理 Matlab直接实现最小二乘法的示例: close x = 1:1:100; a = -1.5; b = -10; y = a*log(x)+b; yrand = y + 0.5*r ...
- BP神经网络拟合给定函数
近期在准备美赛,因为比赛需要故重新安装了matlab,在里面想尝试一下神将网络工具箱.就找了一个看起来还挺赏心悦目的函数例子练练手: y=1+sin(1+pi*x/4) 针对这个函数,我们首先画出其在 ...
- MATLAB神经网络(2) BP神经网络的非线性系统建模——非线性函数拟合
2.1 案例背景 在工程应用中经常会遇到一些复杂的非线性系统,这些系统状态方程复杂,难以用数学方法准确建模.在这种情况下,可以建立BP神经网络表达这些非线性系统.该方法把未知系统看成是一个黑箱,首先用 ...
- MATLAB神经网络(3) 遗传算法优化BP神经网络——非线性函数拟合
3.1 案例背景 遗传算法(Genetic Algorithms)是一种模拟自然界遗传机制和生物进化论而形成的一种并行随机搜索最优化方法. 其基本要素包括:染色体编码方法.适应度函数.遗传操作和运行参 ...
- 『TensorFlow』第二弹_线性拟合&神经网络拟合_恰是故人归
Step1: 目标: 使用线性模拟器模拟指定的直线:y = 0.1*x + 0.3 代码: import tensorflow as tf import numpy as np import matp ...
- 非线性函数的最小二乘拟合及在Jupyter notebook中输入公式 [原创]
突然有个想法,能否通过学习一阶RC电路的阶跃响应得到RC电路的结构特征——时间常数τ(即R*C).回答无疑是肯定的,但问题是怎样通过最小二乘法.正规方程,以更多的采样点数来降低信号采集噪声对τ估计值的 ...
- MATLAB神经网络(7) RBF网络的回归——非线性函数回归的实现
7.1 案例背景 7.1.1 RBF神经网络概述 径向基函数是多维空间插值的传统技术,RBF神经网络属于前向神经网络类型,网络的结构与多层前向网络类似,是一种三层的前向网络.第一层为输入层,由信号源结 ...
随机推荐
- C 语言代码风格之 Linux 内核代码风格
GitHub: https://github.com/storagezhang Emai: debugzhang@163.com 华为云社区:https://bbs.huaweicloud.com/b ...
- Android Studio 之 通过 Intent 完成点击按钮实现页面跳转
•Intent 简介 Intent 是 Android 程序中各组件之间进行交互的一种重要方式: 它不仅可以指明当前组件想要执行的动作,还可以在不同组件之间传递数据. Intent 有多个构造函数,其 ...
- 【DB宝46】NoSQL数据库之CouchBase简介、集群搭建、XDCR同步及备份恢复
目录 一. CouchBase概述 1.1.简述 1.2.CouchDB和CouchBase比对 1.2.1.CouchDB和CouchBase的相同之处 1.2.2.CouchDB和CouchBas ...
- String类的使用2
/*String:字符串,使用一对""引起来表示.1.String声明为final的,不可被继承2.String实现了Serializable接口:表示字符串是支持序列化的. 实现 ...
- C语言-字符串函数的实现(一)之strlen
C语言中的字符串函数有如下这些 获取字符串长度 strlen 长度不受限制的字符串函数 strcpy strcat strcmp 长度受限制的字符串函数 strncpy strncat strncmp ...
- SQLlite实现增删查改
activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android ...
- 基于sinc的音频重采样(二):实现
上篇(基于sinc的音频重采样(一):原理)讲了基于sinc方法的重采样原理,并给出了数学表达式,如下: (1) 本文讲如何基于这个数学表达式来做软件实现.软件实现的 ...
- 华为分析+App Linking:一站式解决拉新、留存、促活难
移动互联网时代,用户注意力稀缺,"如何让用户一键直达APP特定页面"越来越受到产品和运营同学的关注. 比如在各个渠道投放了APP安装广告,希望新用户下载APP首次打开时直接进入活动 ...
- 我与Git的那些破事(上)--代码管理
1. Git是什么? 作为一名程序猿,我相信大家都或多或少接触过git--分布式版本控制软件. 有人说,它是目前世界上最先进的分布式版本控制系统,我想说,是否最先进不知道,但确实好用,实用. 作为一款 ...
- Matrix Chain Multiplication UVA - 442
Suppose you have to evaluate an expression like ABCDE where A,B,C,D and E are matrices. Since matrix ...