使用简单的Gluon简洁实现上一篇文章功能:
具体代码如下:
from mxnet import autograd,nd num_inputs = 2 num_examples = 1000 true_w = [2,3.4] true_b = 4.2 features = nd.random.normal(scale=1,shape=(num_examples, num_inputs)) labels = true_w[0] * features[:,0] + true_w[1] * features[:,1] + true_b labels += nd.random.normal(scale=0.01, shape = labels.shape) from mxnet.gluon import data as gdata batch_size = 10 dataset = gdata.ArrayDataset(features, labels) data_iter = gdata.DataLoader(dataset, batch_size, shuffle=True) for x,y in data_iter: print(x,y) break from mxnet.gluon import nn net = nn.Sequential() net.add(nn.Dense(1)) from mxnet import init net.initialize(init.Normal(sigma=0.01)) from mxnet.gluon import loss as gloss loss = gloss.L2Loss() from mxnet import gluon trainer = gluon.Trainer(net.collect_params(),'sgd',{'learning_rate':0.03}) num_epochs = 8 for epoch in range(1,num_epochs +1): for x,y in data_iter: with autograd.record(): l = loss(net(x),y) l.backward() trainer.step(batch_size) l=loss(net(features),labels) print('epoch %d, loss: %f' %(epoch, l.mean().asnumpy())) dense=net[0] print(true_w,dense.weight.data()) print(true_b,dense.bias.data())
实现效果如下: