def evaluate_accuracy(data_iterator, net, ctx, loss_fun, num_classes):
acc = mx.metric.Accuracy()<EOL>loss_avg = <NUM_LIT:0.><EOL>for i, ((data, length), label) in enumerate(data_iterator):<EOL><INDENT>data = data.as_in_context(ctx)  <EOL>length = length.astype('<STR_LIT>').as_in_context(ctx)<EOL>label = label.as_in_context(ctx)<EOL>output = net(data, length)<EOL>loss = loss_fun(output, label)<EOL>preds = []<EOL>if num_classes == <NUM_LIT:2>:<EOL><INDENT>preds = (nd.sign(output) + <NUM_LIT:1>) / <NUM_LIT:2><EOL>preds = preds.reshape(-<NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>preds = nd.argmax(output, axis=<NUM_LIT:1>)<EOL><DEDENT>acc.update(preds=preds, labels=label)<EOL>loss_avg = loss_avg * i / (i + <NUM_LIT:1>) + nd.mean(loss).asscalar() / (i + <NUM_LIT:1>)<EOL><DEDENT>return acc.get()[<NUM_LIT:1>], loss_avg<EOL>
This function is used for evaluating accuracy of
a given data iterator. (Either Train/Test data)
It takes in the loss function used too!