Kaggle competition: IEEE-CIS Fraud Detection
BATCH = 32
EPOCHS = 100
LR = 0.001
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 device = DEVICE<load_from_csv>
sub_path = ".. /input/ieeesubmissions5"
all_files = os.listdir(sub_path)
 all_files
all_files.remove('blend01.csv')
 all_files.remove('blend02.csv')
 all_files
all_df = pd.read_csv("/kaggle/input/digit-recognizer/train.csv", dtype=np.float32)
 test_df = pd.read_csv("/kaggle/input/digit-recognizer/test.csv", dtype=np.float32)
 sample_sub = pd.read_csv("/kaggle/input/digit-recognizer/sample_submission.csv")
<split>
concat_sub['ieee_max'] = concat_sub.iloc[:, 1:ncol].max(axis=1)
 concat_sub['ieee_min'] = concat_sub.iloc[:, 1:ncol].min(axis=1)
 concat_sub['ieee_mean'] = concat_sub.iloc[:, 1:ncol].mean(axis=1)
 concat_sub['ieee_median'] = concat_sub.iloc[:, 1:ncol].median(axis=1 )
train_df, validate_df = train_test_split(all_df, train_size=0.8, test_size=0.2 )<categorify>
cutoff_lo = 0.8
cutoff_hi = 0.2
original_transforms = transforms.Compose([
transforms.ToPILImage() ,
transforms.ToTensor()
])
 sub = [original_transforms]
train_transforms = transforms.Compose([
transforms.ToPILImage() ,
transforms.RandomAffine(degrees=25, translate=(0.1, 0.1), scale=(0.8, 1.2)) ,
transforms.ToTensor()
] )<categorify>
concat_sub['isFraud'] = concat_sub['ieee_mean']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_mean.csv',
index=False, float_format='%.6f' )
class GetData(Dataset):
def __init__(self, X_Train, Y_Train, Transform):
self.X = X_Train
self.transform = Transform
self.Y = Y_Train
def __len__(self):
return len(self.X)
 def __getitem__(self, index):
return self.transform(self.X[index] ).contiguous() , self.Y[index]<prepare_x_and_y>
concat_sub['isFraud'] = concat_sub['ieee_median']
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_median.csv',
index=False, float_format='%.6f' )
def getSet(data_df, Transform):
data_df.head()
Y = data_df.label.values
X0 = data_df.loc[:, data_df.columns != 'label'].values/255
Y = torch.from_numpy(Y ).type(torch.LongTensor)
 X = X0.reshape(( -1, 1, 28, 28))
X = torch.from_numpy(X)
 return GetData(X, Y, Transform )<load_pretrained>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1), 1,
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
0, concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_pushout_median.csv',
index=False, float_format='%.6f' )
trainset = getSet(train_df, train_transforms)
 validateset = getSet(validate_df, original_transforms)
 dataloaders = { 'train': DataLoader(trainset, batch_size=BATCH, shuffle=True, num_workers=4),
'val': DataLoader(validateset, batch_size=BATCH, shuffle=True, num_workers=4)} 
<define_variables>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_mean']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_mean.csv',
index=False, float_format='%.6f' )
dataset_sizes = { 'train': len(trainset), 'val': len(validateset)}
dataset_sizes<choose_model_class>
concat_sub['isFraud'] = np.where(np.all(concat_sub.iloc[:,1:ncol] > cutoff_lo, axis=1),
concat_sub['ieee_max'],
np.where(np.all(concat_sub.iloc[:,1:ncol] < cutoff_hi, axis=1),
concat_sub['ieee_min'],
concat_sub['ieee_median']))
concat_sub[['TransactionID', 'isFraud']].to_csv('stack_minmax_median.csv',
index=False, float_format='%.6f' )
class VGG(nn.Module):
def __init__(
 self,
features: nn.Module,
num_classes: int = 1000,
init_weights: bool = True 
)-> None:
super(VGG, self ).__init__()
self.features = features
self.avgpool = nn.AdaptiveAvgPool2d(( 1, 1))
self.classifier = nn.Sequential(
 nn.Linear(256, num_classes), 
)
 if init_weights:
self._initialize_weights()
def forward(self, x: torch.Tensor)-> torch.Tensor:
x = self.features(x)
 x = self.avgpool(x)
 x = torch.flatten(x, 1)
 x = self.classifier(x)
 return x
def _initialize_weights(self)-> None:
for m in self.modules() :
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
 if m.bias is not None:
nn.init.constant_(m.bias, 0)
 elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
 nn.init.constant_(m.bias, 0)
 elif isinstance(m, nn.Linear):
nn.init.normal_(m.weight, 0, 0.01)
 nn.init.constant_(m.bias, 0)
 cfgs: Dict[str, List[Union[str, int]]] = {
'A': [64, 'M', 128, 128, 'M', 256, 256, 256, 'M'],
'B': [16, 'M', 256, 'M'],
'firstPadding': 2
}
def make_layers(cfg: List[Union[str, int]], batch_norm: bool = False)-> nn.Sequential:
layers: List[nn.Module] = []
in_channels = 1
in_padding = 5
i = 0
for v in cfg:
if v == 'M':
layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
else:
v = cast(int, v)
 in_padding = 1
if i == 5:
in_padding = 2
conv2d = nn.Conv2d(in_channels, v, kernel_size=3, padding=in_padding)
 if batch_norm:
layers += [conv2d, nn.BatchNorm2d(v), nn.ReLU(inplace=True)]
else:
layers += [conv2d, nn.ReLU(inplace=True)]
in_channels = v
i = i + 1
return nn.Sequential(*layers)
 def _vgg(arch: str, cfg: str, batch_norm: bool,num_classes: int, **kwargs: Any)-> VGG:
model = VGG(make_layers(cfgs[cfg], batch_norm=batch_norm), num_classes, **kwargs)
 return model
def selfDefineVgg(num_classes: int, **kwargs: Any)-> VGG:
return _vgg('vgg', 'A', True, num_classes, **kwargs)
 model = selfDefineVgg(10)
 model = model.to(DEVICE )<choose_model_class>
concat_sub['isFraud'] = concat_sub_rank.median(axis=1)
 concat_sub['isFraud'] =(concat_sub['isFraud']-concat_sub['isFraud'].min())/(concat_sub['isFraud'].max() - concat_sub['isFraud'].min())
 concat_sub[['TransactionID', 'isFraud']].to_csv('stack_median_rank.csv', index=False, float_format='%.8f' )
criterion = nn.CrossEntropyLoss() 
<choose_model_class>
concat_sub['isFraud'] = concat_sub_rank.mean(axis=1)
 concat_sub['isFraud'] =(concat_sub['isFraud']-concat_sub['isFraud'].min())/(concat_sub['isFraud'].max() - concat_sub['isFraud'].min())
 concat_sub[['TransactionID', 'isFraud']].to_csv('stack_mean_rank.csv', index=False, float_format='%.8f' )