MXNET: Weight attenuation-gluon implementation

Source: Internet
Author: User
Tags mxnet

Building a data set
# -*- coding: utf-8 -*-from mxnet import initfrom mxnet import ndarray as ndfrom mxnet.gluon import loss as glossimport gbn_train = 20n_test = 100num_inputs = 200true_w = nd.ones((num_inputs, 1)) * 0.01true_b = 0.05features = nd.random.normal(shape=(n_train+n_test, num_inputs))labels = nd.dot(features, true_w) + true_blabels += nd.random.normal(scale=0.01, shape=labels.shape)train_features, test_features = features[:n_train, :], features[n_train:, :]train_labels, test_labels = labels[:n_train], labels[n_train:]
Data iterators
from mxnet import autogradfrom mxnet.gluon import data as gdatabatch_size = 1num_epochs = 10learning_rate = 0.003train_iter = gdata.DataLoader(gdata.ArrayDataset(    train_features, train_labels), batch_size, shuffle=True)loss = gloss.L2Loss()
Train and Show results

Gb.semilogy function: loss to draw training and test data

From mxnet import gluonfrom mxnet.gluon import nndef fit_and_plot (Weight_decay): NET = nn. Sequential () Net.add (NN. Dense (1)) Net.initialize (init.    Normal (sigma=1) # L2 norm regularization of weight parameters, that is, weight attenuation. Trainer_w = gluon. Trainer (Net.collect_params ('. *weight '), ' sgd ', {' learning_rate ': learning_rate, ' WD ': Weight_decay}) # do not deviate parameters    Regularization of the L2 norm. Trainer_b = gluon.    Trainer (Net.collect_params ('. *bias '), ' sgd ', {' learning_rate ': learning_rate}) Train_ls = [] Test_ls = [] For _ in range (Num_epochs): For X, y in Train_iter:with Autograd.record (): L = loss (net            (X), y) L.backward () # Call the step function on the two Trainer instances, respectively.                             Trainer_w.step (batch_size) trainer_b.step (batch_size) train_ls.append (loss (NET (train_features),                            train_labels). mean (). Asscalar ()) Test_ls.append (loss (NET (test_features), test_labels). mean (). Asscalar ()) gb.semilogy (rangE (1, Num_epochs + 1), Train_ls, ' epochs ', ' loss ', range (1, Num_epochs + 1), Test_ls, [' Train ', ' Test ']) Return ' w[:10]: ', Net[0].weight.data () [:,: ten], ' B: ', Net[0].bias.data () print Fit_and_plot (5)
    • Using Gluon's WD Hyper-parameters, you can use weight attenuation to cope with overfitting problems.
    • We can define multiple Trainer instances to use different iterative methods for different model parameters.

MXNET: Weight attenuation-gluon implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.