def train(): num_epochs, lr, batch_size = 5, 0.5, 256 loss = gloss.SoftmaxCrossEntropyLoss() train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)
def train_resnet_18(): net = residual_network() net.initialize(force_reinit=True, init=init.Xavier()) lr, batch_size, ctx, epoch_num = 0.05, 256, mxnet.cpu(), 5 trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size, resize=96) d2l.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, epoch_num)
def train_small_vgg(): ratio = 4 small_vgg_11 = [(pair[0], pair[1] // ratio) for pair in vgg_11] net = vgg(small_vgg_11) lr, num_epochs, batch_size, ctx = 0.01, 20, 256, mx.cpu() net.initialize() # mx.gluon 训练器中,定义需要更新的参数 net.collect_params(), 更新算法 sgd, 以及学习率 lr trainer = mx.gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': lr}) train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224) d2l.train_ch5(net, train_iter, test_iter, batch_size, trainer, ctx, num_epochs)
''' conv0 output shape: (1, 6, 24, 24) pool0 output shape: (1, 6, 12, 12) conv1 output shape: (1, 16, 8, 8) pool1 output shape: (1, 16, 4, 4) dense0 output shape: (1, 120) dense1 output shape: (1, 84) dense2 output shape: (1, 10) Process finished with exit code 0 ''' # 5.5.2 batch_size = 256 train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size=batch_size) def try_gpu(): try: ctx = mx.gpu() _ = nd.zeros((1, ), ctx=ctx) except mx.base.MXNetError: ctx = mx.cpu() return ctx ctx = try_gpu() def evaluate_accuracy(data_iter, net, ctx):
def train(): num_epochs, lr, batch_size = 5, 0.5, 256 loss = gloss.SoftmaxCrossEntropyLoss() train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size) d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)