Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<set_options>
for dirname, _, filenames in os.walk('/kaggle/input'):
for filename in filenames:
print(os.path.join(dirname, filename))
%reset -f
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
print(PIL.PILLOW_VERSION)
 train_on_gpu = torch.cuda.is_available()
if not train_on_gpu:
print('CUDA is not available.Training on CPU...')
 else:
print('CUDA is available! Training on GPU...')
 device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
 print(device)
 ImageFile.LOAD_TRUNCATED_IMAGES = True
ImageFile.LOAD_TRUNCATED_IMAGES = True<define_variables>
train=pd.read_csv('/kaggle/input/titanic/train.csv')
 test=pd.read_csv('/kaggle/input/titanic/test.csv')
 dataset = pd.concat(objs=[train, test], axis=0,sort=False ).reset_index(drop=True )
! ls -la.. /input/
data_dir = '.. /input/aptos2019-blindness-detection/'
train_dir = data_dir + '/train_images/'
test_dir= data_dir + '/test_images/'
nThreads = 4
batch_size = 32
use_gpu = torch.cuda.is_available()<categorify>
dataset['Ticket_Frequency'] = dataset.groupby('Ticket')['Ticket'].transform('count')
 dataset['Cabin_n'] = dataset['Cabin'].str[0]
dataset['Cabin_n'].fillna('M',inplace=True)
 dataset['Deck'] = dataset['Cabin'].apply(lambda s: s[0] if pd.notnull(s)else 'M')
 dataset['Deck'] = dataset['Deck'].replace(['A', 'B', 'C'], 'ABC')
 dataset['Deck'] = dataset['Deck'].replace(['D', 'E'], 'DE')
 dataset['Deck'] = dataset['Deck'].replace(['F', 'G'], 'FG')
 dataset['Deck'] = dataset['Deck'].replace(['T'], 'A')
 dataset.drop(labels = ["Cabin",'Ticket'], axis = 1, inplace = True)
 dataset["TFamily"] = dataset["Parch"] + dataset["SibSp"] + 1
dataset.loc[dataset['TFamily']==1 ,'IsAlone']=1
dataset.loc[dataset['TFamily']!=1 ,'IsAlone']=0
dataset['IsAlone']= dataset['IsAlone'].astype(int)
 dataset["Embarked"] = dataset["Embarked"].fillna("S")
 dataset['Age'] = dataset.groupby(["Sex", "Pclass"])["Age"].apply(lambda x: x.fillna(x.median()))
med_fare = dataset.groupby(['Pclass', 'Parch', 'SibSp'] ).Fare.median() [3][0][0]
dataset['Fare'] = dataset['Fare'].fillna(med_fare)
 dataset['Age_bin']=pd.cut(dataset['Age'], bins=10)
 dataset['TFamily_bin']=pd.cut(dataset['TFamily'], bins=2)
 dataset['Fare_bin']=pd.cut(dataset['Fare'], bins=13)
 dataset['Sex']=dataset['Sex'].map({'male':1,'female':0})
 dataset = pd.get_dummies(dataset, columns=['Embarked','Age_bin','Cabin_n','Fare_bin','TFamily_bin','Deck'])
 dataset.columns = dataset.columns.str.replace(',','')
 dataset.columns = dataset.columns.str.replace(']','')
 dataset_title = [i.split(",")[1].split(".")[0].strip() for i in dataset["Name"]]
Ticket = []
for i in list(train.Ticket):
if not i.isdigit() :
Ticket.append(i.replace(".","" ).replace("/","" ).strip().split(' ')[0])
 else:
Ticket.append("X")
 train["Ticket"] = Ticket
dataset.drop(labels = ['Name'], axis = 1, inplace = True)
 train_n=dataset[dataset['Survived'].notnull() ]
x_train=train_n.drop(labels=['Survived','PassengerId'],axis=1)
 y_train=train_n['Survived'].astype(int)
 X_train,X_test,Y_train,Y_test = train_test_split(x_train,y_train,test_size=0.25, random_state=0)
class GenericDataset() :
def __init__(self, labels, root_dir, subset=False, transform=None):
self.labels = labels
self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
img_name = self.labels.iloc[idx, 0]
fullname = join(self.root_dir, img_name)
 image = Image.open(fullname ).convert('RGB')
 labels = self.labels.iloc[idx, 2]
if self.transform:
image = self.transform(image)
 return image, labels
@staticmethod
def find_classes(fullDir):
classes = [d for d in os.listdir(fullDir)if os.path.isdir(os.path.join(fullDir, d)) ]
classes.sort()
class_to_idx = {classes[i]: i for i in range(len(classes)) }
idx_to_class = {v: k for k, v in class_to_idx.items() }
train = []
for index, label in enumerate(classes):
path = fullDir + label + '/'
for file in listdir(path):
train.append(['{}/{}'.format(label, file), label, index])
 df = pd.DataFrame(train, columns=['file', 'category', 'category_id', ])
 return classes, class_to_idx, idx_to_class, df
@staticmethod
def find_classes_retino(fullDir):
df_labels = pd.read_csv(".. /input/aptos2019-blindness-detection/train.csv", sep=',')
 class_to_idx = {'0': 0, '1': 1, '2': 2, '3': 3, '4': 4}
idx_to_class = dict(zip(class_to_idx.values() , class_to_idx.keys()))
classes = ['0', '1', '2', '3', '4']
print('Sorted Classes: {}'.format(classes))
print('class_to_idx: {}'.format(class_to_idx))
print('num_to_class: {}'.format(idx_to_class))
train = []
for index, row in(df_labels.iterrows()):
id =(row['id_code'])
 currImage_on_disk = os.path.join(fullDir, id + '.png')
 if os.path.isfile(currImage_on_disk):
if(int(row['diagnosis'])) == 0:
trait = '0'
elif(int(row['diagnosis'])) == 1:
trait = '1'
elif(int(row['diagnosis'])) == 2:
trait = '2'
elif(int(row['diagnosis'])) == 3:
trait = '3'
elif(int(row['diagnosis'])) == 4:
trait = '4'
train.append(['{}'.format(currImage_on_disk), trait, class_to_idx[trait]])
 df = pd.DataFrame(train, columns=['file', 'category', 'category_id', ])
 if os.path.isfile('full_retino_labels.csv'):
os.remove('full_retino_labels.csv')
 df.to_csv('full_retino_labels.csv', index=None)
 return classes, class_to_idx, idx_to_class, df 
<categorify>
kfold = StratifiedKFold(n_splits=10 )
class GenericDatasetTTA() :
def __init__(self, labels, root_dir, subset=False, transform=None,TTA=8):
self.labels = labels
self.root_dir = root_dir
self.transform = transform
self.TTA = TTA
def __len__(self):
return len(self.labels)
 def __getitem__(self, idx):
img_name = self.labels.iloc[idx, 0]
fullname = join(self.root_dir, img_name)
 image = Image.open(fullname ).convert('RGB')
 labels = self.labels.iloc[idx, 2]
a = []
for _ in range(self.TTA):
image1 = self.transform(image)
 a.append(image1)
 image = torch.stack(a)
 return image, labels<define_variables>
random_state = 2
classifiers = []
classifiers.append(SVC(random_state=random_state))
classifiers.append(DecisionTreeClassifier(random_state=random_state))
classifiers.append(AdaBoostClassifier(DecisionTreeClassifier(random_state=random_state),random_state=random_state,learning_rate=0.1))
classifiers.append(RandomForestClassifier(random_state=random_state))
classifiers.append(ExtraTreesClassifier(random_state=random_state))
classifiers.append(GradientBoostingClassifier(random_state=random_state))
classifiers.append(MLPClassifier(random_state=random_state))
classifiers.append(KNeighborsClassifier())
 classifiers.append(LogisticRegression(random_state = random_state))
classifiers.append(LinearDiscriminantAnalysis())
 classifiers.append(XGBClassifier())
 classifiers.append(LGBMClassifier())
 cv_results = []
for classifier in classifiers :
cv_results.append(cross_val_score(classifier, X_train, y = Y_train, scoring = "accuracy", cv = kfold, n_jobs=4))
cv_means = []
cv_std = []
for cv_result in cv_results:
cv_means.append(cv_result.mean())
 cv_std.append(cv_result.std())
 cv_res = pd.DataFrame({"CrossValMeans":cv_means,"CrossValerrors": cv_std,"Algorithm":["SVC","DecisionTree","AdaBoost",
"RandomForest","ExtraTrees","GradientBoosting","MultipleLayerPerceptron","KNeighboors","LogisticRegression","LinearDiscriminantAnalysis",'XGBClassifier','LGBMClassifier']})
 plt.figure(figsize=(20,10))
g = sns.barplot("CrossValMeans","Algorithm",data = cv_res, palette="Set3",orient = "h",**{'xerr':cv_std})
 plt.axvline(0.81)
 plt.axvline(0.84)
 g.set_xlabel("Mean Accuracy")
 g = g.set_title("Cross validation scores" )
__all__ = ['SENet', 'senet154', 'se_resnet50', 'se_resnet101', 'se_resnet152',
'se_resnext50_32x4d', 'se_resnext101_32x4d']
pretrained_settings = {
'senet154': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/senet154-c7b49a05.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet50': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet50-ce0d4300.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet101': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet101-7e38fcc6.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnet152': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnet152-d17c99b7.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext50_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext50_32x4d-a260b3a4.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
'se_resnext101_32x4d': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/se_resnext101_32x4d-3b2fe3d8.pth',
'input_space': 'RGB',
'input_size': [3, 224, 224],
'input_range': [0, 1],
'mean': [0.485, 0.456, 0.406],
'std': [0.229, 0.224, 0.225],
'num_classes': 1000
}
},
}
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self ).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
 self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1,
padding=0)
 self.relu = nn.ReLU(inplace=True)
 self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1,
padding=0)
 self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
 x = self.fc1(x)
 x = self.relu(x)
 x = self.fc2(x)
 x = self.sigmoid(x)
 return module_input * x
class Bottleneck(nn.Module):

 def forward(self, x):
residual = x
out = self.conv1(x)
 out = self.bn1(out)
 out = self.relu(out)
 out = self.conv2(out)
 out = self.bn2(out)
 out = self.relu(out)
 out = self.conv3(out)
 out = self.bn3(out)
 if self.downsample is not None:
residual = self.downsample(x)
 out = self.se_module(out)+ residual
out = self.relu(out)
 return out
class SEBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes * 2, kernel_size=1, bias=False)
 self.bn1 = nn.BatchNorm2d(planes * 2)
 self.conv2 = nn.Conv2d(planes * 2, planes * 4, kernel_size=3,
stride=stride, padding=1, groups=groups,
bias=False)
 self.bn2 = nn.BatchNorm2d(planes * 4)
 self.conv3 = nn.Conv2d(planes * 4, planes * 4, kernel_size=1,
bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNetBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None):
super(SEResNetBottleneck, self ).__init__()
self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False,
stride=stride)
 self.bn1 = nn.BatchNorm2d(planes)
 self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1,
groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(planes)
 self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SEResNeXtBottleneck(Bottleneck):

 expansion = 4
def __init__(self, inplanes, planes, groups, reduction, stride=1,
downsample=None, base_width=4):
super(SEResNeXtBottleneck, self ).__init__()
width = math.floor(planes *(base_width / 64)) * groups
self.conv1 = nn.Conv2d(inplanes, width, kernel_size=1, bias=False,
stride=1)
 self.bn1 = nn.BatchNorm2d(width)
 self.conv2 = nn.Conv2d(width, width, kernel_size=3, stride=stride,
padding=1, groups=groups, bias=False)
 self.bn2 = nn.BatchNorm2d(width)
 self.conv3 = nn.Conv2d(width, planes * 4, kernel_size=1, bias=False)
 self.bn3 = nn.BatchNorm2d(planes * 4)
 self.relu = nn.ReLU(inplace=True)
 self.se_module = SEModule(planes * 4, reduction=reduction)
 self.downsample = downsample
self.stride = stride
class SENet(nn.Module):
def __init__(self, block, layers, groups, reduction, dropout_p=0.2,
inplanes=128, input_3x3=True, downsample_kernel_size=3,
downsample_padding=1, num_classes=1000):

 super(SENet, self ).__init__()
self.inplanes = inplanes
if input_3x3:
layer0_modules = [ 
('conv1', nn.Conv2d(3, 64, 3, stride=2, padding=1,
bias=False)) , 
('bn1', nn.BatchNorm2d(64)) , 
('relu1', nn.ReLU(inplace=True)) , 
('conv2', nn.Conv2d(64, 64, 3, stride=1, padding=1,
bias=False)) , 
('bn2', nn.BatchNorm2d(64)) , 
('relu2', nn.ReLU(inplace=True)) , 
('conv3', nn.Conv2d(64, inplanes, 3, stride=1, padding=1,
bias=False)) , 
('bn3', nn.BatchNorm2d(inplanes)) , 
('relu3', nn.ReLU(inplace=True)) ,
]
else:
layer0_modules = [ 
('conv1', nn.Conv2d(3, inplanes, kernel_size=7, stride=2,
padding=3, bias=False)) , 
('bn1', nn.BatchNorm2d(inplanes)) , 
('relu1', nn.ReLU(inplace=True)) ,
]
layer0_modules.append(( 'pool', nn.MaxPool2d(3, stride=2,
ceil_mode=True)))
 self.layer0 = nn.Sequential(OrderedDict(layer0_modules))
self.layer1 = self._make_layer(
 block,
planes=64,
blocks=layers[0],
groups=groups,
reduction=reduction,
downsample_kernel_size=1,
downsample_padding=0 
)
 self.layer2 = self._make_layer(
 block,
planes=128,
blocks=layers[1],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer3 = self._make_layer(
 block,
planes=256,
blocks=layers[2],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.layer4 = self._make_layer(
 block,
planes=512,
blocks=layers[3],
stride=2,
groups=groups,
reduction=reduction,
downsample_kernel_size=downsample_kernel_size,
downsample_padding=downsample_padding 
)
 self.avg_pool = nn.AvgPool2d(7, stride=1)
 self.dropout = nn.Dropout(dropout_p)if dropout_p is not None else None
self.last_linear = nn.Linear(512 * block.expansion, num_classes)
 def _make_layer(self, block, planes, blocks, groups, reduction, stride=1,
downsample_kernel_size=1, downsample_padding=0):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
 nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=downsample_kernel_size, stride=stride,
padding=downsample_padding, bias=False),
nn.BatchNorm2d(planes * block.expansion), 
)
 layers = []
layers.append(block(self.inplanes, planes, groups, reduction, stride,
downsample))
self.inplanes = planes * block.expansion
for i in range(1, blocks):
layers.append(block(self.inplanes, planes, groups, reduction))
return nn.Sequential(*layers)
 def features(self, x):
x = self.layer0(x)
 x = self.layer1(x)
 x = self.layer2(x)
 x = self.layer3(x)
 x = self.layer4(x)
 return x
def logits(self, x):
x = self.avg_pool(x)
 if self.dropout is not None:
x = self.dropout(x)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, x):
x = self.features(x)
 x = self.logits(x)
 return x
def initialize_pretrained_model(model, num_classes, settings):
assert num_classes == settings['num_classes'], \
'num_classes should be {}, but is {}'.format(
 settings['num_classes'], num_classes)
 model.load_state_dict(model_zoo.load_url(settings['url']))
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
def senet154(num_classes=1000, pretrained='imagenet'):
model = SENet(SEBottleneck, [3, 8, 36, 3], groups=64, reduction=16,
dropout_p=0.2, num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['senet154'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet50(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 6, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet50'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet101(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 4, 23, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet101'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnet152(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNetBottleneck, [3, 8, 36, 3], groups=1, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnet152'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext50_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 6, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext50_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
def se_resnext101_32x4d(num_classes=1000, pretrained='imagenet'):
model = SENet(SEResNeXtBottleneck, [3, 4, 23, 3], groups=32, reduction=16,
dropout_p=None, inplanes=64, input_3x3=False,
downsample_kernel_size=1, downsample_padding=0,
num_classes=num_classes)
 if pretrained is not None:
settings = pretrained_settings['se_resnext101_32x4d'][pretrained]
initialize_pretrained_model(model, num_classes, settings)
 return model
class Flatten(nn.Module):
def forward(self, x):
return x.view(x.size(0), -1)
 def create_net(net_cls, pretrained: bool):
net = net_cls(pretrained=pretrained)
 return net
def get_head(nf: int, n_classes):
model = nn.Sequential(
 nn.AdaptiveAvgPool2d(1),
Flatten() ,
nn.Linear(nf, n_classes)
)
 return model
def init_weights(model):
for i, module in enumerate(model):
if isinstance(module,(nn.BatchNorm1d, nn.BatchNorm2d)) :
if module.weight is not None:
nn.init.uniform_(module.weight)
 if module.bias is not None:
nn.init.constant_(module.bias, 0)
 if isinstance(module,(nn.Linear, nn.Conv2d, nn.Conv1d)) :
if getattr(module, "weight_v", None)is not None:
print("Initing linear with weight normalization")
 assert model[i].weight_g is not None
else:
nn.init.kaiming_normal_(module.weight)
 print("Initing linear")
 if module.bias is not None:
nn.init.constant_(module.bias, 0)
 return model
def get_seresnet_model(arch: str = "se_resnext50_32x4d", n_classes: int = 10, pretrained: bool = False):
full = se_resnext50_32x4d(
 pretrained='imagenet' if pretrained else None)
 model = nn.Sequential(
 nn.Sequential(full.layer0, full.layer1, full.layer2, full.layer3[:3]),
nn.Sequential(full.layer3[3:], full.layer4),
get_head(2048, n_classes))
print(" | ".join([
"{:,d}".format(np.sum([p.numel() for p in x.parameters() ])) for x in model]))
if pretrained:
init_weights(model[-1])
 return model
return init_weights(model)
 class Swish(nn.Module):
def forward(self, x):

 return x * torch.sigmoid(x)
 pretrained_settings = {
'inceptionv4': {
'imagenet': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth',
'input_space': 'RGB',
'input_size': [3, 299, 299],
'input_range': [0, 1],
'mean': [0.5, 0.5, 0.5],
'std': [0.5, 0.5, 0.5],
'num_classes': 1000
},
'imagenet+background': {
'url': 'http://data.lip6.fr/cadene/pretrainedmodels/inceptionv4-8e4777a0.pth',
'input_space': 'RGB',
'input_size': [3, 299, 299],
'input_range': [0, 1],
'mean': [0.5, 0.5, 0.5],
'std': [0.5, 0.5, 0.5],
'num_classes': 1001
}
}
}
class BasicConv2d(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride, padding=0):
super(BasicConv2d, self ).__init__()
self.conv = nn.Conv2d(in_planes, out_planes,
kernel_size=kernel_size, stride=stride,
padding=padding, bias=False)
 self.bn = nn.BatchNorm2d(out_planes,
eps=0.001,
momentum=0.1,
affine=True)
 self.relu = nn.ReLU(inplace=True)
 def forward(self, x):
x = self.conv(x)
 x = self.bn(x)
 x = self.relu(x)
 return x
class Mixed_3a(nn.Module):
def __init__(self):
super(Mixed_3a, self ).__init__()
self.maxpool = nn.MaxPool2d(3, stride=2)
 self.conv = BasicConv2d(64, 96, kernel_size=3, stride=2)
 def forward(self, x):
x0 = self.maxpool(x)
 x1 = self.conv(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Mixed_4a(nn.Module):
def __init__(self):
super(Mixed_4a, self ).__init__()
self.branch0 = nn.Sequential(
 BasicConv2d(160, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1)
)
 self.branch1 = nn.Sequential(
 BasicConv2d(160, 64, kernel_size=1, stride=1),
BasicConv2d(64, 64, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(64, 64, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(64, 96, kernel_size=(3,3), stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Mixed_5a(nn.Module):
def __init__(self):
super(Mixed_5a, self ).__init__()
self.conv = BasicConv2d(192, 192, kernel_size=3, stride=2)
 self.maxpool = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.conv(x)
 x1 = self.maxpool(x)
 out = torch.cat(( x0, x1), 1)
 return out
class Inception_A(nn.Module):
def __init__(self):
super(Inception_A, self ).__init__()
self.branch0 = BasicConv2d(384, 96, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(384, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1)
)
 self.branch2 = nn.Sequential(
 BasicConv2d(384, 64, kernel_size=1, stride=1),
BasicConv2d(64, 96, kernel_size=3, stride=1, padding=1),
BasicConv2d(96, 96, kernel_size=3, stride=1, padding=1)
)
 self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(384, 96, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class Reduction_A(nn.Module):
def __init__(self):
super(Reduction_A, self ).__init__()
self.branch0 = BasicConv2d(384, 384, kernel_size=3, stride=2)
 self.branch1 = nn.Sequential(
 BasicConv2d(384, 192, kernel_size=1, stride=1),
BasicConv2d(192, 224, kernel_size=3, stride=1, padding=1),
BasicConv2d(224, 256, kernel_size=3, stride=2)
)
 self.branch2 = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 out = torch.cat(( x0, x1, x2), 1)
 return out
class Inception_B(nn.Module):
def __init__(self):
super(Inception_B, self ).__init__()
self.branch0 = BasicConv2d(1024, 384, kernel_size=1, stride=1)
 self.branch1 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(224, 256, kernel_size=(7,1), stride=1, padding=(3,0)) 
)
 self.branch2 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 192, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(192, 224, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(224, 224, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(224, 256, kernel_size=(1,7), stride=1, padding=(0,3)) 
)
 self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(1024, 128, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class Reduction_B(nn.Module):
def __init__(self):
super(Reduction_B, self ).__init__()
self.branch0 = nn.Sequential(
 BasicConv2d(1024, 192, kernel_size=1, stride=1),
BasicConv2d(192, 192, kernel_size=3, stride=2)
)
 self.branch1 = nn.Sequential(
 BasicConv2d(1024, 256, kernel_size=1, stride=1),
BasicConv2d(256, 256, kernel_size=(1,7), stride=1, padding=(0,3)) ,
BasicConv2d(256, 320, kernel_size=(7,1), stride=1, padding=(3,0)) ,
BasicConv2d(320, 320, kernel_size=3, stride=2)
)
 self.branch2 = nn.MaxPool2d(3, stride=2)
 def forward(self, x):
x0 = self.branch0(x)
 x1 = self.branch1(x)
 x2 = self.branch2(x)
 out = torch.cat(( x0, x1, x2), 1)
 return out
class Inception_C(nn.Module):
def __init__(self):
super(Inception_C, self ).__init__()
self.branch0 = BasicConv2d(1536, 256, kernel_size=1, stride=1)
 self.branch1_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1)
 self.branch1_1a = BasicConv2d(384, 256, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch1_1b = BasicConv2d(384, 256, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch2_0 = BasicConv2d(1536, 384, kernel_size=1, stride=1)
 self.branch2_1 = BasicConv2d(384, 448, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch2_2 = BasicConv2d(448, 512, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch2_3a = BasicConv2d(512, 256, kernel_size=(1,3), stride=1, padding=(0,1))
self.branch2_3b = BasicConv2d(512, 256, kernel_size=(3,1), stride=1, padding=(1,0))
self.branch3 = nn.Sequential(
 nn.AvgPool2d(3, stride=1, padding=1, count_include_pad=False),
BasicConv2d(1536, 256, kernel_size=1, stride=1)
)
 def forward(self, x):
x0 = self.branch0(x)
 x1_0 = self.branch1_0(x)
 x1_1a = self.branch1_1a(x1_0)
 x1_1b = self.branch1_1b(x1_0)
 x1 = torch.cat(( x1_1a, x1_1b), 1)
 x2_0 = self.branch2_0(x)
 x2_1 = self.branch2_1(x2_0)
 x2_2 = self.branch2_2(x2_1)
 x2_3a = self.branch2_3a(x2_2)
 x2_3b = self.branch2_3b(x2_2)
 x2 = torch.cat(( x2_3a, x2_3b), 1)
 x3 = self.branch3(x)
 out = torch.cat(( x0, x1, x2, x3), 1)
 return out
class InceptionV4(nn.Module):
def __init__(self, num_classes=1001):
super(InceptionV4, self ).__init__()
self.input_space = None
self.input_size =(299, 299, 3)
 self.mean = None
self.std = None
self.features = nn.Sequential(
 BasicConv2d(3, 32, kernel_size=3, stride=2),
BasicConv2d(32, 32, kernel_size=3, stride=1),
BasicConv2d(32, 64, kernel_size=3, stride=1, padding=1),
Mixed_3a() ,
Mixed_4a() ,
Mixed_5a() ,
Inception_A() ,
Inception_A() ,
Inception_A() ,
Inception_A() ,
Reduction_A() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Inception_B() ,
Reduction_B() ,
Inception_C() ,
Inception_C() ,
Inception_C() 
)
 self.last_linear = nn.Linear(1536, num_classes)
 def logits(self, features):
adaptiveAvgPoolWidth = features.shape[2]
x = F.avg_pool2d(features, kernel_size=adaptiveAvgPoolWidth)
 x = x.view(x.size(0), -1)
 x = self.last_linear(x)
 return x
def forward(self, input):
x = self.features(input)
 x = self.logits(x)
 return x
def inceptionv4(num_classes=1000, pretrained='imagenet'):
if pretrained:
settings = pretrained_settings['inceptionv4'][pretrained]
assert num_classes == settings['num_classes'], \
"num_classes should be {}, but is {}".format(settings['num_classes'], num_classes)
 model = InceptionV4(num_classes=1001)
 model.load_state_dict(model_zoo.load_url(settings['url']))
if pretrained == 'imagenet':
new_last_linear = nn.Linear(1536, 1000)
 new_last_linear.weight.data = model.last_linear.weight.data[1:]
new_last_linear.bias.data = model.last_linear.bias.data[1:]
model.last_linear = new_last_linear
model.input_space = settings['input_space']
model.input_size = settings['input_size']
model.input_range = settings['input_range']
model.mean = settings['mean']
model.std = settings['std']
else:
model = InceptionV4(num_classes=num_classes)
 return model 
<set_options>
gbm = GradientBoostingClassifier(random_state=2)
 gbm.fit(X_train,Y_train)
 print('Score: ',gbm.score(X_test,Y_test))
feature_importances = pd.DataFrame(gbm.feature_importances_,index = X_test.columns,columns=['importance'] ).sort_values('importance', ascending=False)
 feature_importances.head(34 )
<set_options><EOS> 
test_n=dataset[dataset['Survived'].isnull() ]
test_n.drop(labels = ["Survived"], axis = 1, inplace = True)
 submission=pd.DataFrame(columns=['PassengerId','Survived'])
 submission['PassengerId']=test_n['PassengerId']
test_n.drop(labels = ["PassengerId"], axis = 1, inplace = True)
 Y_pred=gbm.predict(test_n)
 submission['Survived']=Y_pred.astype(int)
 submission.to_csv('submission_gbm.csv',header=True,index=False )