In [1]:
from __future__ import print_function
import mxnet as mx
from mxnet import nd, autograd
import numpy
import matplotlib.pyplot as plt
mx.random.seed(1)
In [2]:
learning_rate = 0.01
training_epochs = 1000
smoothing_constant = 0.01
display_step = 50
ctx = mx.cpu()
In [3]:
train_X = numpy.asarray([3.3, 4.4, 5.5, 6.71, 6.93, 4.168, 9.779, 6.182, 7.59, 2.167,
7.042, 10.791, 5.313, 7.997, 5.654, 9.27,3.1])
train_Y = numpy.asarray([1.7, 2.76, 2.09, 3.19, 1.694, 1.573, 3.366, 2.596, 2.53, 1.221,
2.827, 3.465, 1.65, 2.904, 2.42, 2.94, 1.3])
n_samples = train_X.shape[0]
In [4]:
# Set model weights,初始化网络模型的权重
W = nd.random_normal(shape=1)
b = nd.random_normal(shape=1)
params = [W, b]
for param in params:
param.attach_grad()
In [5]:
def net(X):
return X*W + b
In [6]:
# Mean squared error,损失函数:均方差
def square_loss(yhat, y):
return nd.mean((yhat - y) ** 2)
# Gradient descent, 优化方式:梯度下降
def SGD(params, lr):
for param in params:
param[:] = param - lr * param.grad
In [7]:
# Fit training data
data = nd.array(train_X)
label = nd.array(train_Y)
losses = []
moving_loss = 0
niter = 0
for e in range(training_epochs):
with autograd.record():
output = net(data)
loss = square_loss(output, label)
loss.backward()
SGD(params, learning_rate)
##########################
# Keep a moving average of the losses
##########################
niter +=1
curr_loss = nd.mean(loss).asscalar()
moving_loss = (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss
# correct the bias from the moving averages
est_loss = moving_loss/(1-(1-smoothing_constant)**niter)
losses.append(est_loss)
if (e + 1) % display_step == 0:
print("Epoch:", '%04d' % (e), "cost=", "{:.9f}".format(curr_loss), "W=", W.asnumpy()[0], "b=", b.asnumpy()[0])
In [8]:
def plot(losses, X, Y, n_samples=10):
xs = list(range(len(losses)))
f, (fg1, fg2) = plt.subplots(1, 2)
fg1.set_title('Loss during training')
fg1.plot(xs, losses, '-r')
fg2.set_title('Estimated vs real function')
fg2.plot(X.asnumpy(), net(X).asnumpy(), 'or', label='Estimated')
fg2.plot(X.asnumpy(), Y.asnumpy(), '*g', label='Real')
fg2.legend()
plt.show()
plot(losses, data, label)