<jupyter_start><jupyter_text>Neural Network with SKORCH<jupyter_code>import torch.nn as nn
from skorch import NeuralNetClassifier
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from skorch.helper import DataFrameTransformer

class MLP(nn.Module):
    def __init__(self,num_inputs=12,num_units_d1=200,num_units_d2=100):
        super(MLP,self).__init__()
        self.dense0 = nn.Linear(num_inputs,num_units_d1)
        self.nonlin = nn.ReLU()
        self.dropout = nn.Dropout(0.5)
        self.dense1 = nn.Linear(num_units_d1,num_units_d2)
        self.output = nn.Linear(num_units_d2,2)
        self.softmax = nn.Softmax(dim=-1)
    def forward(self,X,**kwargs):
        X = self.nonlin(self.dense0(X))
        X = self.dropout(X)
        X = self.nonlin(self.dense1(X))
        X = self.softmax(self.output(X))
        return X

class customNLLLoss(nn.Module):
    criterion = nn.NLLLoss()
    def __init__(self):
        super().__init__()
    def forward(self,logits,target):
         return self.criterion(logits,target.long())

net = NeuralNetClassifier(
    module=MLP,
    criterion=customNLLLoss,
    max_epochs=30,
    lr=0.1,
    batch_size=32,
    train_split=None
)

nn_pipe = Pipeline([
    ('transform', DataFrameTransformer()),
    ('net', net)
])
from pycaret.classification import *

clf1 = setup(
    data=data,
    target=target,
    train_size=0.8,
    fold=5,
    session_id=123,
    log_experiment=True,
    experiment_name='electrical_grid_1',
    silent=True
)
rf_model = create_model("rf")
skorch_model = create_model(nn_pipe)
best_model = compare_models(include=[skorch_model,rf_model],sort='AUC')<jupyter_output><empty_output><jupyter_text>Tuning the Neural Network<jupyter_code>import torch.optim as optim

custom_grid = {
    'net__max_epochs': [20,30],
    'net__lr': [0.01,0.05,0.1],
    'net__module__num_units_d1': [50,100,150,200],
    'net__module__num_units_d2': [50,100,150,200],
    'net__optimizer': [optim.Adam,optim.SGD,optim.RMSprop]
}
tuned_skorch_model = tune_model(skorch_model,custom_grid=custom_grid)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12253327214318495, 'fraction_numerical': 0.03304268012849931, 'mean_word_length': 3.6881720430107525, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '15498', 'license': 'MIT', 'n_tokens_mistral': 865, 'n_tokens_neox': 807, 'n_words': 115, 'path': 'notebooks/bcheng004-skorch-pycaret.ipynb', 'repo_name': 'bcheng004/skorch-pycaret-example', 'size': '2179'}