<jupyter_start><jupyter_text>Introduce Modulewe can construct a module by specifying the following parameters:* symbol: the network definition* context* data_names: the list of input data variable names* label_names: the list of input label variable names<jupyter_code>mod = mx.mod.Module(symbol=net,
                   context=mx.cpu(),
                   data_names=['data'],
                   label_names=['softmax_label'])<jupyter_output><empty_output><jupyter_text>Internediate-level Interfacelet's see how to run training and inference using modules's intermediate level APIs.to train a module, we need to perform the following steps:* bind: prepares environment for the computation by allocating memory* init_params* init_initializer* metric.create* forward* update_metric* backward* update: updates parameters according to the installed optimizer and the gradients computed in the previous forward-backworad batch.<jupyter_code># allocate memory given the input data and label shapes
mod.bind(data_shapes=train_iter.provide_data, label_shapes=train_iter.provide_label)
# initialize parameters by uniform random numbers
mod.init_params(initializer=mx.init.Uniform(scale=.1))
# use SGD with learning rate 0.1 to train
mod.init_optimizer(optimizer='adam', optimizer_params=(('learning_rate', 0.1), ))
# use accuracy as the metric
metric = mx.metric.create('acc')
# train 5 epochs, i.e. going over the data iter one pass
for epoch in range(10):
    train_iter.reset()
    metric.reset()
    for batch in train_iter:
        mod.forward(batch, is_train=True)       # compute predictions
        mod.update_metric(metric, batch.label)  # accumulate prediction accuracy
        mod.backward()                          # compute gradients
        mod.update()                            # update parameters
    print('Epoch %d, Training %s' % (epoch, metric.get()))<jupyter_output>WARNING:root:Already bound, ignoring bind()
/home/poodar/Library/anaconda3/lib/python3.6/site-packages/ipykernel_launcher.py:4: UserWarning: Parameters already initialized and force_init=False. init_params call ignored.
  after removing the cwd from sys.path.
WARNING:root:optimizer already initialized, ignoring...<jupyter_text>High-level Interface<jupyter_code>train_iter.reset()

mod = mx.mod.Module(
    symbol=net, 
    context=mx.cpu(),
    data_names=['data'],
    label_names=['softmax_label']
)

mod.fit(
    train_iter,
    eval_data=val_iter,
    optimizer='sgd',
    optimizer_params={'learning_rate':0.1},
    eval_metric='acc',
    num_epoch=80
)
# Predict and Evaluate
y = mod.predict(val_iter)
assert y.shape == (4000, 26)
# If we need to evaluate on a test set, call score().
score = mod.score(val_iter, ['acc'])
print(score[0][1])
print(score)
print(type(score))
# save and load
model_prefix = 'mx_mlp'
ckpt = mx.callback.do_checkpoint(model_prefix)

mod = mx.mod.Module(symbol=net)
mod.fit(train_iter, num_epoch=50, epoch_end_callback=ckpt)
sym, arg_params, aux_params = mx.model.load_checkpoint(model_prefix, 3)
assert sym.tojson() == net.tojson()

mod.set_params(arg_params, aux_params)
# if we just want to resume training from a saved ckpt, instead of calling set_params, we can directly fall fit()
# passing the loaded parameters, so that fit() knows to start from  those parameters instead of initializing randomly
# we also set the begin_epoch parameters so that fit() knows we are resuming from a previously saved epoch
mod = mx.mod.Module(symbol=sym)
mod.fit(
    train_iter,
    num_epoch=21,
    arg_params=arg_params,
    aux_params=aux_params,
    begin_epoch=40
)
# assert score[0][1] > 0.77<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1001100110011001, 'fraction_numerical': 0.009900990099009901, 'mean_word_length': 4.527355623100304, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '586673', 'license': 'MIT', 'n_tokens_mistral': 1125, 'n_tokens_neox': 1098, 'n_words': 320, 'path': 'native/MXNet-ModuleAPI.ipynb', 'repo_name': 'poodarchu/gluon_step_by_step', 'size': '3636'}