错误修正前
from mxnet import ndarray
from mxnet import autograd
import matplotlib.pyplot as plt
true_w = [-2, 5]
true_b = 1.5
num_batch = 1000
X = ndarray.random_normal(shape=[num_batch, 2])
y = ndarray.ones(shape=[num_batch, 1])
y = X[:, 0]*true_w[0]+X[:, 1]*true_w[1]+true_b
其中groundtrue的写法一般写成true_x。如true_w
数值型的变量一般用num_x。训练集写成num_examples
ndarray.random_normal生成正态分布
ndarray.ondes生成全1矩阵,上面可以不用给y初始化。
修订后代码
from mxnet import ndarray
from mxnet import autograd
import matplotlib.pyplot as plt
true_w = [-2, 5]
true_b = 1.5
num_inputs = 2
num_examples = 1000
X = ndarray.random_normal(shape=[num_examples, num_inputs])
y = X[:, 0]*true_w[0]+X[:, 1]*true_w[1]+true_b
print(X[0:5],y[0:5])
[[ 1.16307855 0.48380461]
[ 0.29956347 0.15302546]
[-1.16881478 1.55807102]
[-0.54594457 -2.35562968]
[ 0.54144025 2.67850637]]
5x2 @cpu(0)>
[ 1.59286594 1.66600037 11.627985 -9.18625927 13.80965233]
5 @cpu(0)>
plt.scatter(X[:, 1].asnumpy(), y.asnumpy())
plt.show()