在之前的博文中我们用TensorFlow与PyTorch进行了拟合曲线,到达了不错的效果。
我们现在使用MXNet进行相同的曲线拟合,进而来比较一下TensorFlow与PyTorch的异同。
搭建神经网络进行训练的步骤基本相同,我们现在开始用MXNet来实现。
问题描述
拟合y= x*x -2x +3 + 0.1(-1到1的随机值) 曲线
给定x范围(0,3)
问题分析
在直线拟合博客中,我们使用最简单的y=wx+b的模型成功拟合了一条直线,现在我们在进一步进行曲线的拟合。简单的y=wx+b模型已经无法满足我们的需求,需要利用更多的神经元来解决问题了。
生成数据
import numpy as np
import matplotlib.pyplot as plt
import torch as t
from torch.autograd import Variable as var
def get_data(x,w,b,d):
c,r = x.shape
y = (w * x * x + b*x + d)+ (0.1*(2*np.random.rand(c,r)-1))
return(y)
xs = np.arange(0,3,0.01).reshape(-1,1)
ys = get_data(xs,1,-2,3)
xs = var(t.Tensor(xs))
ys = var(t.Tensor(ys))
生成的数据图像为:
from mxnet.gluon import loss,nn,data
from mxnet import autograd, nd, gluon,init
import numpy as np
import matplotlib.pyplot as plt
def get_data(x,w,b,d):
c,r = x.shape
y = (w * x * x + b*x + d)+ (0.1*(2*np.random.rand(c,r)-1))
return(y)
xs = np.arange(0,3,0.01).reshape(-1,1)
ys = get_data(xs,1,-2,3)
xs,ys = nd.array(xs),nd.array(ys)
batch_size = 100
# 将训练数据的特征和标签组合。
dataset = data.ArrayDataset(xs, ys)
# 随机读取小批量。
data_iter = data.DataLoader(dataset, batch_size, shuffle=True)
model = nn.Sequential()
model.add(nn.Dense(16,activation='relu'))
model.add(nn.Dense(1))
model.initialize(init.Normal(sigma=0.01))
print(model)
loss_f = loss.L2Loss()
trainer = gluon.Trainer(model.collect_params(), 'Adam', {'learning_rate': 0.1})
num_epochs = 1000
for epoch in range(1, num_epochs + 1):
for X, y in data_iter:
with autograd.record():
l = loss_f(model(X), y)
l.backward()
trainer.step(batch_size)
l = loss_f(model(xs), ys)
if(epoch%100==0):print('epoch %d, loss: %f' % (epoch, l.mean().asnumpy()))
ys_pre = model(xs)
plt.title("curve")
plt.plot(xs.asnumpy(),ys.asnumpy())
plt.plot(xs.asnumpy(),ys_pre.asnumpy())
plt.show()
Sequential(
(0): Dense(None -> 16, Activation(relu))
(1): Dense(None -> 1, linear)
)
epoch 100, loss: 0.229648
epoch 200, loss: 0.233721
epoch 300, loss: 0.233185
epoch 400, loss: 0.178324
epoch 500, loss: 0.018889
epoch 600, loss: 0.009249
epoch 700, loss: 0.007344
epoch 800, loss: 0.003552
epoch 900, loss: 0.003080
epoch 1000, loss: 0.002648