这篇文章主要还是针对于简单的线性回归的复现,使用的线性回归二维特征,损失函数是二次函数,采用SGD梯度下降来迭代。迭代周期数是3。注释在代码中有写。
#coding=utf-8
#%matplotlib inline
import mxnet
from mxnet import autograd
import random
import matplotlib.pyplot as plt
from mxnet import ndarray as nd
#generate the dataset
# the dimension number is 2,characteristic number is 2
num_inputs = 2
# the number of training dataset is 1000
num_examples = 1000
# the true weight is 2 and -3.4 (2 dimensions)
true_w = [2, -3.4]
# the true bias is 4.2
true_b = 4.2
# Randomly generated training data X with mean of 0 and variance of 1,
# obeying normal distribution(正态分布)
features= nd.random_normal(shape=(num_examples, num_inputs))
# Generate corresponding output y according to X, w, B
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
# Add random noise to y
labels += 0.01 * nd.random_normal(shape=labels.shape)
print(features[0],