Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<load_pretrained>
%matplotlib inline
pd.set_option('display.float_format', lambda x: '%.3f' % x)
 pd.set_option('display.max_rows', 100)
 pd.set_option('display.max_columns', 100)
 print('Done' )
siamese_network.load_weights(tmp_roberta_name)
 neural_classifier.save_weights(tmp_roberta_name, overwrite=True, save_format='h5' )<drop_column>
path = '.. /input/competitive-data-science-predict-future-sales/'
DF_sales = pd.read_csv(path + 'sales_train.csv')
 DF_items = pd.read_csv(path + 'items.csv')
 DF_item_cat = pd.read_csv(path + 'item_categories.csv')
 DF_shops = pd.read_csv(path + 'shops.csv')
 DF_test = pd.read_csv(path + 'test.csv')
 DF_sample_subs = pd.read_csv(path + 'sample_submission.csv' )
del neural_classifier, siamese_network<define_variables>
print('---' * 10)
 print('sales_train check
')
 print(DF_sales.isna().sum())
 print('---' * 10)
 print('item_cat check
')
 print(DF_items.isna().sum())
 print('---' * 10)
 print('item check
')
 print(DF_item_cat.isna().sum())
 print('---' * 10)
 print('shops check
')
 print(DF_shops.isna().sum() )
dataset_for_training = calculate_features_of_texts(
 texts=multilingual_corpus,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len,
fe=feature_extractor,
batch_size=batch_size_for_siamese,
max_dataset_size=min(600, batch_size_for_siamese * 9)
)
 assert len(dataset_for_training)== 4<define_variables>
DF_sales = DF_sales[DF_sales.item_price<100000]
DF_sales = DF_sales[DF_sales.item_cnt_day<1000]
DF_sales = DF_sales[DF_sales.item_price > 0].reset_index(drop=True)
 DF_sales.loc[DF_sales.item_cnt_day < 0, 'item_cnt_day'] = 0
dataset_for_submission = calculate_features_of_texts(
 texts=texts_for_submission,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len,
fe=feature_extractor,
batch_size=batch_size_for_siamese,
max_dataset_size=min(300, batch_size_for_siamese * 3)
 )<prepare_x_and_y>
DF_sales.loc[DF_sales.shop_id == 0, 'shop_id'] = 57
DF_sales.loc[DF_sales.shop_id == 1, 'shop_id'] = 58
DF_sales.loc[DF_sales.shop_id == 11, 'shop_id'] = 10
X_embedded = []
y_embedded = []
for cur_lang in dataset_for_training:
X_embedded.append(dataset_for_training[cur_lang][0])
 y_embedded.append(dataset_for_training[cur_lang][1])
 for cur_lang in dataset_for_submission:
X_embedded.append(dataset_for_submission[cur_lang][0])
 y_embedded.append(
 np.array(
 [-1 for _ in range(dataset_for_submission[cur_lang][0].shape[0])],
dtype=np.int32 
)
)
 X_embedded = np.vstack(X_embedded)
 y_embedded = np.concatenate(y_embedded )<drop_column>
DF_shops.loc[DF_shops.shop_name == 'Сергиев Посад ТЦ "7Я"', 'shop_name'] = 'СергиевПосад ТЦ "7Я"'
DF_shops['shop_city'] = DF_shops['shop_name'].str.split(' ' ).map(lambda x: x[0])
 DF_shops['shop_cat'] = DF_shops['shop_name'].str.split(' ' ).map(lambda x: x[1])
 DF_shops.head(5 )
del dataset_for_training, dataset_for_submission, feature_extractor<normalization>
DF_shops['shop_city'] = LabelEncoder().fit_transform(DF_shops['shop_city'])
 DF_shops['shop_cat'] = LabelEncoder().fit_transform(DF_shops['shop_cat'])
 DF_shops.drop(['shop_name'], axis=1, inplace= True)
 DF_shops.head(5 )
X_embedded = TSNE(n_components=2, n_jobs=-1 ).fit_transform(X_embedded )<drop_column>
DF_items = pd.merge(DF_items, DF_item_cat, on = 'item_category_id')
 DF_items
del indices_of_negative_classes
del indices_of_positive_classes
del indices_of_unknown_classes
del X_embedded, y_embedded<set_options>
DF_items['item_sub_cat_1'] = np.select(
 [DF_items.item_category_id.isin(range(0,8)) ,
DF_items.item_category_id.isin(range(10,18)) ,
DF_items.item_category_id.isin(range(18,32)) ,
DF_items.item_category_id.isin(range(32,37)) ,
DF_items.item_category_id.isin(range(37,42)) ,
DF_items.item_category_id.isin(range(42,55)) ,
DF_items.item_category_id.isin(range(55,61)) ,
DF_items.item_category_id.isin(range(61,73)) ,
DF_items.item_category_id.isin(range(73,79)) ,
DF_items.item_category_id.isin([8,80]),
DF_items.item_category_id==83,
DF_items.item_category_id==9,
DF_items.item_category_id==79,
DF_items.item_category_id.isin([81,82])],
['accessories','consoles','games','payment_cards','Cinema','books','music',
'gifts','programs','tickets','batteries','delivery','office','discs'])
 DF_items
gc.collect()
tf.keras.backend.clear_session()
if tpu:
tf.tpu.experimental.shutdown_tpu_system(tpu)
 del strategy
tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu )<categorify>
DF_items['item_sub_cat_1'] = LabelEncoder().fit_transform(DF_items['item_sub_cat_1'])
 DF_items.drop(['item_name','item_category_name'], axis=1, inplace= True)
 DF_items
training_set_size = 0
for cur_lang in all_languages_for_training:
multilingual_corpus[cur_lang] =(
 regular_encode(
 texts=[cur[0] for cur in multilingual_corpus[cur_lang]],
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len 
),
np.array(
 [cur[1] for cur in multilingual_corpus[cur_lang]],
dtype=np.int32 
)
)
 training_set_size += multilingual_corpus[cur_lang][1].shape[0]<categorify>
DF_all = []
cols = ['date_block_num','shop_id','item_id']
for i in range(34):
sales = DF_sales[DF_sales.date_block_num==i]
DF_all.append(np.array(list(product([i], sales.shop_id.unique() , sales.item_id.unique())) , dtype='int16'))
DF_all = pd.DataFrame(np.vstack(DF_all), columns=cols)
 DF_all['date_block_num'] = DF_all['date_block_num'].astype(np.int8)
 DF_all['shop_id'] = DF_all['shop_id'].astype(np.int8)
 DF_all['item_id'] = DF_all['item_id'].astype(np.int16)
 DF_all.sort_values(cols,inplace=True)
 DF_all
texts_for_submission_ = []
identifiers_for_submission = []
for cur_lang in sorted(list(texts_for_submission.keys())) :
texts_for_submission_ += [
cur[0] for cur in texts_for_submission[cur_lang]
]
identifiers_for_submission += [
cur[1] for cur in texts_for_submission[cur_lang]
]
X_submit = regular_encode(
 texts=texts_for_submission_,
tokenizer=xlmroberta_tokenizer, maxlen=max_seq_len 
)
 identifiers_for_submission = np.array(
 identifiers_for_submission,
dtype=np.int32
)<drop_column>
DF_test.drop(['ID'], axis=1, inplace = True)
 DF_test['date_block_num'] = 34
DF_test['date_block_num'] = DF_test['date_block_num']
DF_test['shop_id'] = DF_test['shop_id']
DF_test['item_id'] = DF_test['item_id']
DF_test.head()
del xlmroberta_tokenizer, texts_for_submission_
gc.collect()<train_model>
DF_all = pd.concat([DF_all, DF_test], ignore_index=True, sort=False, keys=cols)
 DF_all = pd.merge(DF_all, DF_shops, on=['shop_id'], how='left')
 DF_all = pd.merge(DF_all, DF_items, on=['item_id'], how='left')
 DF_all.fillna(0, inplace=True)
 DF_all
experiment_duration = int(round(time.time() - experiment_start_time))
print('Duration of the Siamese XLM-RoBERTa preparing is {0} seconds.'.format(
 experiment_duration))<load_pretrained>
DF_all.date_block_num = DF_all.date_block_num.astype(np.int8)
 DF_all.shop_id = DF_all.shop_id.astype(np.int8)
 DF_all.item_id = DF_all.item_id.astype(np.int16)
 DF_all.shop_city = DF_all.shop_city.astype(np.int8)
 DF_all.shop_cat = DF_all.shop_cat.astype(np.int8)
 DF_all.item_category_id = DF_all.item_category_id.astype(np.int8)
 DF_all.item_sub_cat_1 = DF_all.item_sub_cat_1.astype(np.int8 )
with strategy.scope() :
neural_classifier = build_classifier(
 config=xlmroberta_config,
hidden_state_size=sentence_embedding_size,
max_len=max_seq_len,
max_lr=classifier_learning_rate_max,
base_lr=classifier_learning_rate_min,
steps_per_epoch=training_set_size // batch_size_for_cls,
language=all_languages_for_training[0] 
)
 neural_classifier.load_weights(tmp_roberta_name )<define_variables>
def lag_feature(df, lags, col):
tmp = df[['date_block_num','shop_id','item_id',col]]
for i in lags:
shifted = tmp.copy()
shifted.columns = ['date_block_num','shop_id','item_id', col+'_lag_'+str(i)]
shifted['date_block_num'] += i
df = pd.merge(df, shifted, on=['date_block_num','shop_id','item_id'], how='left')
 return df
def fill_na_test(df):
for col in df.columns:
if('_lag' in col)&(df[col].isnull().any()):
print(col)
 if('month' in col):
df[col].fillna(0, inplace=True)
 return df
result_of_submission1, postprocessing_duration = do_submit(
 data_for_training=multilingual_corpus,
data_for_submission=X_submit,
language_for_validation=all_languages_for_training[0],
maxlen=max_seq_len, base_classifier=neural_classifier,
batch_size=batch_size_for_cls,
max_duration=int(round(2.7 * 3600)-(time.time() - experiment_start_time)) // 5
)<set_options>
temp = DF_sales.groupby(['shop_id','item_id','date_block_num'] ).agg(item_cnt_month=('item_cnt_day',sum))
temp.columns = ['item_cnt_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=cols, how='left')
 DF_all['item_cnt_month'] =(DF_all['item_cnt_month'] 
.fillna(0)
.clip(0,20)
.astype(np.float16))
del neural_classifier
gc.collect()
tf.keras.backend.clear_session()
if tpu:
tf.tpu.experimental.shutdown_tpu_system(tpu)
 del strategy
tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu )<load_pretrained>
ts = time.time()
DF_all = lag_feature(DF_all, [1, 2, 3], 'item_cnt_month')
 temp = DF_all.groupby(['date_block_num'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_month')
 DF_all.drop(['avg_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'item_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_item_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'item_id'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_item_month')
 DF_all.drop(['avg_item_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shop_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id'], how='left')
 DF_all = lag_feature(DF_all, [1, 2, 3], 'avg_shop_month')
 DF_all.drop(['avg_shop_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'item_category_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_item_cat_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'item_category_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_item_cat_month')
 DF_all.drop(['avg_item_cat_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_category_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_itemcat_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_category_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_itemcat_month')
 DF_all.drop(['avg_shops_itemcat_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_id'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_item_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_id'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_item_month')
 DF_all.drop(['avg_shops_item_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_id', 'item_sub_cat_1'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shops_item_sub_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_id', 'item_sub_cat_1'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shops_item_sub_month')
 DF_all.drop(['avg_shops_item_sub_month'], axis=1, inplace= True)
 temp = DF_all.groupby(['date_block_num', 'shop_city'] ).agg({'item_cnt_month' : ['mean']})
 temp.columns = ['avg_shopscity_month']
temp.reset_index(inplace=True)
 DF_all = pd.merge(DF_all, temp, on=['date_block_num', 'shop_city'], how='left')
 DF_all = lag_feature(DF_all, [1,2], 'avg_shopscity_month')
 DF_all.drop(['avg_shopscity_month'], axis=1, inplace= True)
 DF_all
print('Use time:', time.time() - ts)
with strategy.scope() :
neural_classifier = build_classifier(
 config=xlmroberta_config,
hidden_state_size=sentence_embedding_size,
max_len=max_seq_len,
max_lr=classifier_learning_rate_max,
base_lr=classifier_learning_rate_min,
steps_per_epoch=training_set_size // batch_size_for_cls,
language=all_languages_for_training[1] 
)
 neural_classifier.load_weights(tmp_roberta_name )<define_variables>
DF_all['month'] = DF_all['date_block_num'] % 12
days = pd.Series([31,28,31,30,31,30,31,31,30,31,30,31])
 DF_all['days'] = DF_all['month'].map(days)
 DF_all['years'] = np.select(
 [DF_all.date_block_num.isin(range(0,12)) ,
DF_all.date_block_num.isin(range(12,25)) ,
DF_all.date_block_num.isin(range(25,35)) ],
['13','14','15'])
 DF_all['month'] = DF_all['month'] + 1
DF_all['days'] = DF_all['days'].astype(np.int8)
 DF_all['month'] = DF_all['month'].astype(np.int8)
 DF_all['years'] = DF_all['years'].astype(np.int8)
 DF_all
result_of_submission2, _ = do_submit(
 data_for_training=multilingual_corpus,
data_for_submission=X_submit,
language_for_validation=all_languages_for_training[1],
maxlen=max_seq_len, base_classifier=neural_classifier,
batch_size=batch_size_for_cls,
max_duration=(int(round(2.7 * 3600)-(time.time() - experiment_start_time)) // 3 -
postprocessing_duration)
 )<set_options>
DF_all['item_shop_first_sale'] = \
DF_all['date_block_num'] - DF_all.groupby(['item_id','shop_id'])['date_block_num'].transform('min')
 DF_all['item_first_sale'] = \
DF_all['date_block_num'] - DF_all.groupby('item_id')['date_block_num'].transform('min')
 DF_all
del neural_classifier
gc.collect()
tf.keras.backend.clear_session()
if tpu:
tf.tpu.experimental.shutdown_tpu_system(tpu)
 del strategy
tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu )<load_pretrained>
DF_all.isnull().any()
with strategy.scope() :
neural_classifier = build_classifier(
 config=xlmroberta_config,
hidden_state_size=sentence_embedding_size,
max_len=max_seq_len,
max_lr=classifier_learning_rate_max,
base_lr=classifier_learning_rate_min,
steps_per_epoch=training_set_size // batch_size_for_cls,
language=all_languages_for_training[2] 
)
 neural_classifier.load_weights(tmp_roberta_name )<define_variables>
DF_all = fill_na_test(DF_all)
 DF_all.isnull().any()
result_of_submission3, _ = do_submit(
 data_for_training=multilingual_corpus,
data_for_submission=X_submit,
language_for_validation=all_languages_for_training[2],
maxlen=max_seq_len, base_classifier=neural_classifier,
batch_size=batch_size_for_cls,
max_duration=(int(round(2.7 * 3600)-(time.time() - experiment_start_time)) // 2 -
postprocessing_duration)
 )<set_options>
DF_all.to_pickle('dataset.pkl' )
del neural_classifier
gc.collect()
tf.keras.backend.clear_session()
if tpu:
tf.tpu.experimental.shutdown_tpu_system(tpu)
 del strategy
tf.tpu.experimental.initialize_tpu_system(tpu)
 strategy = tf.distribute.experimental.TPUStrategy(tpu )<load_pretrained>
del DF_all
del temp
del DF_sales
del DF_items
del DF_item_cat
del DF_shops
gc.collect()
with strategy.scope() :
neural_classifier = build_classifier(
 config=xlmroberta_config,
hidden_state_size=sentence_embedding_size,
max_len=max_seq_len,
max_lr=classifier_learning_rate_max,
base_lr=classifier_learning_rate_min,
steps_per_epoch=training_set_size // batch_size_for_cls,
language=all_languages_for_training[3] 
)
 neural_classifier.load_weights(tmp_roberta_name )<define_variables>
data = pd.read_pickle('dataset.pkl' )
result_of_submission4, _ = do_submit(
 data_for_training=multilingual_corpus,
data_for_submission=X_submit,
language_for_validation=all_languages_for_training[3],
maxlen=max_seq_len, base_classifier=neural_classifier,
batch_size=batch_size_for_cls,
max_duration=(int(round(2.7 * 3600)-(time.time() - experiment_start_time)) -
postprocessing_duration)
 )<save_to_csv>
data = data[[
'date_block_num', 'shop_id', 'item_id', 'shop_city', 'shop_cat',
'item_category_id', 'item_sub_cat_1', 'item_cnt_month',
'item_cnt_month_lag_1', 'item_cnt_month_lag_2', 'item_cnt_month_lag_3',
'avg_month_lag_1', 'avg_month_lag_2', 'avg_month_lag_3',
'avg_item_month_lag_1', 'avg_item_month_lag_2', 'avg_item_month_lag_3',
'avg_shop_month_lag_1', 'avg_shop_month_lag_2', 'avg_shop_month_lag_3',
'avg_item_cat_month_lag_1', 'avg_item_cat_month_lag_2',
'avg_shops_itemcat_month_lag_1', 'avg_shops_itemcat_month_lag_2',
'avg_shops_item_month_lag_1', 'avg_shops_item_month_lag_2',
'avg_shops_item_sub_month_lag_1', 'avg_shops_item_sub_month_lag_2',
'avg_shopscity_month_lag_1', 'avg_shopscity_month_lag_2', 'month',
'days', 'years', 'item_shop_first_sale', 'item_first_sale'
]]
with codecs.open('submission.csv', mode='w', encoding='utf-8', errors='ignore')as fp:
fp.write('id,toxic
')
 for sample_idx in range(identifiers_for_submission.shape[0]):
proba_val = result_of_submission1[sample_idx]
proba_val += result_of_submission2[sample_idx]
proba_val += result_of_submission3[sample_idx]
proba_val += result_of_submission4[sample_idx]
proba_val /= 4.0
id_val = identifiers_for_submission[sample_idx]
fp.write('{0},{1:.9f}
'.format(id_val, proba_val))<train_model>
X_train = data[data.date_block_num < 33].drop(['item_cnt_month'], axis=1)
 Y_train = data[data.date_block_num < 33]['item_cnt_month']
X_valid = data[data.date_block_num == 33].drop(['item_cnt_month'], axis=1)
 Y_valid = data[data.date_block_num == 33]['item_cnt_month']
X_test = data[data.date_block_num == 34].drop(['item_cnt_month'], axis=1 )
print('Experiment duration is {0:.3f}.'.format(time.time() - experiment_start_time))<load_from_url>
lgb_train = lgb.Dataset(X_train, Y_train)
 lgb_eval = lgb.Dataset(X_valid, Y_valid, reference=lgb_train )
!curl https://raw.githubusercontent.com/pytorch/xla/master/contrib/scripts/env-setup.py -o pytorch-xla-env-setup.py
!python pytorch-xla-env-setup.py --version nightly --apt-packages libomp5 libopenblas-dev<set_options>
def objective(trial):
param = {
"objective": "regression",
"metric": "rmse",
"verbosity": -1,
"boosting_type": "gbdt",
"lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
"lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
"num_leaves": trial.suggest_int("num_leaves", 2, 256),
"feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
"bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
"bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
"min_child_samples": trial.suggest_int("min_child_samples", 5, 100),
}
model = lgb.train(param,
lgb_train,
valid_sets=[lgb_train,lgb_eval],
early_stopping_rounds=15,
verbose_eval=1)
 y_pred = model.predict(X_valid)
 accuracy = rmsle(Y_valid, y_pred)
 return accuracy
warnings.filterwarnings("ignore" )<import_modules>
study = optuna.create_study(direction='minimize')
 study.optimize(objective, n_trials=5)
 print('Number of finished trials:', len(study.trials))
print('Best trial:', study.best_trial.params )
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torch.utils.data import DataLoader
import torchvision
from torchvision import transforms
from tqdm.notebook import tqdm<find_best_params>
best_params = study.best_trial.params
print(f'Best trial parameters
{best_params}' )
input = torch.randn(10,device=device)
 linear = torch.nn.Linear(10, 20 ).to(device)
 output = linear(input)
 print(output )<categorify>
x = {"objective": "regression",
"metric" : "rmse",
"verbosity": -1,
"boosting_type": "gbdt"}
best_params.update(x)
 best_params
transform = transforms.ToTensor()
dataset = torchvision.datasets.MNIST(root='./data', train=True,
download=True, transform=transform)
 train_loader = DataLoader(dataset,batch_size=256)
 class MNIST(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1)
 self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1)
 self.conv3 = nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1)
 def forward(self, xb):
xb = xb.view(-1, 1, 28, 28)
 xb = F.relu(self.conv1(xb))
xb = F.relu(self.conv2(xb))
xb = F.relu(self.conv3(xb))
xb = F.avg_pool2d(xb, 4)
 return xb.view(-1, xb.size(1))
def train_loop() :
model = MNIST().train().to(device)
 loss_fn = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters() , lr=1e-2, momentum=0.9)
 for data, target in tqdm(train_loader,total=len(train_loader)) :
optimizer.zero_grad()
data = data.to(device)
 target = target.to(device)
 output = model(data)
 loss = loss_fn(output, target)
 loss.backward()
optimizer.step()
print(f'Final Loss: {loss}' )<feature_engineering>
evals_result = {}
model = lgb.train(best_params,
lgb_train,
valid_sets=[lgb_train,lgb_eval],
evals_result=evals_result,
early_stopping_rounds=30,
verbose_eval=1,
)
os.environ['XLA_USE_BF16']="1"
os.environ['XLA_TENSOR_ALLOCATOR_MAXSIZE'] = '100000000' 
<init_hyperparams>
y_pred = model.predict(X_valid)
 rmsle(Y_valid, y_pred )
class AverageMeter:

 def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count<categorify>
Y_test = model.predict(X_test ).clip(0, 20)
 submission = pd.DataFrame({
"ID": DF_test.index,
"item_cnt_month": Y_test
})
 submission.head(10 )
class ArrayDataset(torch.utils.data.Dataset):
def __init__(self,*arrays):
assert all(arrays[0].shape[0] == array.shape[0] for array in arrays)
 self.arrays = arrays
def __getitem__(self, index):
return tuple(torch.from_numpy(np.array(array[index])) for array in self.arrays)
 def __len__(self):
return self.arrays[0].shape[0]<choose_model_class>
submission.to_csv('submission.csv', index=False )
<feature_engineering><EOS> 
submission.to_csv('submission.csv', index=False )