Kaggle competition: Titanic - Machine Learning from Disaster
<SOS> metric: categorizationaccuracy Kaggle data source: titanic-machine-learning-from-disaster<train_model>
warnings.filterwarnings("ignore")
 %matplotlib inline
kmeans = KMeans(
 n_clusters=30,
random_state=0 
)
 kmeans_train = kmeans.fit(a )<predict_on_test>
train = pd.read_csv(".. /input/titanic/train.csv")
 test = pd.read_csv(".. /input/titanic/test.csv")
 submission = pd.read_csv(".. /input/titanic/gender_submission.csv")
 data = train.append(test )
train['cluster'] = kmeans_train.labels_
test['cluster'] = kmeans.predict(b )<prepare_x_and_y>
train.isnull().sum()
y = train['likes']<feature_engineering>
train.isnull().sum()
def ing(df):
df['user_len'] = df['user'].str.len()
df['username_len'] = df['username'].str.len()
df['img_len'] = df['img'].str.len()
df['description_len'] = df['description'].str.len()
df['caption_upper'] = df['caption'].str.findall(r'[A-Z]' ).str.len()
df['po_co'] = df['posts']/(df['comments']+1)
 df['po_co_pow'] = df['posts']*(df['comments'])
 df['fol'] = df['followers']/(df['followings']+1)
 df['fol_pow'] = df['followers']*(df['followings'])
 df['act'] = df['comments']/(df['followers']+1)
 df['pos'] = df['posts']/(df['followers']+1)
 df['pof'] = df['posts']/(df['followings']+1)
 df['user_count'] = df.groupby('user')['posts'].transform('count')
 df['language_encoded'] = df.groupby('user')['language_encoded'].transform('median')
 df['hashtag_max'] = df.groupby('user')['hashtag'].transform('max')
 df['hashtag_min'] = df.groupby('user')['hashtag'].transform('min')
 df['hashtag_mean'] = df.groupby('user')['hashtag'].transform('mean')
 df['hashtag_std'] = df.groupby('user')['hashtag'].transform('std')
 df['hashtag_mean_diff'] = df['hashtag'] /(df['hashtag_mean']+1)
 df['comments_max'] = df.groupby('user')['comments'].transform('max')
 df['comments_min'] = df.groupby('user')['comments'].transform('min')
 df['comments_mean'] = df.groupby('user')['comments'].transform('mean')
 df['comments_std'] = df.groupby('user')['comments'].transform('std')
 df['comments_mean_diff'] = df['comments'] /(df['comments_mean']+1)
 df['bot'] = df['user'].str.count('_|
df['bot_caption'] = pd.to_numeric(df['caption'].str.count('_|
df['numbers_user'] = df['user'].str.contains('0|1|2|3|4|5|6|7|8|9' ).astype(int)
 df['numbers_caption'] = pd.to_numeric(df['caption'].str.contains('0|1|2|3|4|5|6|7|8|9'), errors='coerce')
 df['s1080.1080'] = pd.to_numeric(df['img'].str.contains('1080\.1080'), errors='coerce')
 df['e35'] = pd.to_numeric(df['img'].str.contains('e35'), errors='coerce')
 return df<drop_column>
test.isnull().sum()
train = ing(train ).select_dtypes(exclude=['object'] ).drop(['likes'], axis=1 ).fillna(0)
 test = ing(test ).select_dtypes(exclude=['object'] ).fillna(0 )<choose_model_class>
test.isnull().sum()
lgbm_model = lgb.LGBMRegressor(
 application='mae',
metric='mape',
learning_rate=0.005,
n_estimators = 5500,
num_leaves = 10,
colsample_bytree = 0.59,
subsample = 0.5,
max_bin = 100,
reg_alpha = 1,
bagging_freq = 5,
reg_lamba=10,
min_data = 1)
 param_grid = {
}
def smape(A, F):
A = 10**(A)-1
F = 10**(F)-1
return 100/len(A)* np.sum(2 * np.abs(F - A)/(np.abs(A)+ np.abs(F)))
 scoring = {'smape': make_scorer(smape, greater_is_better=False)} 
<choose_model_class>
train.groupby('Pclass' ).Survived.value_counts()
xgb_model = xgb.XGBRegressor(max_depth=7,
learning_rate=0.01,
n_estimators=870,
booster='gbtree',
n_jobs=-1,
min_child_weight=5,
subsample=0.8,
colsample_bytree=0.8,
base_score=0.5,
random_state=42,
reg_alpha=0.2,
reg_lambda=3
)<train_model>
train['family'] = train['SibSp'] + train['Parch'] + 1
VotReg = VotingRegressor(estimators=[('lgbm', lgbm_model),('xgb', xgb_model)])
 VotReg.fit(train, np.log10(y+1))<save_to_csv>
train.groupby('family' ).Survived.value_counts()
print(10**(VotReg.predict(test)) -1)
 prediction = pd.read_csv('.. /input/sample_submission.csv')
 prediction['likes'] =(10**(VotReg.predict(test)))-1
prediction.to_csv('exp_submission.csv', index=False )<install_modules>
for i in range(len(train)) :
if(train['family'][i] > 1):
train['family'][i] = 1
else:
train['family'][i] = 0
!pip install gdown
!gdown https://drive.google.com/uc?id=1-2ayU2W8YnVgvfHEukbpT9-HyrLF5vXt<install_modules>
train['Name'] = train.Name.str.extract('([A-Za-z]+)\.',expand = False )
!pip install torchsummary<set_options>
top6 = train['Name'].value_counts() [:6].index.to_list()
top6
%matplotlib inline<set_options>
train['Name'] = train['Name'].apply(lambda x: x if x in top6 else 'Other' )
torch.manual_seed(1337)
 torch.cuda.manual_seed(1337)
 np.random.seed(1337)
 random.seed(1337 )<load_from_csv>
train.groupby('Name' ).Survived.value_counts()
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):
self.data_frame = pd.read_csv(csv_file)
 self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
 def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.data_frame['Id'][idx])
 image = Image.open(img_name ).convert('RGB')
 label = np.array(self.data_frame['Category'][idx])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample<choose_model_class>
train['Cabin'].fillna(0,inplace = True )
class FocalLoss(nn.Module):
def __init__(self, num_class, alpha=None, gamma=2, balance_index=-1, smooth=None, size_average=True):
super(FocalLoss, self ).__init__()
self.num_class = num_class
self.alpha = alpha
self.gamma = gamma
self.smooth = smooth
self.size_average = size_average
if self.alpha is None:
self.alpha = torch.ones(self.num_class, 1)
 elif isinstance(self.alpha,(list, np.ndarray)) :
assert len(self.alpha)== self.num_class
self.alpha = torch.FloatTensor(alpha ).view(self.num_class, 1)
 self.alpha = self.alpha / self.alpha.sum()
elif isinstance(self.alpha, float):
alpha = torch.ones(self.num_class, 1)
 alpha = alpha *(1 - self.alpha)
 alpha[balance_index] = self.alpha
self.alpha = alpha
else:
raise TypeError('Not support alpha type')
 if self.smooth is not None:
if self.smooth < 0 or self.smooth > 1.0:
raise ValueError('smooth value should be in [0,1]')
 def forward(self, logit, target):
if logit.dim() > 2:
logit = logit.view(logit.size(0), logit.size(1), -1)
 logit = logit.permute(0, 2, 1 ).contiguous()
logit = logit.view(-1, logit.size(-1))
target = target.view(-1, 1)
 epsilon = 1e-10
alpha = self.alpha
idx = target.cpu().long()
one_hot_key = torch.FloatTensor(target.size(0), self.num_class ).zero_()
one_hot_key = one_hot_key.scatter_(1, idx, 1)
 if one_hot_key.device != logit.device:
one_hot_key = one_hot_key.to(logit.device)
 if self.smooth:
one_hot_key = torch.clamp(
 one_hot_key, self.smooth/(self.num_class-1), 1.0 - self.smooth)
 pt =(one_hot_key * logit ).sum(1)+ epsilon
logpt = pt.log()
gamma = self.gamma
alpha = alpha[idx]
alpha=alpha.to(device)
 loss = -1 * alpha * torch.pow(( 1 - pt), gamma)* logpt
if self.size_average:
loss = loss.mean()
else:
loss = loss.sum()
return loss 
<init_hyperparams>
train['Cabin'] = train['Cabin'].apply(lambda x: 1 if x != 0 else 0 )
class SubsetSampler(Sampler):
def __init__(self, indices):
self.num_samples = len(indices)
 self.indices = indices
def __iter__(self):
return iter(self.indices)
 def __len__(self):
return self.num_samples<split>
train.groupby('Cabin' ).Survived.value_counts()
def train_valid_split(dataset, test_size = 0.25, shuffle = False, random_seed = 0):
length = dataset.__len__()
indices = list(range(1,length))
if shuffle == True:
random.seed(random_seed)
 random.shuffle(indices)
 if type(test_size)is float:
split = floor(test_size * length)
 elif type(test_size)is int:
split = test_size
else:
raise ValueError('%s should be an int or a float' % str)
 return indices[split:], indices[:split]<categorify>
train['fare_val'] = pd.qcut(train['Fare'],5,labels=False)
 train['fare_val'] = train['fare_val'].astype(int )
transform_augmented = transforms.Compose([
transforms.RandomResizedCrop(224),
PowerPIL() ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
 ])
 transform_raw = transforms.Compose([
transforms.Resize(( 224,224)) ,
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])
<load_from_csv>
trainset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_augmented)
 valset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_raw)
 accset = ImageDataset(csv_file = './.. /input/train.csv', root_dir = './.. /input/data/data', transform=transform_raw )<load_from_csv>
train.groupby('fare_val' ).Survived.value_counts()
df=pd.read_csv("./.. /input/train.csv" )<feature_engineering>
for i in train.Ticket:
train['ticket'] = train['Ticket'].apply(lambda x: x.replace('/','' ).replace('.','' ).split() [0] if not x.isdigit() else 'N' )
class_freq=defaultdict(int)
 freq=[]
for ind,val in df.iterrows() :
class_freq[int(val["Category"])]+=1
for ind,val in class_freq.items() :
freq.append([ind,val])
 freq.sort()
freq=[i[1] for i in freq]
wt_per_class=[0.]*67
N=float(sum(freq))
for i in range(67):
wt_per_class[i] = N/float(freq[i])
 weight=[0]*len(df)
 for ind,val in df.iterrows() :
cat=val["Category"]
weight[ind]=wt_per_class[cat]<load_pretrained>
train['SibSp'] = pd.cut(train['SibSp'],bins=[0,1,100],labels=[0,1],right=False ).astype(int)
 train['Parch'] = pd.cut(train['Parch'], bins=[0,1,100], labels=[0,1], right=False ).astype(int )
train_idx, valid_idx = train_valid_split(trainset, 0.25)
 train_sampler = SubsetRandomSampler(train_idx)
 valid_sampler = SubsetSampler(valid_idx)
 train_loader = DataLoader(trainset,batch_size=50,sampler=train_sampler,num_workers=4)
 valid_loader = DataLoader(valset,batch_size=200,sampler=valid_sampler,num_workers=4)
 acc_loader = DataLoader(accset,batch_size=200,num_workers=4)
<set_options>
train.groupby('Parch' ).Survived.value_counts()
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu" )<load_pretrained>
test['family'] = test['SibSp'] + test['Parch'] + 1
for i in range(len(test)) :
if(test['family'][i] > 1):
test['family'][i] = 1
else:
test['family'][i] = 0
test['Name'] = test['Name'].apply(lambda x: x if x in top6 else 'Other')
 test['Cabin'].fillna(0,inplace = True)
 test['Cabin'] = test['Cabin'].apply(lambda x: 1 if x != 0 else 0)
 for i in test.Ticket:
test['ticket'] = test['Ticket'].apply(lambda x: x.replace('/','' ).replace('.','' ).split() [0] if not x.isdigit() else 'N')
 test['fare_val'] = pd.qcut(test['Fare'],5,labels=False)
 test['fare_val'] = test['fare_val'].fillna(value = test['fare_val'].mode() [0])
 test['fare_val'] = test['fare_val'].astype(int)
 test['SibSp'] = pd.cut(test['SibSp'],bins = [0,1,200],labels=[0,1],right = False ).astype(int)
 test['Parch'] = pd.cut(test['Parch'],bins = [0,1,200],labels=[0,1],right = False ).astype(int )
model_ft = torch.hub.load('facebookresearch/WSL-Images', 'resnext101_32x16d_wsl')
<choose_model_class>
features = [
'Pclass',
'Name',
'Sex',
'Age',
'SibSp',
'Parch',
'fare_val',
]
target = ['Survived']
avgpool = model_ft.avgpool
l4 = model_ft.layer4
l3 = model_ft.layer3<find_best_params>
train[features].isnull().sum()
for param in model_ft.parameters() :
param.requires_grad = False 
<choose_model_class>
test[features].isnull().sum()
model_ft.fc = nn.Sequential(nn.Dropout(p=0.5),nn.Linear(2048, 1024),nn.LeakyReLU(inplace=True),nn.Linear(1024,67),nn.Softmax(dim=1)) 
<train_model>
;
model_ft.to(device )<choose_model_class>
Age_mean = data['Age'].mean()
Age_std = data['Age'].std()
train['Age'] = train['Age'].fillna(np.random.randint(Age_mean-Age_std,Age_mean+Age_std))
test['Age'] = test['Age'].fillna(np.random.randint(Age_mean-Age_std,Age_mean+Age_std))
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model_ft.parameters() , lr=0.00001,momentum=0.9 )<load_pretrained>
lbl = LabelEncoder()
train['Sex'] = lbl.fit_transform(train[['Sex']].values.ravel())
 test['Sex'] = lbl.fit_transform(test[['Sex']].values.ravel() )
model_ft_wts=torch.load("best_model_resnext_16d_2048_1024_dropout_0.5_b.pkl" )<load_from_disk>
lbl2 = LabelEncoder()
train['Name'] = lbl2.fit_transform(train[['Name']].values.ravel())
 test['Name'] = lbl2.fit_transform(test[['Name']].values.ravel() )
model_ft.load_state_dict(model_ft_wts )<find_best_params>
best_acc=0.84516
best_model_wts = copy.deepcopy(model_ft.state_dict() )<train_model>
train['Embarked'] = train['Embarked'].fillna(value=train['Embarked'].mode() [0])
 test['Embarked'] = test['Embarked'].fillna(value=test['Embarked'].mode() [0])
num_epochs=0
for epoch in range(num_epochs):
print("Epoch : "+str(epoch))
print("-"*10)
 running_loss = 0.0
running_corrects=0
wrong=0
model_ft.train()
for inp,labels in train_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.set_grad_enabled(True):
outputs = model_ft(inp)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
running_loss += loss.item()
epoch_loss = running_loss /(len(train_loader)*1.0)
 print('TRAINING SET Loss: {}'.format(epoch_loss))
if True:
correct=0
wrong=0
model_ft.eval()
for inp,labels in valid_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 correct += torch.sum(preds == labels)
 wrong += torch.sum(preds != labels)
 acc =(correct.float())/(( correct+wrong ).float())
 print('VALIDATION SET Correct: {} Wrong {} Acc {}'.format(correct,wrong,acc))
if acc > best_acc:
best_acc = acc
best_model_wts = copy.deepcopy(model_ft.state_dict())
 running_loss = 0.0
running_correct = 0
model_ft.load_state_dict(best_model_wts)
 torch.save(model_ft.state_dict() , "best_model_resnext_16d_2048_1024_dropout_0.5_c_wts.pkl")
 print('------ Finished Training -----' )<load_from_disk>
train_ds = train[features+target]
test_ds = test[features]
model_ft.load_state_dict(best_model_wts )<find_best_params>
if True:
correct=0
wrong=0
model_ft.eval()
for inp,labels in acc_loader:
inp=inp.to(device)
 labels=labels.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 correct += torch.sum(preds == labels)
 wrong += torch.sum(preds != labels)
 acc =(correct.float())/(( correct+wrong ).float())
 print('ACCURACY SET Correct: {} Wrong {} ACC {} '.format(correct,wrong,acc))<load_from_csv>
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()<load_from_csv>
torch.manual_seed(7 )
testset = ImageDataset(csv_file = '.. /input/sample_sub.csv', root_dir = '.. /input/data/data/', transform=transform_raw)
 testloader = torch.utils.data.DataLoader(testset, batch_size=300, shuffle=False, num_workers=4)
 res_predict=[]<find_best_params>
class Titanicnn(nn.Module):
def __init__(self,in_size,hidden_size1,hidden_size2,hidden_size3,hidden_size4,num_classes):
super().__init__()
self.linear1 = nn.Linear(in_size, hidden_size1)
 self.linear2 = nn.Linear(hidden_size1, hidden_size2)
 self.linear3 = nn.Linear(hidden_size2,hidden_size3)
 self.linear4 = nn.Linear(hidden_size3,hidden_size4)
 self.linear5 = nn.Linear(hidden_size4,num_classes)
 self.dropout = nn.Dropout(0.2)
 def forward(self,xb):
xb = xb.view(-1,xb.size(1)).float()
out = self.linear1(xb)
 out = F.relu(out)
 out = self.linear2(out)
 out = self.dropout(out)
 out = self.linear3(out)
 out = F.relu(out)
 out = self.linear4(out)
 out = self.dropout(out)
 out = self.linear5(out)
 out = self.dropout(out)
 return out
def training_step(self,batch):
features, labels = batch
out = self(features)
 loss = F.cross_entropy(out, labels)
 return loss
def validation_step(self,batch):
features,labels = batch
out = self(features)
 loss = F.cross_entropy(out, labels)
 acc = accuracy(out,labels)
 return {'valid_loss': loss, 'valid_acc': acc}
def validation_epoch_end(self,outputs):
batch_losses = [x['valid_loss'] for x in outputs]
epoch_loss = torch.stack(batch_losses ).mean()
batch_accs = [x['valid_acc'] for x in outputs]
epoch_acc = torch.stack(batch_accs ).mean()
return {'valid_loss': epoch_loss.item() , 'valid_acc': epoch_acc.item() }
def epoch_end(self,epoch,result):
print("Epoch [{}], valid_loss: {:.4f}, valid_acc: {:.4f}".format(epoch, result['valid_loss'], result['valid_acc']))
model_ft.eval()
for inp,label in testloader:
inp = inp.to(device)
 label = label.to(device)
 optimizer.zero_grad()
with torch.no_grad() :
outputs = model_ft(inp)
 _, preds = torch.max(outputs.data, 1)
 for i in range(len(preds)) :
res_predict.append(int(preds[i]))
submission['Category'] = res_predict<save_to_csv>
def accuracy(outputs, labels):
_, preds = torch.max(outputs, dim=1)
 return torch.tensor(torch.sum(preds == labels ).item() / len(preds))
return preds
submission.to_csv('submission.csv', index=False, encoding='utf-8' )<set_options>
input_size = train_ds.shape[1] -1
hidden_size1 = 128
hidden_size2 = 1024
hidden_size3 = 512
hidden_size4 = 128
num_classes = 2
%matplotlib inline<load_from_csv>
model = Titanicnn(input_size, hidden_size1=hidden_size1, hidden_size2= hidden_size2,hidden_size3 = hidden_size3,hidden_size4 = hidden_size4,num_classes = num_classes)
 model
df = pd.read_csv('.. /input/train.csv')
 df.head()<load_from_csv>
def convert_to_tensors(ds, valid_size = 0.30,train_set=True):
scaler = StandardScaler()
batch_size = ds.shape[0]
if(train_set == True):
targets_t = ds.Survived.values
features_t = ds.drop(labels = ['Survived'],axis = 1 ).values
features_t = scaler.fit_transform(features_t)
 targetsTrain = torch.from_numpy(targets_t ).type(torch.LongTensor)
 featuresTrain = torch.from_numpy(features_t)
 train_tensor = torch.utils.data.TensorDataset(featuresTrain,targetsTrain)
 valid_size = round(len(train_tensor)*(valid_size))
train_size = len(train_tensor)- valid_size
ttrain_ds, tvalid_ds = random_split(train_tensor, [train_size, valid_size])
 print("train_size:- ",len(ttrain_ds),"test_size:- ", len(tvalid_ds))
train_loader = DataLoader(ttrain_ds, batch_size = batch_size, shuffle=True, num_workers=4, pin_memory=True)
 valid_loader = DataLoader(tvalid_ds, batch_size = batch_size*2, num_workers=4, pin_memory=True)
 return train_loader,valid_loader
else:
featuresTest = ds.values
features_test = scaler.fit_transform(featuresTest)
 featuresTensor = torch.from_numpy(features_test)
 print(len(featuresTensor))
test_loader = DataLoader(featuresTensor, batch_size*2, num_workers=4, pin_memory = True)
 return test_loader
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):

 self.data_frame = pd.read_csv(csv_file)
 self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
 def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.data_frame['Id'][idx])
 image = Image.open(img_name ).convert('RGB')
 label = np.array(self.data_frame['Category'][idx])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample<define_variables>
train_loader,valid_loader = convert_to_tensors(train_ds )
for i in range(len(trainset)) :
sample = trainset[i]
print(i, sample[0].size() , " | Label: ", sample[1])
 if i == 9:
break<train_model>
for data, labels in train_loader:
outputs = model(data)
 print(labels.shape)
 loss = F.cross_entropy(outputs, labels)
 acc = accuracy(outputs,labels)
 print('Loss:', loss.item())
 print('Initial Acc:',float(acc),"%")
 break
print('outputs.shape : ', outputs.shape)
 print('Sample outputs :
', outputs[:5].data)
 print('Sample labels :
', labels[:5].data )
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...' )<choose_model_class>
torch.cuda.is_available()
dir(models)
 model=models.resnext101_32x8d(pretrained=True)
 model<find_best_params>
def get_default_device() :
if torch.cuda.is_available() :
return torch.device('cuda')
 else:
return torch.device('cpu' )
for param in model.parameters() :
param.requires_grad = False<choose_model_class>
def to_device(data,device):
if isinstance(data,(list,tuple)) :
return [to_device(x,device)for x in data]
return data.to(device,non_blocking = True )
model.fc = nn.Sequential(
 nn.Linear(2048, 512),
nn.ReLU() ,
nn.Dropout(0.05),
nn.Linear(512,67), 
)
 model.cuda()
model<choose_model_class>
for xd, yd in train_loader:
print(xd.shape)
 xds = to_device(xd, device)
 print(xds.device)
 break
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.fc.parameters() , lr=3e-4 )<train_model>
class DeviceDataLoader() :
def __init__(self, ds, device):
self.ds = ds
self.device = device
def __iter__(self):
for d in self.ds:
yield to_device(d,self.device)
 def __len__(self):
return len(self.ds )
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )<find_best_params>
train_loader = DeviceDataLoader(train_loader, device)
 valid_loader = DeviceDataLoader(valid_loader, device )
epochs=4
iter=0
for epoch in range(epochs):
for i,(images,labels)in enumerate(trainloader):
images=Variable(images.cuda())
 labels=Variable(labels.cuda())
 optimizer.zero_grad()
outputs=model(images)
 loss=criterion(outputs, labels)
 loss.backward()
optimizer.step()
iter+=1
if iter%500==0:
correct=0
total=0
for images, labels in trainloader:
images=Variable(images.cuda())
 outputs=model(images)
 _, predicted=torch.max(outputs.data,1)
 total+=labels.size(0)
 correct+=(predicted.cpu() ==labels.cpu() ).sum()
accuracy=(100*correct/total)
 print("Iteration {}, Loss {}, Accuracy {}".format(iter, loss.data.item() , accuracy))<find_best_params>
for xd, yd in valid_loader:
print('xd.device:', xd.device)
 print('yd:', yd)
 break
for param in model.layer4.parameters() :
param.requires_grad = True<choose_model_class>
def evaluate(model, valid_loader):
outputs = [model.validation_step(batch)for batch in valid_loader]
return model.validation_epoch_end(outputs )
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=2e-4 )<train_model>
def fit(epochs, lr, model, train_loader, valid_loader, opt_func = torch.optim.SGD):
history = []
optimizer = opt_func(model.parameters() ,lr)
 for epoch in tqdm(range(epochs)) :
for batch in train_loader:
loss = model.training_step(batch)
 loss.backward()
optimizer.step()
optimizer.zero_grad()
result = evaluate(model,valid_loader)
 model.epoch_end(epoch, result)
 history.append(result)
 return history
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )<find_best_params>
model = Titanicnn(input_size,hidden_size1=hidden_size1, hidden_size2= hidden_size2, hidden_size3= hidden_size3,hidden_size4 = hidden_size4,num_classes = num_classes)
 to_device(model, device )
for param in model.layer3.parameters() :
param.requires_grad = True<choose_model_class>
history = [evaluate(model,valid_loader)]
history
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters() , lr=1e-4 )<train_model>
history += fit(20, 0.2, model, train_loader, valid_loader )
total_params = sum(p.numel() for p in model.parameters())
 print(f'{total_params:,} total parameters.')
 total_trainable_params = sum(
 p.numel() for p in model.parameters() if p.requires_grad)
 print(f'{total_trainable_params:,} training parameters.' )<create_dataframe>
history += fit(40, 0.001, model, train_loader, valid_loader )
test_transforms = transforms.Compose([transforms.Resize(256),
transforms.CenterCrop(224),
transforms.ToTensor() ,
transforms.Normalize([0.485, 0.456, 0.406],
[0.229, 0.224, 0.225])])
 testset = ImageDataset(csv_file = '.. /input/sample_sub.csv', root_dir = '.. /input/data/data/', transform=test_transforms)
 testloader = torch.utils.data.DataLoader(testset, batch_size=16, shuffle=False, num_workers=4 )<load_from_csv>
history += fit(100, 0.01, model, train_loader, valid_loader )
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()<save_to_csv>
history += fit(300, 0.01, model, train_loader, valid_loader )
submission.to_csv('submission.csv', index=False, encoding='utf-8')
<set_options>
history += fit(30, 0.001, model, train_loader, valid_loader )
%matplotlib inline 
<load_from_csv>
test_loader = convert_to_tensors(test_ds,train_set= False )
df = pd.read_csv('.. /input/train.csv')
 df.head()<load_from_csv>
def predict(pred_x,model):
for pred in pred_x:
y = model(pred)
 _, y_ = torch.max(y, dim=1)
 return y_
class ImageDataset(Dataset):
def __init__(self, csv_file, root_dir, transform=None):

 self.data_frame = pd.read_csv(csv_file)
 self.root_dir = root_dir
self.transform = transform
def __len__(self):
return len(self.data_frame)
 def __getitem__(self, idx):
img_name = os.path.join(self.root_dir, self.data_frame['Id'][idx])
 image = Image.open(img_name ).convert('RGB')
 label = np.array(self.data_frame['Category'][idx])
 if self.transform:
image = self.transform(image)
 sample =(image, label)
 return sample<create_dataframe>
test_loader_gpu = DeviceDataLoader(test_loader, device )
normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
 transform = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.RandomHorizontalFlip() ,
transforms.ToTensor() ,
normalize
])
 trainset = ImageDataset(csv_file = '.. /input/train.csv', root_dir = '.. /input/data/data/', transform=transform)
 trainloader = torch.utils.data.DataLoader(trainset, batch_size=16, shuffle=True, num_workers=0 )<load_from_csv>
test_preds = predict(test_loader_gpu,model )
<define_variables>
passId = test[['PassengerId']].values
for i in range(len(trainset)) :
sample = trainset[i]
print(i, sample[0].size() , " | Label: ", sample[1])
 if i == 9:
break<train_model>
final_pred = final_pred.data.numpy()
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...' )<choose_model_class>
sub = {'PassengerId':passId.ravel() , 'Survived':final_pred}
<choose_model_class>
submission_csv = pd.DataFrame(sub )
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters() ,lr=0.00007)
 if train_on_gpu:
criterion = criterion.cuda() 
<find_best_params>
submission_csv.to_csv('final_sub_titanic_pth.csv',index=False )
epochs = 14
iter=0
for epoch in range(epochs):
for i,(images, labels)in enumerate(trainloader):
if train_on_gpu:
images = Variable(images.cuda())
 labels = Variable(labels.cuda())
 else:
images = Variable(images)
 labels = Variable(labels)
 optimizer.zero_grad()
outputs = model(images)
 loss = criterion(outputs, labels)
 loss.backward()
optimizer.step()
iter+=1
if iter%100 == 0:
print(iter, end="...")
 print(" ")
 print("<train_model>
x = pd.read_csv("./final_sub_titanic_pth.csv" )
model.eval() 
<load_from_csv>
filename = "./titanic_pytorch.pth"
submission = pd.read_csv('.. /input/sample_sub.csv')
 submission.head()<load_from_csv>
torch.save(model.state_dict() ,filename )
<find_best_params><EOS> 
loaded_model = Titanicnn(input_size,hidden_size1=hidden_size1, hidden_size2= hidden_size2, hidden_size3= hidden_size3,hidden_size4 = hidden_size4,num_classes = num_classes)
 to_device(model, device)
 loaded_model.load_state_dict(torch.load(filename))
loaded_model.eval()