在MXNet中進行超參數調優有多種方法,其中常用的包括Grid Search(網格搜索)、Random Search(隨機搜索)和Bayesian Optimization(貝葉斯優化)等。
from mxnet.gluon import nn
from mxnet.gluon.data.vision import transforms
from mxnet import autograd, gluon, init, nd
from mxnet.gluon import data as gdata, loss as gloss
from mxnet import metric as mtr
import mxnet as mx
import random
import time
import sys
grid_search = GridSearch({
'learning_rate': [0.01, 0.1, 0.5],
'momentum': [0.9, 0.95, 0.99],
'batch_size': [32, 64, 128]
})
for params in grid_search:
net = nn.Sequential()
net.add(nn.Dense(128, activation='relu'),
nn.Dense(64, activation='relu'),
nn.Dense(10))
net.initialize(init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': params['learning_rate'],
'momentum': params['momentum']})
train(net, train_iter, test_iter, batch_size=params['batch_size'],
trainer=trainer, num_epochs=num_epochs)
from mxnet.gluon.contrib.model_zoo import get_model
from mxnet.gluon.data import vision
from mxnet.gluon.data.vision import transforms
random_search = RandomSearch({
'learning_rate': (0.001, 0.1),
'momentum': (0.5, 0.99),
'batch_size': (32, 128)
})
for params in random_search:
net = get_model('resnet18_v1', classes=10)
net.initialize(init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': params['learning_rate'],
'momentum': params['momentum']})
train(net, train_iter, test_iter, batch_size=params['batch_size'],
trainer=trainer, num_epochs=num_epochs)
from bayes_opt import BayesianOptimization
def train_net(learning_rate, momentum, batch_size):
net = nn.Sequential()
net.add(nn.Dense(128, activation='relu'),
nn.Dense(64, activation='relu'),
nn.Dense(10))
net.initialize(init=init.Xavier())
trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': learning_rate, 'momentum': momentum})
train(net, train_iter, test_iter, batch_size=batch_size,
trainer=trainer, num_epochs=num_epochs)
return accuracy
optimizer = BayesianOptimization(
f=train_net,
pbounds={'learning_rate': (0.001, 0.1),
'momentum': (0.5, 0.99),
'batch_size': (32, 128)}
)
optimizer.maximize(init_points=5, n_iter=10)
best_params = optimizer.max['params']