DiCE icon indicating copy to clipboard operation
DiCE copied to clipboard

when using a trained pytorch model,the error:running_mean should contain xx elements not xx

Open soloccc opened this issue 4 years ago • 3 comments

I have already trained a pytorch model, the error occurs in the following sentence: exp = dice_ml.Dice(d, m)

the ERROR show as follows: RuntimeError: running_mean should contain 69 elements not 41 I have saw some explainations,saying that the solution is to align the output dimensions of BN layer and conv layer. But when I trained my model,it did not show this problem.My model trained successfully. the problem occurs when trying to use dice to generate a exp. So I don`t know how to solve it.

soloccc avatar Aug 30 '21 14:08 soloccc

@soloccc, can you share the notebook code that you are running?

Regards,

gaugup avatar Sep 01 '21 17:09 gaugup

@gaugup thx for the reply. Im new for DL,so maybe cant figure out the problem. codes are as follows: `

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import dice_ml
from dice_ml.utils import helpers  # helper functions
from torch.autograd import Variable
import torch.nn as nn
import torch
from sklearn.model_selection import train_test_split
from torch.autograd import Variable
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from torch.utils.data import Dataset, DataLoader

hidden_layer = 100
output_data = 4
epoch_n = 500
learning_rate = 1e-4
batch_size = 50

class tabularDataset(Dataset):
    def __init__(self, X, Y):
        self.x = X.values
        self.y = Y

    def __len__(self):
        return len(self.y)

    def __getitem__(self, idx):
        return (self.x[idx], self.y[idx])


class twolayer(nn.Module):
    def __init__(self, input_data, hidden_layer, output_data):
        super(twolayer, self).__init__()
        self.linear1 = nn.Linear(input_data, hidden_layer)
        self.ReLU = nn.ReLU()
        self.linear2 = nn.Linear(hidden_layer, output_data)

        self.bn_in = nn.BatchNorm1d(input_data)
        self.bn1 = nn.BatchNorm1d(hidden_layer)
        self.bn2 = nn.BatchNorm1d(output_data)

    def forward(self, x):
        x = self.bn_in(x)
        a1 = self.linear1(x)

        self.ReLU(a1)
        a1 = self.bn1(a1)

        y_pred = self.linear2(a1)
        y_pred = self.bn2(y_pred)
        return y_pred


def train(model, train_dl, optimizer, loss_fn):
    model.train()
    total_epochs = 5
    losses = []
    for epoch in range(total_epochs):
        for i, (x, y) in enumerate(train_dl):
            x-x.float().to(DEVICE)
            y = y.long().to(DEVICE)
            
            optimizer.zero_grad()
            outputs = model(x.to(torch.float32))
            
            loss = loss_fn(outputs, y)
            loss.backward()
            optimizer.step()
            losses.append(loss.cpu().data.item())
        print('Epoch : %d/%d,   Loss: %.4f' %
              (epoch+1, total_epochs, np.mean(losses)))

    
    model.eval()
    correct = 0
    total = 0
    for i, (x, y) in enumerate(train_dl):
        x = x.float().to(DEVICE)
        y = y.long()
        outputs = model(x).cpu()
        _, predicted = torch.max(outputs.data, 1)
        total += y.size(0)
        correct += (predicted == y).sum()
    print('auc: %.4f %%' % (100 * correct / total))


if __name__ == '__main__':
    dataset = pd.read_csv('./mykddcup.csv', names=['duration', 'protocol_type'	, 'service',	'flag',	'src_bytes',	'dst_bytes'	, 'land'	, 'wrong_fragment'	, 'urgent'	, 'hot',	'numfailedlogins'	, 'loggedin'	, 'numcompromised',	'rootshell',	'suattempted'	, 'numroot',	'numfilecreations',	'numshells'	, 'numaccessfiles'	, 'numoutboundcmds',	'ishotlogin'	, 'isguestlogin',	'count',	'srv_count',	'serror_rate',
                                                   'srv_serror_rate',	'rerror_rate'	, 'srv_rerror_rate',	'same_srv_rate'	, 'diff_srv_rate'	, 'srv_diff_host_rate',	'dst_host_count',	'dst_host_srv_count',	'dst_host_same_srv_rate',	'dst_host_diff_srv_rate',	'dst_host_same_src_port_rate',	'dst_host_srv_diff_host_rate',	'dst_host_serror_rate',	'dst_host_srv_serror_rate',	'dst_host_rerror_rate',	'dst_host_srv_rerror_rate'	, 'label'])

    y = dataset['label']

    print(torch.__version__)
    for each in y:
        if each == 1:
            y[each] = 1
        else:
            y[each] = 0
    y_label = LabelEncoder()
    y = y_label.fit_transform(y)
    print(y)
    input_data = dataset.drop('label', axis=1).shape[1]

    train_dataset, test_dataset, y_train, y_test = train_test_split(dataset,
                                                                    y,
                                                                    test_size=0.2,
                                                                    random_state=0,
                                                                    stratify=y)

    train_ds = tabularDataset(train_dataset.drop('label', axis=1), y_train)
    test_ds = tabularDataset(test_dataset, y_test)
    d = dice_ml.Data(dataframe=train_dataset, continuous_features=['duration', 'src_bytes', 'dst_bytes', 'wrong_fragment', 'urgent', 'hot', 'numfailedlogins', 'numcompromised', 'numroot', 'numfilecreations', 'numshells', 'numaccessfiles', 'numoutboundcmds', 'count', 'srv_count', 'serror_rate', 'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate',
                                                                   'same_srv_rate', 'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count', 'dst_host_srv_count', 'dst_host_same_srv_rate', 'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate', 'dst_host_srv_diff_host_rate', 'dst_host_serror_rate', 'dst_host_srv_serror_rate', 'dst_host_rerror_rate', 'dst_host_srv_rerror_rate'], outcome_name='label')
    DEVICE = torch.device("cpu")
    if torch.cuda.is_available():
        DEVICE = torch.device("cuda")

    model = twolayer(input_data, hidden_layer, output_data).to(DEVICE)
    loss_fn = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(
        model.parameters(), lr=learning_rate)

    train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
    train(model, train_dl, optimizer, loss_fn)
    torch.save(model, './model.tar')

    #!DICE

    backend = 'PYT'
    m = dice_ml.Model(model_path='./model.tar', backend=backend)

    exp = dice_ml.Dice(d, m, method="random")
    query_instance = {'duration': 0,
                      'dst_bytes': 21,
                      'wrong_fragment': 5,
                      'src_bytes': 0,
                      }
    dice_exp = exp.generate_counterfactuals(
        query_instance, total_CFs=4, desired_class="opposite")
    dice_exp.visualize_as_dataframe(show_only_changes=True)

`

soloccc avatar Sep 06 '21 02:09 soloccc

@gaugup thx for the reply. Im new for DL,so maybe cant figure out the problem. codes are as follows: `

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
import dice_ml
from dice_ml.utils import helpers  # helper functions
from torch.autograd import Variable
import torch.nn as nn
import torch
from sklearn.model_selection import train_test_split
from torch.autograd import Variable
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt
from sklearn.preprocessing import LabelEncoder
from torch.utils.data import Dataset, DataLoader

hidden_layer = 100
output_data = 4
epoch_n = 500
learning_rate = 1e-4
batch_size = 50

class tabularDataset(Dataset):
    def __init__(self, X, Y):
        self.x = X.values
        self.y = Y

    def __len__(self):
        return len(self.y)

    def __getitem__(self, idx):
        return (self.x[idx], self.y[idx])


class twolayer(nn.Module):
    def __init__(self, input_data, hidden_layer, output_data):
        super(twolayer, self).__init__()
        self.linear1 = nn.Linear(input_data, hidden_layer)
        self.ReLU = nn.ReLU()
        self.linear2 = nn.Linear(hidden_layer, output_data)

        self.bn_in = nn.BatchNorm1d(input_data)
        self.bn1 = nn.BatchNorm1d(hidden_layer)
        self.bn2 = nn.BatchNorm1d(output_data)

    def forward(self, x):
        x = self.bn_in(x)
        a1 = self.linear1(x)

        self.ReLU(a1)
        a1 = self.bn1(a1)

        y_pred = self.linear2(a1)
        y_pred = self.bn2(y_pred)
        return y_pred


def train(model, train_dl, optimizer, loss_fn):
    model.train()
    total_epochs = 5
    losses = []
    for epoch in range(total_epochs):
        for i, (x, y) in enumerate(train_dl):
            x-x.float().to(DEVICE)
            y = y.long().to(DEVICE)
            
            optimizer.zero_grad()
            outputs = model(x.to(torch.float32))
            
            loss = loss_fn(outputs, y)
            loss.backward()
            optimizer.step()
            losses.append(loss.cpu().data.item())
        print('Epoch : %d/%d,   Loss: %.4f' %
              (epoch+1, total_epochs, np.mean(losses)))

    
    model.eval()
    correct = 0
    total = 0
    for i, (x, y) in enumerate(train_dl):
        x = x.float().to(DEVICE)
        y = y.long()
        outputs = model(x).cpu()
        _, predicted = torch.max(outputs.data, 1)
        total += y.size(0)
        correct += (predicted == y).sum()
    print('auc: %.4f %%' % (100 * correct / total))


if __name__ == '__main__':
    dataset = pd.read_csv('./mykddcup.csv', names=['duration', 'protocol_type'	, 'service',	'flag',	'src_bytes',	'dst_bytes'	, 'land'	, 'wrong_fragment'	, 'urgent'	, 'hot',	'numfailedlogins'	, 'loggedin'	, 'numcompromised',	'rootshell',	'suattempted'	, 'numroot',	'numfilecreations',	'numshells'	, 'numaccessfiles'	, 'numoutboundcmds',	'ishotlogin'	, 'isguestlogin',	'count',	'srv_count',	'serror_rate',
                                                   'srv_serror_rate',	'rerror_rate'	, 'srv_rerror_rate',	'same_srv_rate'	, 'diff_srv_rate'	, 'srv_diff_host_rate',	'dst_host_count',	'dst_host_srv_count',	'dst_host_same_srv_rate',	'dst_host_diff_srv_rate',	'dst_host_same_src_port_rate',	'dst_host_srv_diff_host_rate',	'dst_host_serror_rate',	'dst_host_srv_serror_rate',	'dst_host_rerror_rate',	'dst_host_srv_rerror_rate'	, 'label'])

    y = dataset['label']

    print(torch.__version__)
    for each in y:
        if each == 1:
            y[each] = 1
        else:
            y[each] = 0
    y_label = LabelEncoder()
    y = y_label.fit_transform(y)
    print(y)
    input_data = dataset.drop('label', axis=1).shape[1]

    train_dataset, test_dataset, y_train, y_test = train_test_split(dataset,
                                                                    y,
                                                                    test_size=0.2,
                                                                    random_state=0,
                                                                    stratify=y)

    train_ds = tabularDataset(train_dataset.drop('label', axis=1), y_train)
    test_ds = tabularDataset(test_dataset, y_test)
    d = dice_ml.Data(dataframe=train_dataset, continuous_features=['duration', 'src_bytes', 'dst_bytes', 'wrong_fragment', 'urgent', 'hot', 'numfailedlogins', 'numcompromised', 'numroot', 'numfilecreations', 'numshells', 'numaccessfiles', 'numoutboundcmds', 'count', 'srv_count', 'serror_rate', 'srv_serror_rate', 'rerror_rate', 'srv_rerror_rate',
                                                                   'same_srv_rate', 'diff_srv_rate', 'srv_diff_host_rate', 'dst_host_count', 'dst_host_srv_count', 'dst_host_same_srv_rate', 'dst_host_diff_srv_rate', 'dst_host_same_src_port_rate', 'dst_host_srv_diff_host_rate', 'dst_host_serror_rate', 'dst_host_srv_serror_rate', 'dst_host_rerror_rate', 'dst_host_srv_rerror_rate'], outcome_name='label')
    DEVICE = torch.device("cpu")
    if torch.cuda.is_available():
        DEVICE = torch.device("cuda")

    model = twolayer(input_data, hidden_layer, output_data).to(DEVICE)
    loss_fn = nn.CrossEntropyLoss()
    optimizer = torch.optim.Adam(
        model.parameters(), lr=learning_rate)

    train_dl = DataLoader(train_ds, batch_size=batch_size, shuffle=True)
    train(model, train_dl, optimizer, loss_fn)
    torch.save(model, './model.tar')

    #!DICE

    backend = 'PYT'
    m = dice_ml.Model(model_path='./model.tar', backend=backend)

    exp = dice_ml.Dice(d, m, method="random")
    query_instance = {'duration': 0,
                      'dst_bytes': 21,
                      'wrong_fragment': 5,
                      'src_bytes': 0,
                      }
    dice_exp = exp.generate_counterfactuals(
        query_instance, total_CFs=4, desired_class="opposite")
    dice_exp.visualize_as_dataframe(show_only_changes=True)

`

soloccc avatar Sep 06 '21 02:09 soloccc