Kaggle competition: Indoor Location & Navigation
<SOS> metric: IndoorLocalization Kaggle data source: indoor-location-&-navigation<load_from_csv>
Image(filename=".. /input/iln-dataset/mymodel-1.png",
format='png' )
ROOT_DIR = '/kaggle/input/titanic'
train_df = pd.read_csv(os.path.join(ROOT_DIR, 'train.csv'))
test_df = pd.read_csv(os.path.join(ROOT_DIR, 'test.csv'))
test_id = test_df['PassengerId']
print('Train shape: ', train_df.shape)
 print('Test shape: ', test_df.shape )<create_dataframe>
%pip install einops
def show_null_values(df):
null_df = pd.DataFrame(
 {'Column': [col for col in df.columns if df[col].isna().sum() ],
'Ratio': [df[col].isna().sum() /df.shape[0] for col in df.columns if df[col].isna().sum() ]}, 
)
 return null_df.sort_values(by=['Ratio'], ascending=False )<count_missing_values>
import numpy as np
import pandas as pd
import os, random, pickle, gc, time, json, math, copy
from tqdm import tqdm
from sklearn.model_selection import StratifiedKFold
from sklearn.preprocessing import LabelEncoder, StandardScaler
from matplotlib import pyplot as plt
import torch
from torch import nn, optim
from torch.nn.parameter import Parameter
from torch.nn.utils import rnn
from einops import rearrange
show_null_values(train_df )<count_missing_values>
def set_seed(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed )
show_null_values(test_df )<drop_column>
dat_ver = 'ILN_631dat'
model_ver = 'ILN_632'
subm_file = '.. /input/indoor-location-navigation/sample_submission.csv'
N_SPLITS = 10
NUM_FEATS = 40
SEED = 1605
batch_size = 128
inference_only = True
if inference_only:
n_epoch = 1
data_dir = '.. /input/iln-dataset/ILN_train_results/'
else:
n_epoch = 400
data_dir = ''
set_seed(SEED)
 ID_FEATS = [f'id_{i}' for i in range(NUM_FEATS)]
STRG_FEATS = [f'strength_{i}' for i in range(NUM_FEATS)]
IMU_FEATS = ['gyro_x_mean', 'gyro_y_mean', 'gyro_z_mean',
'gyro_x_std', 'gyro_y_std', 'gyro_z_std',
'gyro_x_max', 'gyro_y_max', 'gyro_z_max',
'gyro_x_min', 'gyro_y_min', 'gyro_z_min',
'gyro_x_skew', 'gyro_y_skew', 'gyro_z_skew',
'acce_x_mean', 'acce_y_mean', 'acce_z_mean',
'acce_x_std', 'acce_y_std', 'acce_z_std',
'acce_x_max', 'acce_y_max', 'acce_z_max',
'acce_x_min', 'acce_y_min', 'acce_z_min',
'acce_x_skew', 'acce_y_skew', 'acce_z_skew',
'ahrs_x_mean', 'ahrs_y_mean', 'ahrs_z_mean',
'ahrs_x_std', 'ahrs_y_std', 'ahrs_z_std',
'ahrs_x_max', 'ahrs_y_max', 'ahrs_z_max',
'ahrs_x_min', 'ahrs_y_min', 'ahrs_z_min',
'ahrs_x_skew', 'ahrs_y_skew', 'ahrs_z_skew',
'head_magn_x_mean', 'head_magn_y_mean',
'head_magn_x_std', 'head_magn_y_std',
'head_magn_x_max', 'head_magn_y_max',
'head_magn_x_min', 'head_magn_y_min',
'head_magn_x_skew', 'head_magn_y_skew',
'magn_z_mean', 'magn_z_std',
'magn_z_max', 'magn_z_min', 'magn_z_skew']
train_df.drop(columns=['Cabin'], inplace=True)
 test_df.drop(columns=['Cabin'], inplace=True )<define_variables>
floor_map = {"B2":-2, "B1":-1,
"F1":0, "F2":1, "F3":2, "F4":3, "F5":4,
"F6":5, "F7":6, "F8":7, "F9":8,
"1F":0, "2F":1, "3F":2, "4F":3, "5F":4,
"6F":5, "7F":6, "8F":7, "9F":8}
site_list = ['5d2709a003f801723c3251bf','5a0546857ecc773753327266',
'5c3c44b80379370013e0fd2b','5d2709b303f801723c327472',
'5d2709bb03f801723c32852c','5d2709c303f801723c3299ee',
'5d2709d403f801723c32bd39','5d2709e003f801723c32d896',
'5d27075f03f801723c2e360f','5d27096c03f801723c31e5e0',
'5d27097f03f801723c320d97','5d27099f03f801723c32511d',
'5da138b74db8ce0c98bd4774','5da958dd46f8266d0737457b',
'5da1382d4db8ce0c98bbe92e','5da1383b4db8ce0c98bc11ab',
'5da1389e4db8ce0c98bd0547','5da138274db8ce0c98bbd3d2',
'5da138314db8ce0c98bbf3a0','5da138364db8ce0c98bc00f1',
'5da138754db8ce0c98bca82f','5da138764db8ce0c98bcaa46',
'5dbc1d84c1eb61796cf7c010','5dc8cea7659e181adb076a3f']
train_size = train_df.shape[0]<prepare_x_and_y>
df_TestTimeLag = pd.read_csv('.. /input/iln-preprocess-time-lag-table-of-test-data/test_ts_lag.csv',index_col=0)
 with open(f'.. /input/iln-preprocess-create-dataset-public/{dat_ver}_df_wifi_all.pkl', 'rb')as f:
data = pickle.load(f )
y_train = train_df['Survived']
all_data = pd.concat([train_df.drop(columns=['Survived']), test_df] )<data_type_conversions>

 min_seq_len = 10
max_seq_len = 200
tmp = []
for gid, grp in tqdm(data.groupby('path'), ncols=60):
if grp.shape[0] > min_seq_len:
thresh = np.sort(grp['strength_0'].values)[-min_seq_len]
thresh = min([-100, thresh])
 grp = grp[grp['strength_0']>=thresh].copy()
grp['seq_label'] = grp['path'].str.cat(( np.arange(grp.shape[0])
 //max_seq_len ).astype(str),
sep='_')
 tmp.append(grp)
 data = pd.concat(tmp)
 data.reset_index(drop=True, inplace=True)
 del tmp; gc.collect()
print(f'total num of path data: {data.shape[0]}' )
all_data['Age'].fillna(train_df['Age'].median() , inplace=True )<data_type_conversions>
tslabel ='sys_ts'
data['site_path_timestamp'] =(data['site'] 
.str.cat(data['path'], sep='_'))
mask = data['train/test']=='train'
data.loc[ mask,'timestamp'] = data.loc[mask,tslabel]
data.loc[~mask,'timestamp'] =(data.loc[~mask,tslabel]
-df_TestTimeLag.loc[data.loc[~mask,'path']] 
.values.reshape(-1))
data['site_path_timestamp'] =(data['site_path_timestamp'] 
.str.cat(data['timestamp'] 
.astype('int64' ).astype(str)
.str.zfill(13), sep='_'))
all_data['Embarked'].fillna(train_df['Embarked'].mode() [0], inplace=True )<data_type_conversions>
OTHER_FEATS = [itm for itm in data.columns
if(itm[:3]!='id_')&(itm[:9]!='strength_')& 
(itm[-5:]!='_mean')&(itm[-4:]!='_std')& 
(itm[-4:]!='_max')&(itm[-4:]!='_min')& 
(itm[-5:]!='_skew')]
data = data[OTHER_FEATS+ID_FEATS+STRG_FEATS+IMU_FEATS]
all_data['Fare'].fillna(train_df['Fare'].mean() , inplace=True )<count_missing_values>

 le_id = LabelEncoder()
le_id.fit(ID_vocab)
 le_site = LabelEncoder()
le_site.fit(data['site'].unique())
 for i in ID_FEATS:
data.loc[:,i] = le_id.transform(data.loc[:,i])+1
data.loc[:, 'site'] = le_site.transform(data.loc[:, 'site'])
 data[ID_FEATS] *=(~(data[STRG_FEATS].isna() ).values)
 data[STRG_FEATS] = data[STRG_FEATS].fillna(-100)+100
show_null_values(all_data )<drop_column>

 data.sort_index(inplace=True)
 tmp = []
for _, gdf in tqdm(data.groupby('path',sort=False),
ncols=60):
arr = gdf[STRG_FEATS].values.flatten()
gdf[STRG_FEATS] =(gdf[STRG_FEATS]-arr.min())/(arr.max() -arr.min())
 tmp.append(gdf[STRG_FEATS])
 tmp = pd.concat(tmp)
 tmp.sort_index(inplace=True)
 data[STRG_FEATS] = tmp.values
del arr,tmp; gc.collect()
all_data.drop(columns=['Name', 'Ticket'], inplace=True)
 all_data.drop(columns=['PassengerId'], inplace=True )<categorify>
def replace_outlier(series, thresh=0.25, bias=1.5):
q1 = series.quantile(thresh)
 q3 = series.quantile(1-thresh)
 iqr = q3 - q1
outlier_min = q1 -(iqr)* bias
outlier_max = q3 +(iqr)* bias
series = series.clip(outlier_min, outlier_max)
 return series
for col in ['Sex', 'Embarked', 'Pclass']:
dummies = pd.get_dummies(all_data[col], prefix=col)
 all_data = pd.concat([all_data, dummies], axis=1)
 all_data.drop(columns=col, inplace=True )<prepare_x_and_y>

 data[['acce_z_mean','acce_z_max','acce_z_min']] = \
data[['acce_z_mean','acce_z_max','acce_z_min']]-9.80665
for sens in['gyro','acce','ahrs','magn']:
ss = StandardScaler()
if sens=='magn':
tmpFEATS = [sens+'_z_mean']
ss.fit(data[tmpFEATS].values.reshape(-1,1))
else:
tmpFEATS = [sens+'_x_mean', sens+'_y_mean', sens+'_z_mean']
ss.fit(np.tile(data[tmpFEATS].values.reshape(-1,1),(1,3)))
 data[tmpFEATS] = ss.transform(data[tmpFEATS])
 for itm in tmpFEATS:
data[itm]=replace_outlier(data[itm])
 ss = StandardScaler()
if sens=='magn':
tmpFEATS = [sens+'_z_std']
ss.fit(data[tmpFEATS].values.reshape(-1,1))
else:
tmpFEATS = [sens+'_x_std', sens+'_y_std', sens+'_z_std']
ss.fit(np.tile(data[tmpFEATS].values.reshape(-1,1),(1,3)))
 data[tmpFEATS] = ss.transform(data[tmpFEATS])
 for itm in tmpFEATS:
data[itm]=replace_outlier(data[itm])
 ss = StandardScaler()
if sens=='magn':
tmpFEATS = [sens+'_z_skew']
ss.fit(data[tmpFEATS].values.reshape(-1,1))
else:
tmpFEATS = [sens+'_x_skew', sens+'_y_skew', sens+'_z_skew']
ss.fit(np.tile(data[tmpFEATS].values.reshape(-1,1),(1,3)))
 data[tmpFEATS] = ss.transform(data[tmpFEATS])
 for itm in tmpFEATS:
data[itm]=replace_outlier(data[itm])
 ss = StandardScaler()
if sens=='magn':
tmpFEATS = [sens+'_z_max', sens+'_z_min']
ss.fit(np.tile(data[tmpFEATS].values.reshape(-1,1),(1,2)))
 else:
tmpFEATS = [sens+'_x_max', sens+'_x_min',
sens+'_y_max', sens+'_y_min',
sens+'_z_max', sens+'_z_min']
ss.fit(np.tile(data[tmpFEATS].values.reshape(-1,1),(1,6)))
 data[tmpFEATS] = ss.transform(data[tmpFEATS])
 for itm in tmpFEATS:
data[itm]=replace_outlier(data[itm] )
X_train = all_data[:train_size]
X_test = all_data[train_size:]<normalization>

 trvl_data = data[data['train/test']=='train'].copy()
test_data = data[data['train/test']=='test'].copy()
del data; gc.collect()
scaled_features = ['Age', 'Fare']
scaler = StandardScaler()
X_train_scaled = X_train.copy()
X_test_scaled = X_test.copy()
X_train_scaled[scaled_features] = scaler.fit_transform(X_train_scaled[scaled_features])
 X_test_scaled[scaled_features] = scaler.transform(X_test_scaled[scaled_features] )<train_on_grid>

 grb = trvl_data[['seq_label','site','floor']].groupby('seq_label')
 df_stra = grb.first()
df_stra['seq_len'] = grb['seq_label'].count().values
df_stra['seq_len_cut'] = pd.qcut(df_stra['seq_len'],4)
 df_stra['stratify'] = df_stra.apply(lambda x:
str(x['seq_len_cut'])+'_'+
str(x['site'])+'_'+
str(x['floor']),
axis=1)
 skf = StratifiedKFold(n_splits=N_SPLITS, shuffle=True,
random_state=SEED)
 for i,(_, val_idx)in enumerate(skf.split(df_stra['stratify'],
df_stra['stratify'])) :
tmp = np.zeros(df_stra.shape[0] ).astype(bool)
 tmp[val_idx] = True
df_stra[f'fold_{i}'] = tmp
df_stra.shape
logistic_regr = LogisticRegression(max_iter=500)
 param_grid = {'C': [0.01, 0.05, 0.1, 0.5], 'penalty': ['l1', 'l2']}
logistic_gcv = GridSearchCV(logistic_regr, param_grid=param_grid, scoring='accuracy', n_jobs=-1, cv=5, return_train_score=True)
 logistic_gcv.fit(X_train_scaled, y_train )<compute_train_metric>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
 print('using device:', device )
print('Logistic best params: ', logistic_gcv.best_params_)
 print('Logistic Regression best score: ', logistic_gcv.best_score_)
 print(logistic_gcv.cv_results_['mean_train_score'])
 print(logistic_gcv.cv_results_['mean_test_score'] )<train_on_grid>

 tr_id, tr_strg, tr_dlt, tr_imu, tr_site, tr_pos, tr_xy, tr_mask = \
[],[],[],[],[],[],[],[]
for gid, grp in tqdm(trvl_data.groupby('seq_label'), ncols=60):
tr_id.append(torch.from_numpy(grp[ID_FEATS].values ).long())
 tr_strg.append(torch.from_numpy(grp[STRG_FEATS].values ).float())
 tr_dlt.append(torch.from_numpy(grp[DELTA_FEATS].values ).float())
 tr_imu.append(torch.from_numpy(grp[IMU_FEATS].values ).float())
 tr_site.append(torch.from_numpy(grp['site'].values ).long())
 tr_pos.append(torch.from_numpy(grp['len_pos'].values ).float())
 tr_xy.append(torch.from_numpy(grp[['x','y']].values ).float())

tr_mask.append(tr_id[-1].sum(dim=-1)==0)
 tr_id = rnn.pad_sequence(tr_id, batch_first=True ).to(device)
 tr_strg = rnn.pad_sequence(tr_strg, batch_first=True ).to(device)
 tr_dlt = rnn.pad_sequence(tr_dlt, batch_first=True ).to(device)
 tr_imu = rnn.pad_sequence(tr_imu, batch_first=True ).to(device)
 tr_site = rnn.pad_sequence(tr_site, batch_first=True ).to(device)
 tr_pos = rnn.pad_sequence(tr_pos, batch_first=True ).to(device)
 tr_xy = rnn.pad_sequence(tr_xy, batch_first=True ).to(device)
 tr_mask = rnn.pad_sequence(tr_mask, batch_first=True,
padding_value=True ).to(device )
dt = DecisionTreeClassifier(random_state=2)
 dt_params = {'max_depth': [3, 5, 6],
}
dt_gcv = GridSearchCV(dt, param_grid=dt_params, scoring='accuracy', n_jobs=-1, cv=5, return_train_score=True)
 dt_gcv.fit(X_train, y_train )<find_best_params>
class ILNdatasets(torch.utils.data.Dataset):
def __init__(self, x_id, x_strg, x_dlt, x_imu, x_site, x_pos, x_mask,
y, train=True):
self.train = train
self.x_id = x_id
self.x_strg = x_strg
self.x_dlt = x_dlt
self.x_imu = x_imu
self.x_site = x_site
self.x_pos = x_pos
self.x_mask = x_mask
self.y = y
self.datanum = len(y)
 def __len__(self):
return self.datanum
def __getitem__(self, idx):
return(self.x_id[idx], self.x_strg[idx], self.x_dlt[idx], self.x_imu[idx],
self.x_site[idx], self.x_pos[idx], self.x_mask[idx],
self.y[idx] )
print('Decision Tree best params: ', dt_gcv.best_params_)
 print('Decision Tree best score: ', dt_gcv.best_score_)
 print(dt_gcv.cv_results_['mean_train_score'])
 print(dt_gcv.cv_results_['mean_test_score'])
 print(dt_gcv.cv_results_['std_train_score'])
 print(dt_gcv.cv_results_['std_test_score'] )<train_on_grid>
class PositionalEncoding(nn.Module):
def __init__(self, d_model, dropout=0.1):
super(PositionalEncoding, self ).__init__()
self.dropout = nn.Dropout(p=dropout)
 self.div_term = torch.exp(torch.arange(0, d_model, 2 ).float() *(-math.log(10000.0)/ d_model))
def forward(self, x, position, mask):
if position is None:
position = torch.cumsum(mask.logical_not() , dim=1)-1
else:
position *= mask.logical_not()
position = rearrange(position, 'bn seq -> bn seq 1')
 pe = torch.zeros(x.shape ).to(x.device)
 div_term = self.div_term.to(x.device)
 pe[:,:,0::2] = torch.sin(position * div_term)
 pe[:,:,1::2] = torch.cos(position * div_term)
 x = x + pe
return self.dropout(x )
svm = SVC()
svm_params = {'C': [0.01, 0.05, 0.1, 1]}
svm_gcv = GridSearchCV(svm, param_grid=svm_params, scoring='accuracy', n_jobs=-1, cv=5, return_train_score=True)
 svm_gcv.fit(X_train_scaled, y_train )<compute_train_metric>

 def clones(module, N):
"Produce N identical layers."
return nn.ModuleList([copy.deepcopy(module)for _ in range(N)])
 def attention(query, key, value, key_padding_mask=None, attn_weight=None, dropout=None):
"Compute 'Scaled Dot Product Attention'"
d_k = query.size(-1)
 scores = torch.matmul(query, key.transpose(-2, -1)) \
/ math.sqrt(d_k)
 if key_padding_mask is not None:
scores = scores.masked_fill(key_padding_mask.unsqueeze(1 ).unsqueeze(2), -1e9)
 if attn_weight is not None:
if len(attn_weight.shape)!= len(scores.shape):
attn_weight = attn_weight.unsqueeze(-3)
 scores -= attn_weight
p_attn = nn.functional.softmax(scores, dim=-1)
 if dropout is not None:
p_attn = dropout(p_attn)
 return torch.matmul(p_attn, value), p_attn
class MultiHeadedAttention(nn.Module):
def __init__(self, d_model, nhead, dropout=0.1):
"Take in model size and number of heads."
super(MultiHeadedAttention, self ).__init__()
assert d_model % nhead == 0
self.d_k = d_model // nhead
self.nhead = nhead
self.linears = clones(nn.Linear(d_model, d_model, bias=False), 4)
 self.attn = None
self.dropout = nn.Dropout(p=dropout)
 def forward(self, query, key, value, key_padding_mask=None, attn_weight=None):
"Implements Figure 2"
nbatches = query.size(0)
 query, key, value = \
[l(x ).view(nbatches, -1, self.nhead, self.d_k ).transpose(1, 2)
 for l, x in zip(self.linears,(query, key, value)) ]
x, self.attn = attention(query, key, value, key_padding_mask=key_padding_mask,
attn_weight=attn_weight,
dropout=self.dropout)
 x = x.transpose(1, 2 ).contiguous() \ 
.view(nbatches, -1, self.nhead * self.d_k)
 return self.linears[-1](x)
 class PositionwiseFeedForward(nn.Module):
"Implements FFN equation."
def __init__(self, d_model, d_ff, dropout=0.1):
super(PositionwiseFeedForward, self ).__init__()
self.w_1 = nn.Linear(d_model, d_ff)
 self.w_2 = nn.Linear(d_ff, d_model)
 self.dropout = nn.Dropout(dropout)
 def forward(self, x):
return self.w_2(self.dropout(nn.functional.relu(self.w_1(x))))
class CustomEncoderLayer(nn.Module):

 def __init__(self, d_model, nhead, dim_feedforward = 1024, dropout = 0.1):
super().__init__()
self._self_attn = MultiHeadedAttention(d_model, nhead, dropout)
 self._ffn = PositionwiseFeedForward(d_model, dim_feedforward, dropout)
 self._layernorms = clones(nn.LayerNorm(d_model, eps=1e-6), 2)
 self._dropout = nn.Dropout(dropout)
 def forward(self, src, key_padding_mask = None, attn_weight = None):

 src2 = self._self_attn(query=src, key=src, value=src, key_padding_mask=key_padding_mask,
attn_weight=attn_weight)
 src = src + self._dropout(src2)
 src = self._layernorms[0](src)
 src2 = self._ffn(src)
 src = src + self._dropout(src2)
 src = self._layernorms[1](src)
 return src
print('SVM best params: ', svm_gcv.best_params_)
 print('SVM best score: ', svm_gcv.best_score_)
 print(svm_gcv.cv_results_['mean_train_score'])
 print(svm_gcv.cv_results_['mean_test_score'] )<train_on_grid>

 class ILNnet(nn.Module):
def __init__(self, input_dim, id_num_embd, id_embd_dim,
strg_dim, site_num_embd, imu_dim,
ninp, nhead, nhid, nlayers, dropout):
super(ILNnet, self ).__init__()

 self.id_embd = nn.Embedding(id_num_embd, id_embd_dim,
padding_idx=0)
 self.strg_ln = nn.LayerNorm(input_dim)
 self.strg_lin = nn.Linear(input_dim, strg_dim)
 self.site_embd = nn.Embedding(site_num_embd, ninp)
 cat_dim = input_dim*id_embd_dim + strg_dim
self.main_seq1 = nn.Sequential(nn.Linear(cat_dim, ninp),
nn.LayerNorm(ninp),
nn.ReLU() ,
nn.Dropout(dropout))
self.dltimu_lin = nn.Linear(4+imu_dim, ninp)

self.pos_encoder = PositionalEncoding(ninp, dropout)
 self.attn_wt_coef = nn.Parameter(torch.clamp(torch.randn(nhead), min=1e-3))
self.customTR1 = CustomEncoderLayer(ninp, nhead, nhid, dropout)
 self.customTR2 = CustomEncoderLayer(ninp, nhead, nhid, dropout)

self.lstm1 = nn.LSTM(batch_first=True,
bidirectional=True, num_layers=1,
input_size=ninp, hidden_size=ninp//2)
 self.lstm2 = nn.LSTM(batch_first=True,
bidirectional=True, num_layers=1,
input_size=ninp, hidden_size=ninp//2)
 self.main_seq2 = nn.Sequential(nn.Linear(ninp,ninp),
nn.LayerNorm(ninp),
nn.Dropout(dropout),
nn.Linear(ninp,2))
self.init_weights()
def init_weights(self):
self.id_embd.weight.data.zero_()
self.site_embd.weight.data.zero_()
def forward(self, x_id, x_strg, x_dlt, x_imu, x_site, x_pos, x_mask):

 x_id = self.id_embd(x_id)
 x_id = rearrange(x_id, 'bn seq d1 d2 -> bn seq(d1 d2)')
 x_strg = self.strg_ln(x_strg)
 x_strg = torch.relu(self.strg_lin(x_strg))
x_site = self.site_embd(x_site)
 x = torch.cat(( x_id, x_strg), dim=-1)
 x = self.main_seq1(x)+ x_site

 x_dltimu = torch.cat(( x_dlt, x_imu),-1)
 x_dltimu = torch.relu(self.dltimu_lin(x_dltimu))
x = x + x_dltimu

 xt = self.pos_encoder(x, None, x_mask)
 attn_wt =(rearrange(x_pos, 'bn seq -> bn 1 seq 1')
 -rearrange(x_pos, 'bn seq -> bn 1 1 seq')).abs()
attn_coef = rearrange(self.attn_wt_coef, 'nh -> 1 nh 1 1')
 xt = self.customTR1(xt, x_mask, torch.log(1.0 + attn_wt)* attn_coef)
 xt = self.customTR2(xt, x_mask, torch.log(1.0 + attn_wt)* attn_coef)
 x = x + xt

 lengths = x_mask.logical_not().sum(dim=1 ).to('cpu')
 x = rnn.pack_padded_sequence(x, lengths, batch_first=True,
enforce_sorted=False)
 x,_ = self.lstm1(x)
 x,_ = self.lstm2(x)
 x = rnn.pad_packed_sequence(x, batch_first=True)[0]
if x.size(1)!=x_mask.size(1):
pad_len = x_mask.size(1)-x.size(1)
 x = torch.cat([x, torch.zeros(x.size(0),pad_len,x.size(2)).to(device)],
dim=1)
 assert x.size(1)==x_mask.size(1)
 x = self.main_seq2(x)
 return x
gb = GradientBoostingClassifier()
gb_params = {'n_estimators': [30, 50, 60, 100]}
gb_gcv = GridSearchCV(gb, param_grid=gb_params, scoring='accuracy', n_jobs=-1, cv=5, return_train_score=True)
 gb_gcv.fit(X_train_scaled, y_train )<compute_train_metric>
def ILN_loss(output, filter_, delta, label):
metr1 =(output[:,:,:2]-label[:,:,:2] ).square()
metr1 = metr1.sum(dim=-1)+1e-6
metr1 = metr1.sqrt().sum()
metric = metr1/filter_.sum()
loss = metric
return loss, metric
print('Gradient Boosting best params: ', gb_gcv.best_params_)
 print('Gradient Boosting best score: ', gb_gcv.best_score_)
 print(gb_gcv.cv_results_['mean_train_score'])
 print(gb_gcv.cv_results_['mean_test_score'] )<predict_on_test>
class LearningRateScheduler:
def __init__(self, lr:list,
switch_epoch: list):
self.lr = lr
self.switch_epoch = switch_epoch
def __call__(self, epoch:int):
idx = [i>epoch for i
in self.switch_epoch+[1e9]].index(True)
 return self.lr[idx]
gb = GradientBoostingClassifier(n_estimators=50)
 gb.fit(X_train_scaled, y_train)
 predict = gb.predict(X_test_scaled )<save_to_csv>
def train_val(net, trainloader, valloader, bsz):

 loss_sum, metric_sum = 0., 0.
 net.train()
optimizer.zero_grad()
with tqdm(trainloader, ncols=80)as pbar:
prefix = f"epoch {i_epoch+1} train"
pbar.set_description(prefix)
 descpost = None
for i,(tr_id, tr_strg, tr_dlt, tr_imu, tr_site, tr_pos,
tr_mask, tr_xy)in enumerate(pbar):
output = net(tr_id, tr_strg, tr_dlt, tr_imu, tr_site, tr_pos, tr_mask)
 filter_ = tr_mask.logical_not()
output *= filter_.unsqueeze(-1)
 loss, metric = ILN_loss(output, filter_, tr_dlt, tr_xy)
 loss.backward()
optimizer.step()
optimizer.zero_grad()
loss_sum += loss.item()
metric_sum += metric.item()
descpost = f'{loss_sum/(i+1):.2f}/{metric_sum/(i+1):.2f}'
pbar.set_postfix({"l/m":descpost})
 history['epoch'].append(i_epoch+1)
 history['train_loss'].append(loss_sum/(i+1))
history['train_metric'].append(metric_sum/(i+1))

 with torch.no_grad() :
loss_sum, metric_sum = 0., 0.
 net.eval()
with tqdm(valloader, ncols=80)as pbar:
prefix = ' '*(len(prefix)-10)+'validation'
pbar.set_description(prefix)
 descpost = None
for i,(vl_id, vl_strg, vl_dlt, vl_imu, vl_site, vl_pos, vl_mask, vl_xy)\
in enumerate(pbar):
output = net(vl_id, vl_strg, vl_dlt, vl_imu, vl_site, vl_pos, vl_mask)
 filter_ = vl_mask.logical_not()
output *= filter_.unsqueeze(-1)
 loss, metric = ILN_loss(output, filter_, vl_dlt, vl_xy)
 loss_sum += loss.item()
metric_sum += metric.item()
descpost = f'{loss_sum/(i+1):.2f}/{metric_sum/(i+1):.2f}'
pbar.set_postfix({"l/m":descpost})
 history['val_loss'].append(loss_sum/(i+1))
history['val_metric'].append(metric_sum/(i+1))
lr_scheduler.step()
return net, history['val_metric'][-1]
<load_from_csv><EOS> 

 te_id, te_strg, te_dlt, te_imu, te_site, te_pos, te_mask = \
[],[],[],[],[],[],[]
for gid, grp in tqdm(test_data.groupby('seq_label'), ncols=60):
te_id.append(torch.from_numpy(grp[ID_FEATS].values ).long())
 te_strg.append(torch.from_numpy(grp[STRG_FEATS].values ).float())
 te_dlt.append(torch.from_numpy(grp[DELTA_FEATS].values ).float())
 te_imu.append(torch.from_numpy(grp[IMU_FEATS].values ).float())
 te_site.append(torch.from_numpy(grp['site'].values ).long())
 te_pos.append(torch.from_numpy(grp['len_pos'].values ).float())

te_mask.append(te_id[-1].sum(dim=-1)==0)
 te_id = rnn.pad_sequence(te_id, batch_first=True ).to(device)
 te_strg = rnn.pad_sequence(te_strg, batch_first=True ).to(device)
 te_dlt = rnn.pad_sequence(te_dlt, batch_first=True ).to(device)
 te_imu = rnn.pad_sequence(te_imu, batch_first=True ).to(device)
 te_site = rnn.pad_sequence(te_site, batch_first=True ).to(device)
 te_pos = rnn.pad_sequence(te_pos, batch_first=True ).to(device)
 te_mask = rnn.pad_sequence(te_mask, batch_first=True,
padding_value=True ).to(device )