标签:near back code att logs 结果 ret 随机梯度 super
下列代码来自 https://zh.gluon.ai/chapter_supervised-learning/linear-regression-scratch.html
1 from mxnet import ndarray as nd 2 from mxnet import autograd 3 import random 4 5 num_inputs = 2 6 num_examples = 1000 7 8 true_w = [2, -3.4] 9 true_b = 4.2 10 11 X = nd.random_normal(shape=(num_examples, num_inputs)) #1000行,2列的数据集 12 y = true_w[0] * X[:, 0] + true_w[1] * X[:, 1] + true_b #已知答案的结果 13 y += .01 * nd.random_normal(shape=y.shape) #加入噪音 14 15 batch_size = 10 16 def data_iter(): 17 #产生一个随机索引列表 18 idx = list(range(num_examples)) 19 random.shuffle(idx) 20 for i in range(0, num_examples, batch_size): 21 j = nd.array(idx[i:min(i+batch_size,num_examples)]) 22 yield nd.take(X, j), nd.take(y, j) #每次随机取出10行,2列的数据 23 24 #初始化模型参数(即:需要求解的参数变量) 25 w = nd.random_normal(shape=(num_inputs, 1)) 26 b = nd.zeros((1,)) 27 params = [w, b] 28 29 #创建梯度 30 for param in params: 31 param.attach_grad() 32 33 #定义线性回归模型 34 def net(X): 35 return nd.dot(X, w) + b 36 37 #定义损失函数 38 def square_loss(yhat, y): 39 # 注意这里我们把y变形成yhat的形状来避免自动广播 40 return (yhat - y.reshape(yhat.shape)) ** 2 41 42 #随机梯度下降法 43 def SGD(params, lr): 44 for param in params: 45 param[:] = param - lr * param.grad 46 47 48 #训练 49 epochs = 5 50 learning_rate = .001 51 for e in range(epochs): 52 total_loss = 0 53 for data, label in data_iter(): 54 with autograd.record(): 55 output = net(data) 56 loss = square_loss(output, label) 57 loss.backward() 58 SGD(params, learning_rate) 59 60 total_loss += nd.sum(loss).asscalar() 61 print("Epoch %d, average loss: %f" % (e, total_loss/num_examples)) 62 63 print(true_w) #打印答案 64 print(w) #打印求解结果 65 66 print(true_b) #打印答案 67 print(b) #打印求解结果
Epoch 0, average loss: 6.012281 Epoch 1, average loss: 0.102830 Epoch 2, average loss: 0.001909 Epoch 3, average loss: 0.000133 Epoch 4, average loss: 0.000101 #5次迭代后,已经快速收敛 [2, -3.4] #已知答案 [[ 2.00017834] #求解结果 [-3.40006614]] <NDArray 2x1 @cpu(0)> 4.2 [ 4.19863892] <NDArray 1 @cpu(0)>
标签:near back code att logs 结果 ret 随机梯度 super
原文地址:http://www.cnblogs.com/yjmyzz/p/7764496.html