<jupyter_start><jupyter_text>DNN Model using Pytorch<jupyter_code># Creating the dataloader for pytorch
def create_dataloader(df, batch_size):
    cls = dataset.MaliciousBenignData(df)
    return DataLoader(
        cls,
        batch_size = batch_size,
        num_workers = 0
    )

df_train_loader = create_dataloader(df_train, batch_size = config.BATCH_SIZE)
df_test_loader = create_dataloader(df_test, batch_size = 1) # Here for testing using the batch size as 1
# DataLoader components
df_train_loader.__dict__
# Transfer the model on the device -- 'GPU' if available or Default 'CPU'
model = model_dispatcher.dnn()
model.to(config.DEVICE)
print (model)
# Criterian and the Optimizer for the model
criterian = nn.BCEWithLogitsLoss()
optimizer = optim.Adam(model.parameters(), lr = config.LEARNING_RATE)
# Simple Binary Accuracy Function
def binary_acc(predictions, y_test):
    y_pred = torch.round(torch.sigmoid(predictions))
    correct = (y_pred == y_test).sum().float()
    acc = torch.round((correct/y_test.shape[0])*100)
    return acc
# Training function

def train_model(model, device, data_loader, optimizer, criterian):
    # Putting the model in training mode
    model.train()

    for epoch in range(1, config.EPOCHS+1):
        epoch_loss = 0
        epoch_acc = 0
        for X, y in data_loader:

            X = X.to(device)
            y_ = torch.tensor(y.unsqueeze(1), dtype = torch.float32)
            y = y_.to(device)

            # Zeroing the gradient
            optimizer.zero_grad()

            predictions = model(X.float())

            loss = criterian(predictions, y)
            acc = binary_acc(predictions, y)

            loss.backward() # Calculate Gradient
            optimizer.step() # Updating Weights

            epoch_loss += loss.item()
            epoch_acc += acc.item()

        print (f"Epoch -- {epoch} | Loss : {epoch_loss/len(data_loader): .5f} | Accuracy : {epoch_acc/len(data_loader): .5f}")
# Evaluation Function

def eval_model(model, device, data_loader):
    # Putting the model in evaluation mode
    model.eval()

    y_pred = []
    y_test_al = []

    with torch.no_grad():
        for X_test, y_test in data_loader:
            X_test = X_test.to(device)

            predictions = model(X_test.float())
            pred = torch.round(torch.sigmoid(predictions))

            y_test_al.append(y_test.tolist())
            y_pred.append(pred.tolist())

        # Changing the Predictions into list 
        y_test_al = [ele[0] for ele in y_test_al]
        y_pred = [int(ele[0][0]) for ele in y_pred] # the format of the prediction is [[[0]], [[1]]]

        return (y_test_al, y_pred)<jupyter_output><empty_output><jupyter_text>All Done, Training Time!!<jupyter_code># Training the Model
train_model(model, config.DEVICE, df_train_loader, optimizer, criterian)
# Evaluating the model and getting the predictions
y_test, preds = eval_model(model, config.DEVICE, df_test_loader)
print ('Predictions : ', preds[0:10])
# Evaluation Metrics
Metrics.metric_scores(y_test, preds)
torch.save(model.state_dict(), '../models/dnn.pth')<jupyter_output><empty_output><jupyter_text>Machine Learning Models<jupyter_code>X_train = df_train.drop(columns = ['label'])
y_train = df_train.label.values

X_test = df_test.drop(columns = ['label'])
y_test = df_test.label.values<jupyter_output><empty_output><jupyter_text>Logistic Regression<jupyter_code>clf = model_dispatcher.models['lr']

clf.fit(X_train, y_train)
preds = clf.predict(X_test)
# Evaluation Metrics
Metrics.metric_scores(y_test, preds)
joblib.dump(
            clf,
            os.path.join(config.MODEL_OUTPUT, "lr.bin")
        )<jupyter_output><empty_output><jupyter_text>XGBoost<jupyter_code>clf = model_dispatcher.models['xg']

clf.fit(X_train, y_train)
preds = clf.predict(X_test)
# Evaluation Metrics
Metrics.metric_scores(y_test, preds)
joblib.dump(
            clf,
            os.path.join(config.MODEL_OUTPUT, "xg.bin")
        )<jupyter_output><empty_output><jupyter_text>Decision Tree<jupyter_code>clf = model_dispatcher.models['dt']

clf.fit(X_train, y_train)
preds = clf.predict(X_test)
# Evaluation Metrics
Metrics.metric_scores(y_test, preds)
joblib.dump(
            clf,
            os.path.join(config.MODEL_OUTPUT, "dt.bin")
        )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11511627906976744, 'fraction_numerical': 0.005581395348837209, 'mean_word_length': 3.5513227513227514, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '334408', 'license': 'MIT', 'n_tokens_mistral': 1521, 'n_tokens_neox': 1482, 'n_words': 300, 'path': 'notebooks/Modelling.ipynb', 'repo_name': 'SumitM0432/Malicious-Webpage-Classifier', 'size': '4300'}