<jupyter_start><jupyter_text>MXNET Implementation<jupyter_code>class MxModel(gluon.HybridBlock):
    def __init__(self, **kwargs):
        super(MxModel, self).__init__(**kwargs)
        with self.name_scope():
            self.embed = gluon.nn.Embedding(input_dim=num_words + 1, output_dim=EMBED_DIM)
            
            self.conv1 = gluon.nn.Conv1D(channels=128, kernel_size=5)
            self.conv2 = gluon.nn.Conv1D(channels=128, kernel_size=5)
            self.conv3 = gluon.nn.Conv1D(channels=128, kernel_size=5)
            
            self.bnorm1 = gluon.nn.BatchNorm()
            self.bnorm2 = gluon.nn.BatchNorm()
            self.bnorm3 = gluon.nn.BatchNorm()
            
            self.fc1 = gluon.nn.Dense(units=128)
            self.fc2 = gluon.nn.Dense(units=2)
            
            self.dropout = gluon.nn.Dropout(rate=0.25)
    def hybrid_forward(self, F, x, *args, **kwargs):
        x = self.embed(x)
        x = F.relu(self.bnorm1(self.conv1(x)))
        x = F.relu(self.bnorm2(self.conv2(x)))
        x = F.relu(self.bnorm3(self.conv3(x)))
        x = F.relu(self.dropout(self.fc1(x)))
        x = self.dropout(self.fc2(x))
        return x
            
mx_model = MxModel()
mx_model.collect_params().initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss(sparse_label=False)
trainer = gluon.Trainer(mx_model.collect_params(), 'adam', {'learning_rate': 0.001})
acc = mx.metric.Accuracy()
train_data = mx.io.NDArrayIter(data=x_train, label=y_train, batch_size=128, shuffle=True)
test_data = mx.io.NDArrayIter(data=x_test, label=y_test, batch_size=128, shuffle=False)
def evaluate_accuracy(data_iterator, net):
    data_iterator.reset()
    acc_test = mx.metric.Accuracy()
    for batch in data_iterator:
        data = batch.data[0].as_in_context(ctx)
        label = batch.label[0].as_in_context(ctx)
        output = net(data)
        acc_test.update(preds=output, labels=label)
    return acc_test.get()[1]
epochs = 2
smoothing_constant = .01
mx_model.hybridize()

start = time.time()

for e in range(epochs):
    print(f"Epoch {e+1}--------------")
    i = 0
    train_data.reset()
    for batch in train_data:
        data = batch.data[0].as_in_context(ctx)
        label = batch.label[0].as_in_context(ctx)
        with autograd.record():
            output = mx_model(data)
            loss = softmax_cross_entropy(output, label)
        loss.backward()
        trainer.step(data.shape[0])

        ##########################
        #  Keep a moving average of the losses
        ##########################
        curr_loss = mx.nd.mean(loss).asscalar()
        acc.update(preds=output, labels=label)
        if i % 50 == 0:
            print(f"loss: {curr_loss}    acc:{acc.get()[1]}")
        i += 1
    print(f"val acc: {evaluate_accuracy(test_data, mx_model)}")
    print(time_since(start))
    acc.reset()
    
print(time_since(start))
mx_model.save_params("data/mx_model")<jupyter_output>Epoch 1--------------
loss: 0.7064136266708374    acc:0.5
loss: 0.6858754754066467    acc:0.5
loss: 0.6862083077430725    acc:0.5000386757425742
loss: 0.6491576433181763    acc:0.5009054221854304
val acc: 0.5065104166666666
14m 19s
Epoch 2--------------
loss: 0.6357454061508179    acc:0.50390625
loss: 0.6397137641906738    acc:0.5003063725490197
loss: 0.6163472533226013    acc:0.49176206683168316
loss: 0.5043906569480896    acc:0.47775248344370863
val acc: nan
27m 20s
27m 20s
{'fraction_non_alphanumeric': 0.1522740356937248, 'fraction_numerical': 0.09643062751871043, 'mean_word_length': 2.852549889135255, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '446946', 'license': 'MIT', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1272, 'n_words': 198, 'path': 'spam_email.ipynb', 'repo_name': 'srijita1297/Spam-detection', 'size': '3474'}