Kaggle competition: CareerCon 2019 - Help Navigate Robots
<SOS> metric: categorizationaccuracy Kaggle data source: career-con-2019<predict_on_test>
from collections import defaultdict
from functools import partial
from multiprocessing import cpu_count
from pathlib import Path
from textwrap import dedent
test_preds = lgb_first.predict(x_test)
 test_first_preds_df = pd.DataFrame(list(zip(seg_id_test, test_preds)))
 test_first_preds_df.columns = ['segment_id', 'time_to_eruption']
test_first_preds_df = test_first_preds_df.groupby('segment_id' ).mean().reset_index()<define_variables>
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.externals import joblib
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import LabelEncoder, StandardScaler
import torch
from torch import nn
from torch import optim
from torch.nn import functional as F
from torch.optim.lr_scheduler import _LRScheduler
from torch.utils.data import TensorDataset, DataLoader
steps = [(0, 2500000),(0, 15000000),(10000000, 25000000), 
(20000000, 35000000),(30000000, 50000000),(44000000, 50000000)]
output_list, model_list = [], []
y_val_list, val_preds_list = [], []<init_hyperparams>
seed = 1
np.random.seed(seed )
params = {'application':'regression',
'boosting ': 'dart',
'num_iterations':8000,
'learning_rate':0.05,
'num_leaves': 95,
'extra_trees': True,
'feature_fraction':0.8,
'bagging_fraction':0.9,
'lambda_l1':0.1,
'lambda_l2':0.1,
'min_split_gain':0.01,
'early_stopping_round':100,
'max_depth': 7,
'min_child_weight':40,
'n_estimators': 400,
'metric':'mae',
'verbosity': -1}<split>
sample = Path.cwd().parent/'input'/'career-con-2019'/'sample_submission.csv'
for l, u in steps:
_x, _y = get_dataset_by_range(train_df, labels_df, l, u)
 _x = _x[features]
_x = pd.DataFrame(scaler.transform(_x))
_x.columns = features
_x_train, _x_val, _y_train, _y_val = train_test_split(_x, _y, random_state=786, test_size=0.2, shuffle=False)
 _lgb = LGBMRegressor(**params)
 _lgb.fit(_x_train, _y_train, eval_set=(_x_val, _y_val))
_val_preds = _lgb.predict(_x_val)
 _output = pd.DataFrame(list(zip(_y_val, _val_preds)))
 _output.columns = ['val', 'pred']
_output = _output.groupby('val' ).median().reset_index()
_output['diff'] = _output['val'] - _output['pred']
model_list.append(_lgb)
 output_list.append(_output )<create_dataframe>
ROOT = Path.cwd().parent/'input'/'career-con-2019-preprocessed-data'
enc = joblib.load(ROOT/'encoder.model')
 raw_arr = np.load(ROOT/'feat.npy' ).transpose(0, 2, 1)
 fft_arr = np.load(ROOT/'feat_fft.npy' ).transpose(0, 2, 1)
 target = np.load(ROOT/'target.npy')
 print(dedent(f))
_mae_list = []
for idx, _output in enumerate(output_list):
l, h = steps[idx]
_mae = mae(_output['val'].to_numpy() , _output['pred'].to_numpy())
 _range = '%s-%s' %(l, h)
 _mae_list.append(( _range, _mae))
new_mae_df = pd.DataFrame(_mae_list, columns=['range', 'mae'] )<merge>
def create_datasets(data, target, train_size, valid_pct=0.1, seed=None):

 raw, fft = data
assert len(raw)== len(fft)
 sz = train_size
idx = np.arange(sz)
 trn_idx, val_idx = train_test_split(
 idx, test_size=valid_pct, random_state=seed)
 trn_ds = TensorDataset(
 torch.tensor(raw[:sz][trn_idx] ).float() ,
torch.tensor(fft[:sz][trn_idx] ).float() ,
torch.tensor(target[:sz][trn_idx] ).long())
 val_ds = TensorDataset(
 torch.tensor(raw[:sz][val_idx] ).float() ,
torch.tensor(fft[:sz][val_idx] ).float() ,
torch.tensor(target[:sz][val_idx] ).long())
 tst_ds = TensorDataset(
 torch.tensor(raw[sz:] ).float() ,
torch.tensor(fft[sz:] ).float() ,
torch.tensor(target[sz:] ).long())
 return trn_ds, val_ds, tst_ds
test_pred_df = pd.merge(test_df, test_first_preds_df, on='segment_id' )<define_variables>
def create_loaders(data, bs=128, jobs=0):

 trn_ds, val_ds, tst_ds = data
trn_dl = DataLoader(trn_ds, batch_size=bs, shuffle=True, num_workers=jobs)
 val_dl = DataLoader(val_ds, batch_size=bs, shuffle=False, num_workers=jobs)
 tst_dl = DataLoader(tst_ds, batch_size=bs, shuffle=False, num_workers=jobs)
 return trn_dl, val_dl, tst_dl
segment_id, time_to_eruption = [], []<drop_column>
class _SepConv1d(nn.Module):

 def __init__(self, ni, no, kernel, stride, pad):
super().__init__()
self.depthwise = nn.Conv1d(ni, ni, kernel, stride, padding=pad, groups=ni)
 self.pointwise = nn.Conv1d(ni, no, kernel_size=1)
 def forward(self, x):
return self.pointwise(self.depthwise(x))
for idx, value in enumerate(steps):
l, h = value
if l == 0:
l = -50000000
if h == 50000000:
h = 100000000
_test_df = test_pred_df[(test_pred_df['time_to_eruption'] >= l)& 
(test_pred_df['time_to_eruption'] <= h)]
_test_id = _test_df['segment_id']
_x_test = _test_df.drop(['segment_id', 'time_to_eruption'], axis=1)
 _x_test = pd.DataFrame(scaler.transform(_x_test))
_x_test.columns = features
_preds = model_list[idx].predict(_x_test)
 segment_id += list(_test_id)
 time_to_eruption += list(_preds )<create_dataframe>
class SepConv1d(nn.Module):

 def __init__(self, ni, no, kernel, stride, pad, drop=None,
activ=lambda: nn.ReLU(inplace=True)) :
super().__init__()
assert drop is None or(0.0 < drop < 1.0)
 layers = [_SepConv1d(ni, no, kernel, stride, pad)]
if activ:
layers.append(activ())
 if drop is not None:
layers.append(nn.Dropout(drop))
self.layers = nn.Sequential(*layers)
 def forward(self, x):
return self.layers(x )
kaggle_submit = pd.DataFrame(list(zip(segment_id, time_to_eruption)))
 kaggle_submit.columns = ['segment_id', 'time_to_eruption']
kaggle_submit = kaggle_submit.groupby('segment_id' ).median().reset_index()<define_variables>
class Flatten(nn.Module):

 def __init__(self, keep_batch_dim=True):
super().__init__()
self.keep_batch_dim = keep_batch_dim
def forward(self, x):
if self.keep_batch_dim:
return x.view(x.size(0), -1)
 return x.view(-1 )
assert len(kaggle_submit)== 4520
assert kaggle_submit['segment_id'].dtypes == 'int64'
assert kaggle_submit['time_to_eruption'].dtypes == 'float64'<save_to_csv>
class Classifier(nn.Module):
def __init__(self, raw_ni, fft_ni, no, drop=.5):
super().__init__()
self.raw = nn.Sequential(
 SepConv1d(raw_ni, 32, 8, 2, 3, drop=drop),
SepConv1d(32, 64, 8, 4, 2, drop=drop),
SepConv1d(64, 128, 8, 4, 2, drop=drop),
SepConv1d(128, 256, 8, 4, 2),
Flatten() ,
nn.Dropout(drop), nn.Linear(256, 64), nn.ReLU(inplace=True),
nn.Dropout(drop), nn.Linear(64, 64), nn.ReLU(inplace=True))
self.fft = nn.Sequential(
 SepConv1d(fft_ni, 32, 8, 2, 4, drop=drop),
SepConv1d(32, 64, 8, 2, 4, drop=drop),
SepConv1d(64, 128, 8, 4, 4, drop=drop),
SepConv1d(128, 128, 8, 4, 4, drop=drop),
SepConv1d(128, 256, 8, 2, 3),
Flatten() ,
nn.Dropout(drop), nn.Linear(256, 64), nn.ReLU(inplace=True),
nn.Dropout(drop), nn.Linear(64, 64), nn.ReLU(inplace=True))
self.out = nn.Sequential(
 nn.Linear(128, 64), nn.ReLU(inplace=True), nn.Linear(64, no))
def forward(self, t_raw, t_fft):
raw_out = self.raw(t_raw)
 fft_out = self.fft(t_fft)
 t_in = torch.cat([raw_out, fft_out], dim=1)
 out = self.out(t_in)
 return out
kaggle_submit.to_csv('./submission.csv', index=False )<choose_model_class>
trn_sz = 3810
ver = 27<set_options>
datasets = create_datasets(( raw_arr, fft_arr), target, trn_sz, seed=seed )
plt.style.use('ggplot')
 def get_df_name(df):
name =[x for x in globals() if globals() [x] is df][0]
return name
def tstats(t, doplot = False):
print('
for _,i in enumerate(t):
nulls = t[i].isna().sum()
if nulls > 0:
print(i,'=', t[i].nunique() , ',NULLS = ', nulls, ',% of nulls = ',round(100*nulls/t.shape[0]))
else:
print(i,'=', t[i].nunique())
 if doplot:
print()
print(t.sample(10))
print()
warnings.filterwarnings('ignore' )<load_from_csv>
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu' )
homedir = '/kaggle/input/predict-volcanic-eruptions-ingv-oe/'
def read_n_files(dirname, nfiles):
tmp = pd.DataFrame()
cnt = 0
for dirname, _, filenames in os.walk(homedir + dirname):
for filename in filenames:
cnt += 1
if cnt > nfiles: break
t = pd.read_csv(os.path.join(dirname, filename))
t['segment_id'] = filename[:-4]
tmp = pd.concat([tmp, t], ignore_index=True)
 return tmp
t10 = read_n_files('train', 10 ).fillna(0)
 float_cols = [c for c in t10 if t10[c].dtype == "float64"]
float32_cols = {c: np.float32 for c in float_cols}
nparts = 6
r = [int(j*30000/nparts)for j in range(nparts+1)]
def get_peaks(tab, col):
N = tab.shape[0]
T = 1.0 / 800.0
x = np.linspace(0.0, N*T, N)
 y = tab[col].values
yf = scipy.fftpack.fft(y)
 xf = np.linspace(0.0, int(1.0/(2.0*T)) , int(N/2))
x = np.log(2.0/N * np.abs(yf[:N//2]))
peaks, _ = find_peaks(x, distance=500)
 return x, peaks
def read_n_files32(dirname, nfiles):
tmp_res = pd.DataFrame()
cnt = 0
for dirname, _, filenames in os.walk(homedir + dirname):
for filename in tqdm_notebook(filenames):
cnt += 1
if cnt > nfiles: break
t = pd.read_csv(os.path.join(dirname, filename), engine='c', dtype=float32_cols)
 res = []
for i in [i for i in range(1,11)]:
x, peaks = get_peaks(t, 'sensor_'+str(i))
for j in range(len(r)-1):
xpeaks = peaks[(peaks > r[j])&(peaks <= r[j+1])]
ypeaks = x[xpeaks]
mx = round(np.median(xpeaks))
sx = round(np.std(xpeaks))
my = round(np.median(ypeaks), 2)
 sy = round(np.std(ypeaks), 2)
 r0, r1 = r[j], r[j+1]
res.append([f's{i:02d}_r{r0:05d}_{r1}_mx', mx])
 res.append([f's{i:02d}_r{r0:05d}_{r1}_sx', sx])
 res.append([f's{i:02d}_r{r0:05d}_{r1}_my', my])
 res.append([f's{i:02d}_r{r0:05d}_{r1}_sy', sy])
 df1 = pd.DataFrame({'ind':peaks, 'x':x[peaks]})
 df2 = pd.DataFrame({'ind':np.linspace(0,30000,30001 ).astype(int)})
 df = df2.merge(df1, how='left', on='ind')
 df = df.interpolate(method='linear', limit_direction='both', axis=0)
 data = df['x'].values
data[0] = data[:5000].min()
peaks, _ = find_peaks(data)
 prominences = peak_prominences(data, peaks)[0]
contour_heights = data[peaks] - prominences
results_half = peak_widths(data, peaks, rel_height=0.5)
 results_half[0]
results_full = peak_widths(data, peaks, rel_height=1)
 results_full[0]
peak_data = pd.DataFrame({
'peak_x':peaks,
'peak_y':data[peaks],
'width':results_full[0],
'height':prominences
} ).sort_values(by='height', ascending=False)
 tmp = peak_data.reset_index(drop=True ).reset_index().head(5)
 tmp['index'] = tmp['index'].astype(str ).apply(lambda x: x.zfill(2))
tmp = tmp.melt(id_vars=['index'], value_vars=['peak_x', 'peak_y', 'width', 'height'])
 tmp['variable'] = tmp['variable'] + tmp['index']
tmp = np.array(tmp[['variable', 'value']])
 for titem in tmp:
res.append([f's{i:02d}p_' + titem[0], titem[1]])
 t = pd.DataFrame(np.array(res), columns=['col', 'val'])
 t['segment_id'] = filename[:-4]
t['val'] = t['val'].astype(float)
 t = t.fillna(0)
 t = t.pivot_table(
 index='segment_id',
columns='col',
values='val'
).reset_index()
tmp_res = pd.concat([tmp_res, t], ignore_index=True)
 return tmp_res.fillna(0)
 def read_all_files(dirname):
tmp = pd.DataFrame()
cnt = 0
for dirname, _, filenames in os.walk(homedir + dirname):
for filename in filenames:
cnt += 1
t = pd.read_csv(os.path.join(dirname, filename)).fillna(0)
 cols = t.columns
t['segment_id'] = filename[:-4]
t = t.groupby('segment_id', as_index=False ).median()
tmp = pd.concat([tmp, t], ignore_index=True)
 return tmp<load_pretrained>
raw_feat = raw_arr.shape[1]
fft_feat = fft_arr.shape[1]
trn_dl, val_dl, tst_dl = create_loaders(datasets, bs=256)
 lr = 0.001
n_epochs = 3000
iterations_per_epoch = len(trn_dl)
 num_classes = 9
best_acc = 0
patience, trials = 500, 0
base = 1
step = 2
loss_history = []
acc_history = []
model = Classifier(raw_feat, fft_feat, num_classes ).to(device)
 criterion = nn.CrossEntropyLoss(reduction='sum')
 opt = optim.Adam(model.parameters() , lr=lr)
 print('Start model training')
 for epoch in range(1, n_epochs + 1):
model.train()
epoch_loss = 0
for i, batch in enumerate(trn_dl):
x_raw, x_fft, y_batch = [t.to(device)for t in batch]
opt.zero_grad()
out = model(x_raw, x_fft)
 loss = criterion(out, y_batch)
 epoch_loss += loss.item()
loss.backward()
opt.step()
epoch_loss /= trn_sz
loss_history.append(epoch_loss)
 model.eval()
correct, total = 0, 0
for batch in val_dl:
x_raw, x_fft, y_batch = [t.to(device)for t in batch]
out = model(x_raw, x_fft)
 preds = F.log_softmax(out, dim=1 ).argmax(dim=1)
 total += y_batch.size(0)
 correct +=(preds == y_batch ).sum().item()
acc = correct / total
acc_history.append(acc)
 if epoch % base == 0:
print(f'Epoch: {epoch:3d}.Loss: {epoch_loss:.4f}.Acc.: {acc:2.2%}')
 base *= step
if acc > best_acc:
trials = 0
best_acc = acc
torch.save(model.state_dict() , 'best.pth')
 print(f'Epoch {epoch} best model saved with accuracy: {best_acc:2.2%}')
 else:
trials += 1
if trials >= patience:
print(f'Early stopping on epoch {epoch}')
 break
print('Done!' )
tmp = read_n_files32('train', 10 )<compute_test_metric>
def smooth(y, box_pts):
box = np.ones(box_pts)/box_pts
y_smooth = np.convolve(y, box, mode='same')
 return y_smooth
tstats(tmp,1 )<data_type_conversions>
test_results = []
model.load_state_dict(torch.load('best.pth'))
model.eval()
for x_raw, x_fft, _ in tst_dl:
batches = [t.to(device)for t in(x_raw, x_fft)]
out = model(*batches)
 y_hat = F.log_softmax(out, dim=1 ).argmax(dim=1)
 test_results.extend(y_hat.tolist() )
<load_from_csv>
submit = pd.read_csv(sample)
 submit['surface'] = enc.inverse_transform(test_results)
 submit.to_csv('submit_base.csv', index=None )
def prepare_data() :
t4m = pd.read_csv('/kaggle/input/volcanic-eruptions/t4m_10.csv')
 t4p = pd.read_csv('/kaggle/input/volcanic-eruptions/t4p_10.csv')
 features = t4m.columns[1:-1]
target = 'time_to_eruption'
df = t4m[t4m.columns[1:]].copy()
t = df.corr() [target].reset_index()
t[target] = abs(t[target])
 t.sort_values(by=target, ascending = False)
 t = t.loc[(t[target] > 0.05)&(t[target] < 1)].sort_values(by=target, ascending=False ).reset_index()
good_features = t['index'].values
scaler = StandardScaler()
t = pd.DataFrame(scaler.fit_transform(t4m[good_features]), columns = good_features)
 t['sum_feat'] = t[good_features].sum(axis=1)
 t['mul_feat'] = t[good_features].prod(axis=1)
 t['s_m_feat'] = t['sum_feat'] * t['mul_feat']
t4m['sum_feat'] = t['sum_feat'].values
t4m['mul_feat'] = t['mul_feat'].values
t4m['s_m_feat'] = t['s_m_feat'].values
t = pd.DataFrame(scaler.fit_transform(t4p[good_features]), columns = good_features)
 t['sum_feat'] = t[good_features].sum(axis=1)
 t['mul_feat'] = t[good_features].prod(axis=1)
 t['s_m_feat'] = t['sum_feat'] * t['mul_feat']
t4p['sum_feat'] = t['sum_feat'].values
t4p['mul_feat'] = t['mul_feat'].values
t4p['s_m_feat'] = t['s_m_feat'].values
feat = list(features)
 feat.append('sum_feat')
 feat.append('mul_feat')
 feat.append('s_m_feat')
 X, y = t4m[feat], t4m[target]
data = y.values.reshape(-1, 1)
 rng = np.random.RandomState(304)
 qt = QuantileTransformer(n_quantiles=1000, output_distribution='normal',
random_state=rng)
 z = qt.fit_transform(data)
 z_back = qt.inverse_transform(z)
 plt.figure(figsize=(15,5))
plt.subplot(131)
 plt.hist(data)
 plt.title('y')
 plt.subplot(132)
 plt.hist(z, bins = 50)
 plt.title('z')
 plt.subplot(133)
 plt.hist(z_back, bins = 50)
 plt.title('inverse')
 plt.show()
return t4m, t4p, X, y, z, feat, target, qt
t4m, t4p, X, y, z, feat, target, qt = prepare_data()
def de_qt(val):
return qt.inverse_transform(val.reshape(-1, 1)).ravel()<prepare_x_and_y>
def cosine(epoch, t_max, ampl):

 t = epoch % t_max
return(1 + np.cos(np.pi*t/t_max)) *ampl/2
%%time
scaler = StandardScaler()
nval = 1000
ntest = 500
t = X.copy()
t[target] = z
t = t.sample(frac=1)
 t_init = t.copy()
t[feat] = scaler.fit_transform(t[feat])
 t1 = t[:nval]
t2 = t[nval:nval+ntest]
t3 = t[nval+ntest:]
X_train, y_train = t1[feat], t1[target]
X_val, y_val = t2[feat], t2[target]
X_test, y_test = t3[feat], t3[target]
model = Sequential()
model.add(Dense(128, input_shape=(X.shape[1],), activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(16, activation='relu', kernel_regularizer= kreg.l1(0.01)))
 model.add(Dense(1))
model.compile(loss='mse', optimizer='rmsprop', metrics=['mae'])
 history = model.fit(X_train, y_train, epochs=70, batch_size=4, verbose=False,
validation_data =(X_val, y_val))
plot_history(history, 'mae')
 t = t_init.copy()
t1 = t[:nval]
t2 = t[nval:nval+ntest]
t3 = t[nval+ntest:]
X_train, y_train = t1[feat], t1[target]
X_val, y_val = t2[feat], t2[target]
X_test, y_test = t3[feat], t3[target]
X_train['keras'] = model.predict(X_train)
 X_test['keras'] = model.predict(X_test)
 model = CatBoostRegressor(verbose=False)
 y_pred = de_qt(model.fit(X_train[feat], y_train ).predict(X_test[feat]))
y_true = de_qt(y_test.values)
 lmetric = int(mean_absolute_error(y_true, y_pred))
print('The MAE of prediction w/o keras is:', lmetric)
 model = CatBoostRegressor(verbose=False)
 y_pred = de_qt(model.fit(X_train, y_train ).predict(X_test))
y_true = de_qt(y_test.values)
 lmetric = int(mean_absolute_error(y_true, y_pred))
print('The MAE of prediction with keras is:', lmetric)
 importances = model.feature_importances_
target_results = pd.DataFrame(importances, columns=[target])
 target_results['feature'] = X_train.columns
t = target_results.sort_values(by = target)
 t = t.tail(15)
 plt.figure(figsize=(5, 5))
plt.title('Feature Importance')
 plt.barh(t['feature'], t[target])
 plt.show() 
<split>
def inv_cosine(epoch, t_max, ampl):

 return 1 - cosine(epoch, t_max, ampl )
%%time
n_fold = 7
folds = KFold(n_splits=n_fold, shuffle=True, random_state=101)
 params = {
"n_estimators": 2000,
"boosting_type": "gbdt",
"metric": "mae",
"num_leaves": 66,
"learning_rate": 0.005,
"feature_fraction": 0.9,
"bagging_fraction": 0.8,
"agging_freq": 3,
"max_bins": 2048,
"verbose": 0,
"random_state": 101,
"nthread": -1,
"device": "cpu",
}
y_pred = np.zeros(t4p.shape[0])
 for n_fold,(trn_idx, val_idx)in enumerate(folds.split(t4m)) :
print(f"Fold {n_fold}:")
 trn_x, trn_y = t4m[feat].iloc[trn_idx], t4m[target].iloc[trn_idx]
val_x, val_y = t4m[feat].iloc[val_idx], t4m[target].iloc[val_idx]
model = lgb.LGBMRegressor(**params)
 model.fit(trn_x, trn_y,
eval_set= [(trn_x, trn_y),(val_x, val_y)],
eval_metric="mae", verbose=200, early_stopping_rounds=50 
)
 y_pred += model.predict(t4p[feat], num_iteration=model.best_iteration_)/ folds.n_splits
plt.figure()
plt.hist(y_pred, bins=50)
 plt.title('y pred distribution')
 plt.show()
my_submission = pd.DataFrame({
'segment_id':t4p['segment_id'].values,
'time_to_eruption':y_pred
})
 my_submission.loc[my_submission[target] < 60000, target] = 60000
my_submission.loc[my_submission[target] > 4.8e+07, target] = 4.8e+07
my_submission.to_csv(f'submission_{ver}.csv', index=False )<import_modules>
def one_cycle(epoch, t_max, a1=0.6, a2=1.0, pivot=0.3):

 pct = epoch / t_max
if pct < pivot:
return inv_cosine(epoch, pivot*t_max, a1)
 return cosine(epoch - pivot*t_max,(1-pivot)*t_max, a2 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from glob import glob
import pandas as pd
import os
import dask.dataframe as dd
from tsfresh.feature_extraction import feature_calculators
import librosa
import pywt
from joblib import Parallel, delayed
from tqdm import tqdm_notebook
import scipy as sp
import itertools
import gc
from sklearn.metrics import mean_absolute_error
from lightgbm import LGBMRegressor
from catboost import CatBoostRegressor
from xgboost import XGBRegressor
import lightgbm as lgb
import gc<load_from_csv>
class SepConv1d(nn.Module):

 def __init__(self, ni, no, kernel, stride, pad,
drop=None, bn=True,
activ=lambda: nn.PReLU()):
super().__init__()
assert drop is None or(0.0 < drop < 1.0)
 layers = [_SepConv1d(ni, no, kernel, stride, pad)]
if activ:
layers.append(activ())
 if bn:
layers.append(nn.BatchNorm1d(no))
if drop is not None:
layers.append(nn.Dropout(drop))
self.layers = nn.Sequential(*layers)
 def forward(self, x):
return self.layers(x )
train = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train.csv')
 train['segment_id'] = train['segment_id'].astype(str)
 train = train.sort_values('segment_id')
 y = train['time_to_eruption']
del(train )<define_variables>
class Classifier(nn.Module):
def __init__(self, raw_ni, fft_ni, no, drop=.5):
super().__init__()
self.raw = nn.Sequential(
 SepConv1d(raw_ni, 32, 8, 2, 3, drop=drop),
SepConv1d(32, 32, 3, 1, 1, drop=drop),
SepConv1d(32, 64, 8, 4, 2, drop=drop),
SepConv1d(64, 64, 3, 1, 1, drop=drop),
SepConv1d(64, 128, 8, 4, 2, drop=drop),
SepConv1d(128, 128, 3, 1, 1, drop=drop),
SepConv1d(128, 256, 8, 4, 2),
Flatten() ,
nn.Dropout(drop), nn.Linear(256, 64), nn.PReLU() , nn.BatchNorm1d(64),
nn.Dropout(drop), nn.Linear(64, 64), nn.PReLU() , nn.BatchNorm1d(64))
self.fft = nn.Sequential(
 SepConv1d(fft_ni, 32, 8, 2, 4, drop=drop),
SepConv1d(32, 32, 3, 1, 1, drop=drop),
SepConv1d(32, 64, 8, 2, 4, drop=drop),
SepConv1d(64, 64, 3, 1, 1, drop=drop),
SepConv1d(64, 128, 8, 4, 4, drop=drop),
SepConv1d(128, 128, 8, 4, 4, drop=drop),
SepConv1d(128, 256, 8, 2, 3),
Flatten() ,
nn.Dropout(drop), nn.Linear(256, 64), nn.PReLU() , nn.BatchNorm1d(64),
nn.Dropout(drop), nn.Linear(64, 64), nn.PReLU() , nn.BatchNorm1d(64))
self.out = nn.Sequential(
 nn.Linear(128, 64), nn.ReLU(inplace=True), nn.Linear(64, no))
self.init_weights(nn.init.kaiming_normal_)
 def init_weights(self, init_fn):
def init(m):
for child in m.children() :
if isinstance(child, nn.Conv1d):
init_fn(child.weights)
 init(self)
 def forward(self, t_raw, t_fft):
raw_out = self.raw(t_raw)
 fft_out = self.fft(t_fft)
 t_in = torch.cat([raw_out, fft_out], dim=1)
 out = self.out(t_in)
 return out
train_paths = sorted(glob('/kaggle/input/predict-volcanic-eruptions-ingv-oe/train/*'))<load_from_csv>
raw_feat = raw_arr.shape[1]
fft_feat = fft_arr.shape[1]
trn_dl, val_dl, tst_dl = create_loaders(datasets, bs=256)
 lr = 0.001
n_epochs = 3000
iterations_per_epoch = len(trn_dl)
 period = n_epochs*iterations_per_epoch
num_classes = 9
best_acc = 0
patience, trials = 500, 0
base = 1
step = 2
iteration = 0
loss_history = []
acc_history = []
model = Classifier(raw_feat, fft_feat, num_classes ).to(device)
 criterion = nn.CrossEntropyLoss(reduction='sum')
 opt = optim.Adam(model.parameters() , lr=lr)
 sched = Scheduler(opt, partial(one_cycle, t_max=period, pivot=0.3))
print('Start model training')
 for epoch in range(1, n_epochs + 1):
model.train()
epoch_loss = 0
for i, batch in enumerate(trn_dl):
iteration += 1
x_raw, x_fft, y_batch = [t.to(device)for t in batch]
sched.step(iteration)
 opt.zero_grad()
out = model(x_raw, x_fft)
 loss = criterion(out, y_batch)
 epoch_loss += loss.item()
loss.backward()
opt.step()
epoch_loss /= trn_sz
loss_history.append(epoch_loss)
 model.eval()
correct, total = 0, 0
for batch in val_dl:
x_raw, x_fft, y_batch = [t.to(device)for t in batch]
out = model(x_raw, x_fft)
 preds = F.log_softmax(out, dim=1 ).argmax(dim=1)
 total += y_batch.size(0)
 correct +=(preds == y_batch ).sum().item()
acc = correct / total
acc_history.append(acc)
 if epoch % base == 0:
print(f'Epoch: {epoch:3d}.Loss: {epoch_loss:.4f}.Acc.: {acc:2.2%}')
 base *= step
if acc > best_acc:
trials = 0
best_acc = acc
torch.save(model.state_dict() , 'best.pth')
 print(f'Epoch {epoch} best model saved with accuracy: {best_acc:2.2%}')
 else:
trials += 1
if trials >= patience:
print(f'Early stopping on epoch {epoch}')
 break
print('Done!' )
sub = pd.read_csv('/kaggle/input/predict-volcanic-eruptions-ingv-oe/sample_submission.csv' )<statistical_test>
test_results = []
model.load_state_dict(torch.load('best.pth'))
model.eval()
for x_raw, x_fft, _ in tst_dl:
batches = [t.to(device)for t in(x_raw, x_fft)]
out = model(*batches)
 y_hat = F.log_softmax(out, dim=1 ).argmax(dim=1)
 test_results.extend(y_hat.tolist() )
<set_options><EOS> 
submit = pd.read_csv(sample)
 submit['surface'] = enc.inverse_transform(test_results)
 submit.to_csv('submit_one_cycle.csv', index=None )