Kaggle competition: Predict Future Sales
feature_cols = [c for c in process_data(train ).columns if c not in all_target_cols]
feature_cols = [c for c in feature_cols if c not in ['kfold', 'sig_id', 'drug_id']]
num_features = len(feature_cols)
 num_features<define_variables>
warnings.filterwarnings("ignore")
 %matplotlib inline
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 25
BATCH_SIZE = 128
WEIGHT_DECAY = {'ALL_TARGETS': 1e-5, 'SCORED_ONLY': 3e-6}
MAX_LR = {'ALL_TARGETS': 1e-2, 'SCORED_ONLY': 3e-3}
DIV_FACTOR = {'ALL_TARGETS': 1e3, 'SCORED_ONLY': 1e2}
PCT_START = 0.1<choose_model_class>
train_dir = ".. /input/vinbigdata-chest-xray-abnormalities-detection/train"
test_dir = ".. /input/vinbigdata-chest-xray-abnormalities-detection/test"
train_files = os.listdir(train_dir)
 test_files = os.listdir(test_dir)
 train_df = pd.read_csv(".. /input/vinbigdata-chest-xray-abnormalities-detection/train.csv")
 sample_submission = pd.read_csv(".. /input/vinbigdata-chest-xray-abnormalities-detection/sample_submission.csv" )
model = Model(num_features, num_all_targets)
<define_search_model>
train_df.nunique().to_frame().rename(columns={0:"Unique Values"} ).style.background_gradient(cmap="plasma" )
def make_cv_folds(train, SEEDS, NFOLDS, DRUG_THRESH):
vc = train.drug_id.value_counts()
vc1 = vc.loc[vc <= DRUG_THRESH].index.sort_values()
vc2 = vc.loc[vc > DRUG_THRESH].index.sort_values()
for seed_id in range(SEEDS):
kfold_col = 'kfold_{}'.format(seed_id)
 dct1 = {}
dct2 = {}
skf = MultilabelStratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=seed_id)
 tmp = train.groupby('drug_id')[target_cols].mean().loc[vc1]
for fold,(idxT, idxV)in enumerate(skf.split(tmp, tmp[target_cols])) :
dd = {k: fold for k in tmp.index[idxV].values}
dct1.update(dd)
 skf = MultilabelStratifiedKFold(n_splits=NFOLDS, shuffle=True, random_state=seed_id)
 tmp = train.loc[train.drug_id.isin(vc2)].reset_index(drop=True)
 for fold,(idxT, idxV)in enumerate(skf.split(tmp, tmp[target_cols])) :
dd = {k: fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 train[kfold_col] = train.drug_id.map(dct1)
 train.loc[train[kfold_col].isna() , kfold_col] = train.loc[train[kfold_col].isna() , 'sig_id'].map(dct2)
 train[kfold_col] = train[kfold_col].astype('int8')
 return train
SEEDS = 7
NFOLDS = 7
DRUG_THRESH = 18
train = make_cv_folds(train, SEEDS, NFOLDS, DRUG_THRESH)
 train.head() 
<define_variables>
print("Unique images in train_df: ", train_df.image_id.nunique() )
model_path = '.. /input/models-approach-v2/'<split>
for _ in range(3):
print(train_files[random.randint(0, len(train_files)) ] )
def run_inference(fold_id, seed_id):
test_ = process_data(test)
 model = Model(num_features, num_targets)
 model.load_state_dict(torch.load(f"{model_path}/SCORED_ONLY_FOLD{fold_id}_{seed_id}.pth"))
model.to(DEVICE)
 x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 predictions = np.zeros(( len(test_), num_targets))
predictions = inference_fn(model, testloader, DEVICE)
 return predictions 
<define_variables>
sample_fn = train_df.image_id.to_list() [random.randint(0, len(train_df)) ]
sample = pdc.read_file(os.path.join(train_dir, sample_fn + ".dicom"))
sample
SEED = [0, 1, 2, 3, 4, 5, 6]
fold =[0,1,2,3,4,5,6]
predictions = np.zeros(( len(test), len(target_cols)))
 time_begin = time()
for fold_id in fold:
for seed_id in SEED:
predictions_ = run_inference(fold_id, seed_id)
 predictions += predictions_ / len(SEED)
 time_diff = time() - time_begin
test[target_cols] = predictions<feature_engineering>
def read_dicom_df(fn):
_ = pdc.read_file(os.path.join(train_dir, fn))
pass
<merge><EOS> 
sample_submission.to_csv("submission.csv", index=False )