Kaggle competition: Predict Future Sales
<SOS> metric: rmse Kaggle data source: predict-future-sales<load_pretrained>
print(os.listdir(".. /input"))
print(lgb.__name__, lgb.__version__)
 sns.set()
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/resnet50/resnet50.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'resnet50.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url<load_from_csv>
df_shop = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/shops.csv')
 df_item_categories = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/item_categories.csv')
 df_items = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/items.csv')
 df_sales_train = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sales_train.csv')
 df_test = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/test.csv')
 df_sample_submission = pd.read_csv('.. /input/competitive-data-science-predict-future-sales/sample_submission.csv')
train_df = pd.read_csv(path/'train.csv')
 train_df.head()<load_from_csv>
show_EDA = False
labels_df = pd.read_csv(path/'labels.csv')
 labels_df.head()<load_from_csv>
if show_EDA:
df_items.head()
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.head()<normalization>
if show_EDA:
print(df_data[df_data['item_cnt_month'] !=0].item_cnt_month.value_counts())
 print('training unique(id)with zero:')
 print('shop: ', df_data[df_data['date_block_num'] <= 33].shop_id.nunique())
 print('item: ', df_data[df_data['date_block_num'] <= 33].item_id.nunique())
 print('training unique(id)w/o zero:')
 print('shop: ', df_data[(df_data['date_block_num'] <= 33)&(df_data['item_cnt_month'] !=0)].shop_id.nunique())
 print('item: ', df_data[(df_data['date_block_num'] <= 33)&(df_data['item_cnt_month'] !=0)].item_id.nunique())
 print('testing unique(id):')
 print('shop: ', df_data[df_data['date_block_num'] == 34].shop_id.nunique())
 print('item: ', df_data[df_data['date_block_num'] == 34].item_id.nunique() )
tfms = get_transforms(do_flip=True, flip_vert=False, max_rotate=0.10, max_zoom=1.5, max_warp=0.2, max_lighting=0.2,
xtra_tfms=[(symmetric_warp(magnitude=(-0,0), p=0)) ,] )<define_variables>
if show_EDA:
for shop, item in zip(df_test.shop_id[:10], df_test.item_id[:10]):
print(df_data[(df_data['shop_id'] == shop)&(df_data['item_id'] == item)&(df_data['date_block_num'] < 34)])
 print('next===========================================================')
 for shop, item in zip(df_test.shop_id[10:20], df_test.item_id[10:20]):
print('combination accounted for {0:.4%} of sample in the data set'.format(df_data[ 
(df_data['shop_id'] == shop)& 
(df_data['item_id'] == item)& 
(df_data['date_block_num'] < 34)& 
(df_data['item_cnt_month'] != 0)].shape[0] /
df_data[(df_data['date_block_num'] < 34)& 
(df_data['item_cnt_month'] != 0)].shape[0]))
data.show_batch()<compute_test_metric>
splits = df_shop['shop_name'].str.split(' ', expand=True)
 df_shop['pre_shop'] = splits[0]
df_shop['pre_shop'] = df_shop['pre_shop'].astype('category')
 df_shop['pre_shop_encode'] = df_shop['pre_shop'].cat.codes
df_shop.head()
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val =(-logit ).clamp(min=0)
 loss = logit - logit * target + max_val + \ 
(( -max_val ).exp() +(-logit - max_val ).exp() ).log()
invprobs = F.logsigmoid(-logit *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
 return loss.mean()<choose_model_class>
splits = df_item_categories['item_category_name'].str.split(' - ', expand=True)
 df_item_categories['pre_cat'], df_item_categories['post_cat'] = splits[0], splits[1]
df_item_categories['pre_cat'] = df_item_categories['pre_cat'].fillna('unknown')
 df_item_categories['post_cat'] = df_item_categories['post_cat'].fillna('unknown')
 df_item_categories['pre_cat'] = df_item_categories['pre_cat'].astype('category')
 df_item_categories['post_cat'] = df_item_categories['post_cat'].astype('category')
 df_item_categories['pre_cat_encode'] = df_item_categories['pre_cat'].cat.codes
df_item_categories['post_cat_encode'] = df_item_categories['post_cat'].cat.codes
df_item_categories.head()
learn = cnn_learner(data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta,ps=0.5 )<train_model>
df_data['item_cnt_month'] = df_data['item_cnt_month'].clip(0, 20)
 df_data = create_aggregation(df_data, ['shop_id', 'date_block_num'], 'mean', 'item_cnt_day', 'shop_total_item_cnt_month')
 df_data = create_aggregation(df_data, ['item_id', 'date_block_num'], 'mean', 'item_cnt_day', 'total_item_cnt_month')
 df_data = create_aggregation(df_data, ['shop_id', 'item_id', 'date_block_num'], 'mean', 'item_price', 'mean_item_price')
 df_data = create_aggregation(df_data, ['date_block_num'], pd.Series.nunique, 'date', 'days_in_month')
 df_data = pd.merge(df_data, df_items.loc[:, ['item_id', 'item_category_id']], how='left', on='item_id')
 df_data = pd.merge(df_data, df_item_categories.loc[:, ['item_category_id', 'pre_cat_encode', 'post_cat_encode']], how='left', on='item_category_id')
 df_data = pd.merge(df_data, df_shop.loc[:, ['shop_id', 'pre_shop_encode']], how='left', on='shop_id')
 df_sales_train = pd.merge(df_sales_train, df_items.loc[:, ['item_id', 'item_category_id']], how='left', on='item_id')
 df_sales_train = pd.merge(df_sales_train, df_item_categories.loc[:, ['item_category_id', 'pre_cat_encode', 'post_cat_encode']], how='left', on='item_category_id')
 df_sales_train = pd.merge(df_sales_train, df_shop.loc[:, ['shop_id', 'pre_shop_encode']], how='left', on='shop_id')
 df_data = create_aggregation(df_data, ['pre_cat_encode', 'date_block_num'], 'mean', 'item_cnt_day', 'pre_cat_total_item_cnt_month')
 df_data = create_aggregation(df_data, ['post_cat_encode', 'date_block_num'], 'mean', 'item_cnt_day', 'post_cat_total_item_cnt_month')
 df_data = create_aggregation(df_data, ['pre_shop_encode', 'date_block_num'], 'mean', 'item_cnt_day', 'pre_shop_total_item_cnt_month')
 df_data = create_aggregation(df_data, ['item_category_id', 'date_block_num'], 'mean', 'item_cnt_day', 'item_cat_total_item_cnt_month')
 del df_sales_train
gc.collect()
current_feature = list(df_data.columns.difference(['shop_id', 'item_id', 'date_block_num']))
print(current_feature)
 df_data.head()
learn.fit_one_cycle(3)
 learn.save('stage-1', return_path=True)
 learn.unfreeze()
learn.lr_find()
learn.recorder.plot()
learn.fit_one_cycle(4, slice(1e-5,1e-2))
learn.save('stage-2', return_path=True)
<predict_on_test>
col_int = ['shop_id', 'item_id', 'date_block_num', 'item_category_id', 'pre_cat_encode', 'post_cat_encode', 'pre_shop_encode']
col_float = list(df_data.columns.difference(col_int))
df_data.loc[:, col_int] = df_data.loc[:, col_int].astype(np.int16)
 df_data.loc[:, col_float] = df_data.loc[:, col_float].astype(np.float16)
 df_data.info()
valid_preds = learn.get_preds(DatasetType.Valid)
 best_thr = find_best_fixed_threshold(*valid_preds )<categorify>
def create_lag(to_merge, lag_range, col_target):
col_index = ['shop_id', 'item_id', 'date_block_num']
for month_shift in lag_range:
print('now running for lag {}'.format(month_shift))
temp_df = to_merge[col_index + col_target].copy()
gc.collect()
temp_df['date_block_num'] = temp_df['date_block_num'] + month_shift
naming_lag_col = lambda x: '{}_lag_{}'.format(x, month_shift)if x in col_target else x
temp_df = temp_df.rename(columns=naming_lag_col)
 col_to_float = list(temp_df.columns.difference(col_index))
temp_df.loc[:, col_to_float] = temp_df.loc[:, col_to_float].astype(np.float16)
 to_merge = pd.merge(to_merge, temp_df, how='left', on=col_index ).fillna(0)
 gc.collect()
print('completed!')
 return to_merge
test_preds = learn.TTA(ds_type=DatasetType.Test)
 test_df.attribute_ids = join_preds(test_preds, best_thr)
 test_df.head()<save_to_csv>
def create_diff(lag_range, col_target):
for col in col_target:
print('running diff for ', col, '...')
 for i in lag_range:
name = col + '_' + str(i)+ '-' + str(i + 1)
 if i == 0:
after = col
else:
after = col + '_' + 'lag_' + str(i)
 before = col + '_' + 'lag_' + str(i + 1)
 df_data[name] = df_data[after] - df_data[before]
print('completed!')
test_df.to_csv('submission.csv', index=False )<import_modules>
try:
os.remove('df_data.pickle')
 print('old file removed successfully')
 except:
print('file does not exist, just moving on...')
 with open('df_data.pickle', 'wb')as handle:
pickle.dump(df_data, handle )
fastai.__version__<define_variables>
with open('df_data.pickle', 'rb')as handle:
df_data = pickle.load(handle)
 gc.collect()
full_feature = df_data.columns
del df_data
gc.collect()
len(gc.get_objects() )
BATCH = 126
SIZE = 250
path = Path('.. /input/imet-2019-fgvc6/' )<load_pretrained>
ID_and_Category = ['item_id', 'shop_id', 'item_category_id','pre_cat_encode','post_cat_encode','pre_shop_encode']
current_feature = ['item_cnt_month','shop_total_item_cnt_month','total_item_cnt_month','mean_item_price','pre_cat_total_item_cnt_month','post_cat_total_item_cnt_month',
'pre_shop_total_item_cant_month','item_cat_total_item_cnt_month']
diff_feature = [col + '_1-2' for col in col_target]
drop_current_col = current_feature + diff_feature
Path('models' ).mkdir(exist_ok=True)
 !cp '.. /input/resnet50/resnet50.pth' 'models/'
def load_url(*args, **kwargs):
model_dir = Path('models')
 filename = 'resnet50.pth'
if not(model_dir/filename ).is_file() : raise FileNotFoundError
return torch.load(model_dir/filename)
 model_zoo.load_url = load_url<load_from_csv>
df_feature_selection = pd.read_csv('.. /input/feature-selection190713/LR_Selection-190713.csv')
 df_feature_selection.head()
train_df = pd.read_csv(path/'train.csv')
 train_df.head()<load_from_csv>
LR_ranking = df_feature_selection.LR_ranking.values
LGBM_ranking = df_feature_selection.LGBM_ranking.values
col_list = df_feature_selection.feature.values
LR_ranking
labels_df = pd.read_csv(path/'labels.csv')
 labels_df.head()<load_from_csv>
col_list[np.where(LGBM_ranking<=5)]
test_df = pd.read_csv(path/'sample_submission.csv')
 test_df.head()<normalization>
pd.set_option('display.max_rows', 500 )
tfms = get_transforms(do_flip=True, flip_vert=False, max_rotate=0.10, max_zoom=1.5, max_warp=0.2, max_lighting=0.2,
xtra_tfms=[(symmetric_warp(magnitude=(-0,0), p=0)) ,] )<define_variables>
def rmse_score(y_test, y_pred):
return np.sqrt(mean_squared_error(y_test, y_pred))
def clear_save_file(file_name, variable=None, save=False, file_type='pickle'):
file_path = file_name + '.' + file_type
try:
os.remove(file_path)
 except:
pass
if save:
if file_type == 'pickle':
with open(file_path, 'wb')as handle:
pickle.dump(variable, handle)
 gc.collect()
print(len(gc.get_objects()))
def create_data(val_month, feature_list, model_name, validation=False, meta=False):
print('Fetching data for ', model_name)
 with open('df_data.pickle', 'rb')as handle:
df_data = pickle.load(handle)
 gc.collect()
month_train =(df_data['date_block_num'] < val_month)
 X_train = df_data.loc[month_train, feature_list].values
y_train = df_data[month_train].item_cnt_month.values
month_test =(df_data['date_block_num'] == val_month)
 X_test = df_data.loc[month_test, feature_list].values
if validation:
y_test = df_data[month_test].item_cnt_month.values
del df_data
gc.collect()
if model_name == 'Linear':
clear_save_file('X_train', X_train, save=True, file_type='pickle')
 del X_train
clear_save_file('y_train', y_train, save=True, file_type='pickle')
 del y_train
clear_save_file('X_test', X_test, save=True, file_type='pickle')
 del X_test
if validation:
clear_save_file('y_test', y_test,save=True, file_type='pickle')
 del y_test
gc.collect()
print('Data on disk now!')
 if model_name == 'LGBM':
X_train_LGBM = lgb.Dataset(data=X_train, label=y_train)
 del X_train
del y_train
gc.collect()
print(len(gc.get_objects()))
clear_save_file('X_train_LGBM',file_type='bin')
 X_train_LGBM.save_binary('X_train_LGBM.bin')
 clear_save_file('X_test', X_test, save=True, file_type='pickle')
 if validation:
X_test_LGBM = lgb.Dataset(data=X_test, label=y_test, reference=X_train_LGBM)
 del X_train_LGBM
gc.collect()
print(len(gc.get_objects()))
clear_save_file('y_test', y_test, save=True, file_type='pickle')
 del y_test
gc.collect()
print(len(gc.get_objects()))
clear_save_file('X_test_LGBM',file_type='bin')
 X_test_LGBM.save_binary('X_test_LGBM.bin')
 del X_test_LGBM
gc.collect()
print(len(gc.get_objects()))
else:
del X_train_LGBM
gc.collect()
print(len(gc.get_objects()))
print('data on disk now!')
 def gridsearch_LGBM(num_feature_list, params, feature_ranking, col_list):
final_param_list = []
final_param_score = []
for num_feature in num_feature_list:
print('Now testing for ', num_feature, ' features...')
 feature_list = col_list[np.where(LGBM_ranking <= num_feature)]
create_data(33, feature_list, 'LGBM', validation=True, meta=False)
 key_list = np.array(list(params.keys()))
param_dict = {key: np.arange(len(params[key])) for key in key_list}
combinations = list(product(*[param_dict[key] for key in key_list]))
print('Totalling {} combinations to be tested!'.format(len(combinations)))
 count = 0
for combination in combinations:
ts = time.time()
param_combination = {key_list[k_pos]: params[key_list[k_pos]][v_pos] for k_pos, v_pos in enumerate(combination)}
param_combination['metric'] = 'rmse'
model = lgb.train(param_combination,train_set=lgb.Dataset('X_train_LGBM.bin'), num_boost_round=1000, verbose_eval=False,
early_stopping_rounds=10, valid_sets=lgb.Dataset('X_test_LGBM.bin'))
cur_iter = model.current_iteration()
with open('X_test.pickle', 'rb')as handle:
X_test = pickle.load(handle)
 pred = model.predict(X_test)
 with open('y_test.pickle', 'rb')as handle:
y_test = pickle.load(handle)
 score = -mean_squared_error(pred, y_test)
 param_combination['iteration'] = cur_iter
param_combination['num_feature'] = num_feature
final_param_list.append(param_combination)
 final_param_score.append(score)
 del model
del X_test
del y_test
gc.collect()
print(len(gc.get_objects()))
count = count + 1
print(count, '/', len(combinations), ' is done! used time: ', time.time() - ts)
 clear_save_file('X_train_LGBM', file_type='bin')
 clear_save_file('X_test_LGBM', file_type='bin')
 clear_save_file('X_test', file_type='pickle')
 clear_save_file('y_test', file_type='pickle')
 best_idx = np.argmax(final_param_score)
 print('best score is: ', final_param_score[best_idx])
 print('best param combination is: ')
 print(final_param_list[best_idx])
 class feature_selector(BaseEstimator, TransformerMixin):
def __init__(self, feature_rank=0, num_feature=2):
self.feature_rank = feature_rank
self.num_feature = num_feature
self.feature_selected = None
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.feature_selected = np.where(self.feature_rank <= self.num_feature)[0]
print('now number of feature is: ', self.num_feature)
 return X[:, self.feature_selected]
class feature_selector(BaseEstimator, TransformerMixin):
def __init__(self, feature_rank=0, num_feature=2):
self.feature_rank = feature_rank
self.num_feature = num_feature
self.feature_selected = None
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
self.feature_selected = np.where(self.feature_rank <= self.num_feature)[0]
print('now number of feature is: ', self.num_feature)
 return X[:, self.feature_selected]
class get_feature_column(BaseEstimator, TransformerMixin):
def __init__(self, num_component=3):
self.num_component = num_component
def fit(self, X, y=None):
return self
def transform(self, X, y=None):
print('returning number of pca vector: ', self.num_component)
 return X[:, 0: self.num_component]
class my_scaler(BaseEstimator, TransformerMixin):
def __init__(self):
self.std = np.array([])
 self.mean = np.array([])
 def fit(self, X, y=None):
for i in range(X.shape[1]):
std = np.std(X[:, i].astype(np.float32))
mean = np.mean(X[:, i].astype(np.float32))
self.mean = np.append(self.mean, mean)
 self.std = np.append(self.std, std)
 return self
def transform(self, X, y=None):
for i in range(X.shape[1]):
X[:, i] =(X[:, i] - self.mean[i])/ self.std[i]
return X.astype(np.float16)
 def my_grid_search(pipeline, params, X_path, y_path, cv=3, cv_by_index=False):
key_list = np.array(list(params.keys()))
param_dict = {key: np.arange(len(params[key])) for key in key_list}
combinations = list(product(*[param_dict[key] for key in key_list]))
print('Totalling {} combinations to be tested!'.format(len(combinations)))
 param_list = []
param_score = []
for combination in combinations:
param_combination = {key_list[k_pos]: params[key_list[k_pos]][v_pos] for k_pos, v_pos in enumerate(combination)}
my_score = []
if cv_by_index:
train_idx, val_idx = cv
with open(X_path, 'rb')as handle:
X = pickle.load(handle)
 with open(y_path, 'rb')as handle:
y = pickle.load(handle)
 my_pipeline = clone(pipeline)
 my_pipeline.set_params(**param_combination)
 my_pipeline.fit(X[train_idx], y[train_idx])
 val_pred = my_pipeline.predict(X[val_idx])
 val_score = -mean_squared_error(val_pred, y[val_idx])
 my_score.append(val_score)
 del my_pipeline
del X
del y
gc.collect()
len(gc.get_objects())
 else:
for train_idx, val_idx in KFold(n_splits=cv ).split(X):
my_pipeline = clone(pipeline)
 my_pipeline.set_params(**param_combination)
 my_pipeline.fit(X[train_idx], y[train_idx])
 val_pred = my_pipeline.predict(X[val_idx])
 val_score = -mean_squared_error(val_pred, y[val_idx])
 my_score.append(val_score)
 del my_pipeline
gc.collect()
len(gc.get_objects())
 param_list.append(param_combination)
 param_score.append(np.mean(my_score))
best_idx = np.argmax(param_score)
 print('best score is: ', param_score[best_idx])
 print('best param combination is: ')
 print(param_list[best_idx])
 def run_meta_LR(col_list, feature_ranking, meta_train, meta_test, meta_train_month, meta_col=0):
meta_months = [28, 29, 30, 31, 32, 33, 34]
meta_test_month = 34
feature_list = col_list[np.where(feature_ranking <= 80)]
for month in meta_months:
ts = time.time()
create_data(month, feature_list, 'Linear', validation=False, meta=False)
 with open('X_train.pickle', 'rb')as handle:
X_train = pickle.load(handle)
 with open('X_test.pickle', 'rb')as handle:
X_test = pickle.load(handle)
 with open('y_train.pickle', 'rb')as handle:
y_train = pickle.load(handle)
 scaler = my_scaler()
scaler.fit(X_train)
 X_train = scaler.transform(X_train)
 X_test = scaler.transform(X_test)
 model = LinearRegression(normalize=False)
 model.fit(X_train, y_train)
 meta_pred = model.predict(X_test)
 loc =(meta_train_month == month)
 if month != meta_test_month:
meta_train[loc, meta_col] = meta_pred
else:
meta_test[:, meta_col] = meta_pred
del X_train
del X_test
del y_train
clear_save_file('X_train')
 clear_save_file('y_train')
 clear_save_file('X_test')
 gc.collect()
print(len(gc.get_objects()))
print('LR for month ', month, ' is done! used time: ', time.time() - ts)
 def run_meta_LR_PCA(col_list, meta_train, meta_test, meta_train_month, meta_col=1):
meta_months = [28, 29, 30, 31, 32, 33, 34]
meta_test_month = 34
feature_num = 56
for month in meta_months:
ts = time.time()
create_data(month, col_list, 'Linear', validation=False, meta=False)
 with open('X_train.pickle', 'rb')as handle:
X_train = pickle.load(handle)
 with open('X_test.pickle', 'rb')as handle:
X_test = pickle.load(handle)
 with open('y_train.pickle', 'rb')as handle:
y_train = pickle.load(handle)
 print('starting PCA...')
 ipca = IncrementalPCA(batch_size=40)
 ipca.fit(X_train)
 print('PCA trained, now converting X...')
 X_train = ipca.transform(X_train)[:, 0: feature_num]
X_test = ipca.transform(X_test)[:, 0: feature_num]
del ipca
gc.collect()
print(len(gc.get_objects()))
print('PCA finished...')
 model = LinearRegression(normalize=False)
 model.fit(X_train, y_train)
 meta_pred = model.predict(X_test)
 loc =(meta_train_month == month)
 if month != meta_test_month:
meta_train[loc, meta_col] = meta_pred
else:
meta_test[:, meta_col] = meta_pred
del X_train
del X_test
del y_train
clear_save_file('X_train')
 clear_save_file('y_train')
 clear_save_file('X_test')
 gc.collect()
print(len(gc.get_objects()))
print('LR for month ', month, ' is done! used time: ', time.time() - ts)
 def run_meta_LGBM(col_list, feature_ranking, meta_train, meta_test, meta_train_month, meta_col=2):
feature_list = col_list[np.where(feature_ranking < 60)]
meta_months = [28, 29, 30, 31, 32, 33, 34]
meta_test_month = 34
for month in meta_months:
ts = time.time()
create_data(month, feature_list, 'LGBM', validation=False, meta=False)
 print('now running the model...')
 params = {'max_depth': 9, 'num_leaves': 64, 'learning_rate': 0.1, 'metric': 'rmse'}
model = lgb.train(params,train_set=lgb.Dataset('X_train_LGBM.bin'), num_boost_round=80, verbose_eval=False)
 with open('X_test.pickle', 'rb')as handle:
X_test = pickle.load(handle)
 meta_pred = model.predict(X_test)
 loc =(meta_train_month == month)
 if month != meta_test_month:
meta_train[loc, meta_col] = meta_pred
else:
meta_test[:, meta_col] = meta_pred
del X_test
del model
clear_save_file('X_train_LGBM', file_type='bin')
 clear_save_file('X_test')
 gc.collect()
print(len(gc.get_objects()))
print('LGBM for month ', month, ' is done! used time: ', time.time() - ts )
data.show_batch()<compute_test_metric>
with open('df_data.pickle', 'rb')as handle:
df_data = pickle.load(handle)
 meta_train_months = [28, 29, 30, 31, 32, 33]
meta_test_month = [34]
months = df_data.date_block_num
meta_train_months_series = months[months.isin(meta_train_months)]
meta_test_month_series = months[months.isin(meta_test_month)]
y_train_meta = df_data[months.isin(meta_train_months)].item_cnt_month.values
X_train_meta = np.zeros([y_train_meta.shape[0], 3])
 X_test_meta = np.zeros([meta_test_month_series.shape[0], 3])
 del df_data
gc.collect()
print(len(gc.get_objects()))
class FocalLoss(nn.Module):
def __init__(self, gamma=2):
super().__init__()
self.gamma = gamma
def forward(self, logit, target):
target = target.float()
max_val =(-logit ).clamp(min=0)
 loss = logit - logit * target + max_val + \ 
(( -max_val ).exp() +(-logit - max_val ).exp() ).log()
invprobs = F.logsigmoid(-logit *(target * 2.0 - 1.0))
loss =(invprobs * self.gamma ).exp() * loss
if len(loss.size())==2:
loss = loss.sum(dim=1)
 return loss.mean()<choose_model_class>
run_meta_LR(col_list, LR_ranking, X_train_meta, X_test_meta, meta_train_months_series, meta_col=0)
 gc.collect()
print(len(gc.get_objects()))
run_meta_LR_PCA(col_list, X_train_meta, X_test_meta, meta_train_months_series, meta_col=1)
 gc.collect()
print(len(gc.get_objects()))
run_meta_LGBM(col_list, LGBM_ranking, X_train_meta, X_test_meta, meta_train_months_series, meta_col=2)
 gc.collect()
print(len(gc.get_objects()))
learn = cnn_learner(data, base_arch=models.resnet50, loss_func=FocalLoss() , metrics=fbeta )<find_best_params>
model = LinearRegression()
model.fit(X_train_meta, y_train_meta)
learn.lr_find()
learn.recorder.plot()<train_model>
meta_pred = model.predict(X_test_meta)
 gc.collect()
print(len(gc.get_objects()))
learn.unfreeze()
learn.fit_one_cycle(22, slice(1e-5,1e-2))
learn.freeze()
learn.save('stage-1', return_path=True )<predict_on_test>
df_submission = df_test.copy().drop(['shop_id', 'item_id', 'date_block_num'], axis=1)
 df_submission['item_cnt_month'] = meta_pred
df_submission.head()
<categorify><EOS> 
df_submission.to_csv('my_submission.csv', index=False )