使用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选项。

非线性函数拟合

在前面这篇博客中我们所拟合的是一个简单的线性函数:

\[y=ax+b \]

那么在这里我们先考虑一个最简单的非线性函数的场景:

\[y=ax^2+b \]

同样的还是两个参数,需要注意的是,如果要用线性神经网络来拟合非线性的函数,那么在给出参数的时候就要给出非线性的入参,以下是完整的代码(如果需要展示结果更好看的话可以参考上面提到的线性函数拟合的博客,这里我们为了提速,删除了很多非比要的模块):

# 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个入参,这里我们要拟合的函数模型是:

\[z(x,y)=ax^2+by^3+c \]

以下是完整的代码实例:

# 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\)也是非常接近的。

其他的函数类型

使用上一章节中所介绍的方法,不仅可以拟合多参数、多幂次的函数,同样的可以拟合一些其他的初等函数,比如:

\[z(x,y)=ax^2+b\ sin(y)+c \]

完整的代码如下所示:

# 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/

“留一手”加剧内卷,“讲不清”浪费时间。
posted @ 2021-05-15 10:48  DECHIN  阅读(261)  评论(0编辑  收藏  举报