Kaggle competition: PUBG Finish Placement Prediction (Kernels Only)
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(32, 3, 1, padding='same', activation='relu', input_shape=(height, width, channels)))
 model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Conv2D(32, 3, 1, padding='same', activation='relu', input_shape=(height, width, channels)))
 model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.20))
model.add(tf.keras.layers.Conv2D(64, 3, 1, padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Conv2D(64, 3, 1, padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(2, 2), padding='valid'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.20))
model.add(tf.keras.layers.Conv2D(128, 3, 1, padding='same', activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Flatten())
 model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.BatchNormalization())
 model.add(tf.keras.layers.Dropout(0.20))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.compile(optimizer='Adam', loss="categorical_crossentropy", metrics=["accuracy"] )<train_model>
import lightgbm
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.metrics import mean_absolute_error, mean_squared_error
model.fit(train_data_gen, batch_size=batch_size, epochs = n_epochs, validation_data =(X_val, y_val), callbacks=[calrc], verbose=2 )<find_best_model_class>
def match_normalize(match):
match = match.groupby('groupId' ).mean()
for head in match.columns.values:
if head != 'winPlacePerc':
match[head] /= match[head].max()
match = match.drop(columns=['maxPlace', 'numGroups'])
 return match
def rearrange(t_order, p_orders):
flag = True
while flag:
flag = False
for p_order in p_orders:
for i in range(len(p_order)-1):
if t_order.index(p_order[i])> t_order.index(p_order[i+1]):
t_order.remove(p_order[i])
 t_order.insert(t_order.index(p_order[i+1]), p_order[i])
 flag = True
return t_order
def load_all_models(n_models):
all_models = list()
for i in range(n_models):
filename = f'snapshot_model_{str(i)}.h5'
model = tf.keras.models.load_model(filename)
 all_models.append(model)
 return all_models
def ensemble_predictions(models, testX):
yhats = [model.predict(testX)for model in models]
yhats = np.array(yhats)
 summed = np.sum(yhats, axis=0)
 result = np.argmax(summed, axis=1)
 return result
def evaluate_n_models(models, n_models, testX, testy):
subset = models[:n_models]
yhat = ensemble_predictions(subset, testX)
 return sk.metrics.accuracy_score(testy, yhat )<load_from_csv>
train = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/train_V2.csv' ).dropna()
X_pred = pd.read_csv('.. /input/digit-recognizer/test.csv')
 X_pred = scaler.transform(X_pred)
 X_pred = X_pred.reshape(-1, height, width, channels )<save_to_csv>
train = train.drop(['swimDistance', 'headshotKills', 'killPoints', 'matchDuration', 'rankPoints', 'teamKills', 'winPoints'], axis=1)
 train = train.groupby('matchId')
 train_groupwise = train.apply(match_normalize )
y_pred = pd.DataFrame()
y_pred['ImageId'] = pd.Series(range(1,X_pred.shape[0] + 1))
y_pred['Label'] = ensemble_predictions(models, X_pred)
 y_pred.to_csv("submission.csv", index=False )<load_from_csv>
train_matchId = list(train.groups.keys())
 split = train_groupwise.index.get_loc(train_matchId[round(len(train_matchId)* 0.8)] ).start
train_X = train_groupwise.drop('winPlacePerc', axis=1 ).iloc[:split]
train_Y = train_groupwise['winPlacePerc'].iloc[:split]
valid_X = train_groupwise.drop('winPlacePerc', axis=1 ).iloc[split:]
valid_Y = train_groupwise['winPlacePerc'].iloc[split:]
Path.ls = lambda x: list(x.iterdir())
 path = Path('/kaggle/input/digit-recognizer/')
 def get_data(path,fn='train.csv'):
df = pd.read_csv(path/fn)
 if 'label' not in df.columns:
vals = np.ones_like(df.iloc[:,0].values)*-1
df.insert(0,'label',vals)
 X = df.iloc[:,1:].values
y = df.iloc[:,0].values
return X,y
class Dataset:
def __init__(self,X,y):
self.X, self.y = X,y
def __len__(self):
return len(self.X)
 def __getitem__(self,idx):
return torch.tensor(self.X[idx],dtype=torch.float),torch.tensor(self.y[idx],dtype=torch.long)
 def get_dls(train_ds,test_ds,bs=64):
return(DataLoader(train_ds,batch_size = bs, shuffle=True, drop_last=True),
DataLoader(test_ds, batch_size = bs*2, shuffle=False))
def init_cnn(m, uniform=False):
f = init.kaiming_uniform_ if uniform else init.kaiming_normal_
for l in m:
if isinstance(l, nn.Sequential):
f(l[0].weight, a=0.1)
 if l[0].bias is not None:
l[0].bias.data.zero_()
class Lambda(nn.Module):

 def __init__(self,func):
super().__init__()
self.func = func
def forward(self,x):
return self.func(x)
 def flatten(x):
return x.view(x.shape[0],-1)
 def mnist_resize(x):
return x.view(-1,1,28,28)
 def get_model(layers,pct_sparsity=0.,use_gpu=True):

 model = layers;
if use_gpu:
return model.cuda()
return model
def get_optimizer(model,lr=0.1):

 return optim.SGD(model.parameters() , lr=lr, momentum=0.9)
 def accuracy(preds, y):
return(torch.argmax(preds, dim=1)== y ).float().mean()
def average_metrics(dataloader,model,metrics=[],use_gpu=True):

 with torch.no_grad() :
count = 0
tot_metrics = [0.for _ in metrics]
for xb,yb in dataloader:
if use_gpu:
xb, yb = xb.cuda() , yb.cuda()
bs=len(xb)
 for idx, metric in enumerate(metrics):
tot_metrics[idx] += metric(model(xb),yb)* bs
count += bs
avg_metrics = list()
for metric in tot_metrics:
avg_metrics.append(metric/count)
 return avg_metrics
def fit_one_cycle(epochs,sched_func,use_gpu=True):

 n_epochs = 0;
for epoch in range(epochs):
n_epochs = epoch
iters = len(train_dl)
 model.train() ;
for xb,yb in train_dl:
if use_gpu:
xb, yb = xb.cuda() , yb.cuda()
preds = model(xb)
 loss = loss_func(preds,yb)
 loss.backward()
sched_params(opt, n_epochs, epochs,sched_func)
 opt.step()
opt.zero_grad()
n_epochs += 1./iters
print(f"Epoch {epoch} completed")
 def sched_params(opt, n_epochs, epoch,sched_func):
for pg in opt.param_groups:
pg['lr'] = sched_func(n_epochs/epoch)
 def annealer(f):
def _inner(start, end): return partial(f, start, end)
 return _inner
@annealer
def sched_cos(start, end, pos): return start +(1+math.cos(math.pi*(1-pos)))*(end-start)/2
def combine_scheds(pcts, scheds):
assert sum(pcts)== 1
pcts = tensor([0]+ list(pcts))
assert torch.all(pcts>=0)
 pcts = torch.cumsum(pcts,0)
 def _inner(pos):
idx =(pos>=pcts ).nonzero().max()
actual_pos =(pos-pcts[idx])/(pcts[idx+1]-pcts[idx])
 return scheds[idx](actual_pos)
 return _inner
def normalize(x,m,s):
return(x-m)/s
def normalize_to(data, train):
mean, std = train.mean() , train.std()
return normalize(data, mean, std), normalize(train, mean, std)
 def get_normalized_data() :
X_train, y_train = get_data(path, 'train.csv')
 X_test, y_test = get_data(path,'test.csv')
 X_test, X_train = normalize_to(X_test, X_train)
 return X_train, y_train, X_test, y_test
def get_stats(x):
return f'mean :{x.mean() } , std : {x.std() }';
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
 def get_preds(model,dl):
model.eval()
preds = []
for xb,yb in dl:
xb,yb = xb.cuda() , yb.cuda()
pred= model(xb)
 preds += pred.cpu().detach()
return preds<choose_model_class>
model = lightgbm.LGBMRegressor()
model.fit(train_X, train_Y, eval_set=(valid_X, valid_Y), eval_metric='l1' )
class GeneralReLU(nn.Module):

 def __init__(self, leak=None, sub=None, maxv=None):
super().__init__()
self.leak, self.sub, self.maxv = leak, sub, maxv;
def forward(self, x):
x = F.leaky_relu(x, self.leak)if self.leak is not None else F.relu(x)
 if self.sub is not None: x.sub_(self.sub);
if self.maxv is not None: x.clamp_max_(self.maxv)
 return x<concatenate>
test = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/test_V2.csv' )
def conv_layer(f_in, f_out, ks, s, p):
return nn.Sequential(nn.Conv2d(f_in, f_out, kernel_size=ks, stride=s, padding=p,bias=False),
nn.BatchNorm2d(f_out),
GeneralReLU(sub=0.5)) 
<categorify>
test = test.drop(['swimDistance', 'headshotKills', 'killPoints', 'matchDuration', 'rankPoints', 'teamKills', 'winPoints'], axis=1)
 test = test.groupby('matchId')
 test_groupwise = test.apply(match_normalize )
class ResBlock(nn.Module):
def __init__(self, nf):
super().__init__()
self.nf = nf
self.conv1 = conv_layer(nf,nf,3,1,1)
 self.conv2 = conv_layer(nf,nf,3,1,1)
 def forward(self, X):
return X + self.conv2(self.conv1(X))<categorify>
predict_groupwise = model.predict(test_groupwise )
class DenseBlock(nn.Module):
def __init__(self, ni, nf):
super().__init__()
self.ni, self.nf = ni, nf
self.conv1 = conv_layer(ni, nf,3,1,1)
 self.conv2 = conv_layer(nf, nf,3,1,1)
 def forward(self, X):
return torch.cat([X,self.conv2(self.conv1(X)) ],dim=1 )<choose_model_class>
submission = pd.read_csv('/kaggle/input/pubg-finish-placement-prediction/sample_submission_V2.csv')
 submission = submission.astype({'winPlacePerc': 'float64'} )
layers = nn.Sequential(Lambda(mnist_resize),
conv_layer(1,8,5,1,2),
nn.Dropout2d(p=0.05),
ResBlock(8),
nn.Dropout2d(p=0.05),
nn.MaxPool2d(3,2,1),
DenseBlock(8,8),
nn.Dropout2d(p=0.05),
nn.MaxPool2d(3,2,1),
DenseBlock(16,16),
nn.Dropout2d(p=0.05),
nn.AdaptiveAvgPool2d(1),
Lambda(flatten),
nn.Linear(32,10),
nn.BatchNorm1d(10)
 )<compute_train_metric>
for test_matchId, test_indices in test.groups.items() :
loc = test_groupwise.index.get_loc(test_matchId)
 groups = [index[1] for index in test_groupwise.index.values[loc]]
t_order = np.array(groups)[predict_groupwise[loc].argsort() ].tolist()
p_orders = [kill[1]['groupId'].values[kill[1]['killPlace'].values.argsort() [::-1]].tolist() for kill in test.get_group(test_matchId ).groupby('kills')]
order = rearrange(t_order, p_orders)
 maxPlace = test.get_group(test_matchId ).iloc[0]['maxPlace']
numGroups = test.get_group(test_matchId ).iloc[0]['numGroups']
numVirtual = maxPlace - numGroups
for i in range(numVirtual):
order.insert(int(round(numGroups /(numVirtual + 1)*(i + 1)+ i)) , 'v')
 for i in range(len(test_indices)) :
submission.at[test_indices[i], 'winPlacePerc'] = order.index(test.get_group(test_matchId ).iloc[i]['groupId'])/ float(maxPlace - 1 )
X_train, y_train, X_test, y_test = get_normalized_data()
train_dl, valid_dl = get_dls(Dataset(X_train,y_train), Dataset(X_test,y_test))
model = get_model(layers=layers)
 opt = get_optimizer(model)
 loss_func = nn.CrossEntropyLoss()
init_cnn(model )<count_values>
submission.to_csv('submission.csv', index=False )
count_parameters(model )<train_model>
submission.to_csv('submission.csv', index=False )