Kaggle competition: Mechanisms of Action (MoA) Prediction
warnings.filterwarnings('ignore')
 plt.style.use('dark_background')
 class Clock:
def __enter__(self):
self.begin_time = time.time()
def __exit__(self, exec_type, exec_value, exec_trace):
print("time:", time.time() - self.begin_time, "s")
 def parallelize_dataframe(df, func, cores=4):

 df_split = np.array_split(df, cores)
 pool = Pool(cores)
 df = pd.concat(pool.map(func, df_split), axis=0)
 pool.close()
pool.join()
return df
def clean_str(text, max_text_length=60):

 try:
text = ' '.join([w for w in text.split() [:max_text_length]])
 text = text.lower()
text = re.sub(u"é", u"e", text)
 text = re.sub(u"ē", u"e", text)
 text = re.sub(u"è", u"e", text)
 text = re.sub(u"ê", u"e", text)
 text = re.sub(u"à", u"a", text)
 text = re.sub(u"â", u"a", text)
 text = re.sub(u"ô", u"o", text)
 text = re.sub(u"ō", u"o", text)
 text = re.sub(u"ü", u"u", text)
 text = re.sub(u"ï", u"i", text)
 text = re.sub(u"ç", u"c", text)
 text = re.sub(u"\u2019", u"'", text)
 text = re.sub(u"\xed", u"i", text)
 text = re.sub(u"w\/", u" with ", text)
 text = re.sub(u"[^a-z0-9]", " ", text)
 text = u" ".join(re.split('(\d+)',text))
text = re.sub(u"\s+", u" ", text ).strip()
except:
text = np.NaN
return text
def take(generator):
try:
for elem in generator:
yield elem
except StopIteration:
return
def fill_brandname(name, brand_name_set):
try:
for size in [4, 3, 2, 1]:
for x in take(ngrams(name.split() , size)) :
if " ".join(x)in brand_name_set:
return " ".join(x)
 return np.NaN
except:
return np.NaN
def clean_str_df(x):
return x.apply(lambda y: clean_str(y))
def fill_brandname_df(x):
global brand_name_set
return x.apply(lambda y: fill_brandname(y, brand_name_set))
def lg(text):
text = [x for x in text.split() if x != '']
return len(text)
 def tokenize(text):
return [w for w in text.split() ]
def get_sample(train_file, test_file, max_text_length=60):
print("读取样本")
 with Clock() :
train = pd.read_csv(train_file, sep='\t', encoding='utf-8')
 train.drop(columns=["train_id"], inplace=True)
 print('train shape:', train.shape, "columns:", train.columns)
 test = pd.read_csv(test_file, sep='\t', encoding='utf-8')
 test.drop(columns=["test_id"], inplace=True)
 print('test shape:', test.shape, "columns:", test.columns)
 train['price'] = np.log1p(train['price'].values)
 sample = pd.concat([train.drop(columns=["price"], axis=0), test], axis=0)
 gc.collect()
with Clock() :
print("处理特征")
 sample["item_description"].fillna("", inplace=True)
 sample["item_description"] = sample["item_description"].apply(lambda x :
x.replace('No description yet',''))
sample["item_description"] = parallelize_dataframe(sample["item_description"], clean_str_df)
 sample["name"] = parallelize_dataframe(sample["name"], clean_str_df)
 sample["brand_name"] = parallelize_dataframe(sample["brand_name"], clean_str_df)
 global brand_name_set
brand_name_set = sample.groupby("brand_name" ).size()
sample.loc[sample["brand_name"].isnull() , "brand_name"] = parallelize_dataframe(
 sample.loc[sample["brand_name"].isnull() , "name"],
fill_brandname_df)
 sample["brand_name"].fillna("", inplace=True)
 sample["item_condition_id"].fillna(2, inplace=True)
 sample["category_name"].fillna("//", inplace=True)
 sample["catery_name_first"] = sample["category_name"].apply(lambda x: x.split("/")[0])
 sample["catery_name_second"] = sample["category_name"].apply(lambda x: x.split("/")[1])
 sample["catery_name_third"] = sample["category_name"].apply(lambda x: x.split("/")[2])
 sample['category_name'] = sample['category_name'].apply(lambda x: ' '.join(x.split('/')).strip())
 sample['nb_words_item_description'] = sample['item_description'].apply(lg ).astype(np.uint16)
 sample['nb_words_item_description'] = 1.0 * sample['nb_words_item_description'] / max_text_length
for x in ['brand_name', 'category_name', 'catery_name_first', 'catery_name_second',
'catery_name_third']:
le = preprocessing.LabelEncoder()
sample[x] = le.fit_transform(sample[x])
 sample["item_condition_id"] = 1.0 * sample["item_condition_id"] / 5
sample["old_name"] = sample["name"].copy()
sample["brand_cat"] = "cat1_" + sample["catery_name_first"].astype(str)+ " " + \
"cat2_" + sample["catery_name_second"].astype(str)+ " " + \
"cat3_" + sample["catery_name_third"].astype(str)+ " " + \
"brand_" + sample["brand_name"].astype(str)
 sample["name"] = sample["brand_cat"] + " " + sample["name"]
sample["name_desc"] = sample["name"] + " " + sample["item_description"].apply(lambda x: " ".join(x.split() [:5]))
gc.collect()
print("构造文本特征:")
 max_features = 200000
with Clock() :
vectorizer = CountVectorizer(lowercase=True, tokenizer=tokenize, stop_words='english',
ngram_range=(1, 2), min_df=5, max_features=max_features,
binary=True, dtype=np.int8)
 vectorizer.fit(sample['name'] + ' ' + sample['item_description'])
 print("构建统计特征:")
 with Clock() :
train_new = sample[:train.shape[0]].copy()
train_new["price"] = train["price"]
for x in ['catery_name_first', 'catery_name_second', 'catery_name_third', 'category_name', 'brand_name' ]:
tmp = train_new.groupby(x)["price"].mean().astype(np.float32)
 sample['mean_price_' + x] = sample[x].map(tmp ).astype(np.float32)
 sample['mean_price_' + x].fillna(tmp.mean() , inplace=True)
 gc.collect()
print("构造样本:")
 with Clock() :
split_num = train.shape[0]
x = {
'sparse_data': vectorizer.transform(sample[:split_num]['name'] +
' ' + sample[:split_num]['item_description']),
'item_condition': np.array(sample['item_condition_id'])[:split_num],
'shipping': np.array(sample["shipping"])[:split_num],
'mean_price_catery_name_second': np.array(sample["mean_price_catery_name_second"])[:split_num],
'nb_words_item_description': np.array(sample["nb_words_item_description"])[:split_num]
}
test = {
'sparse_data': vectorizer.transform(sample[split_num:]['name'] +
' ' + sample[split_num:]['item_description']),
'item_condition': np.array(sample['item_condition_id'])[split_num:],
'shipping': np.array(sample["shipping"])[split_num:],
'mean_price_catery_name_second': np.array(sample["mean_price_catery_name_second"])[split_num:],
'nb_words_item_description': np.array(sample["nb_words_item_description"])[split_num:]
}
y = train.price.values
return x, y, test
def model(max_features=200000):
sparse_data = Input(shape=[max_features], name="sparse_data", dtype='float32', sparse=True)
 item_condition = Input(shape=[1], name="item_condition", dtype='float32')
 shipping = Input(shape=[1], name="shipping", dtype='float32')
 mean_price_catery_name_second = Input(shape=[1], name="mean_price_catery_name_second",
dtype='float32')
 nb_words_item_description = Input(shape=[1], name="nb_words_item_description",
dtype='float32')
 x = Dense(200, kernel_initializer=he_uniform(seed=0))(sparse_data)
 x = PReLU()(x)
 x = concatenate([x, item_condition, shipping, mean_price_catery_name_second,
nb_words_item_description], axis=1)
 x = Dense(200, kernel_initializer=he_uniform(seed=0))(x)
 x = PReLU()(x)
 x = Dense(100, kernel_initializer=he_uniform(seed=0))(x)
 x = PReLU()(x)
 x = Dense(1, kernel_initializer=he_uniform(seed=0))(x)
 m = Model([sparse_data, item_condition, shipping, mean_price_catery_name_second,
nb_words_item_description], x)
 optimizer = Adam (.001)
 m.compile(loss="mse", optimizer=optimizer)
 return m<prepare_x_and_y>
warnings.filterwarnings('ignore' )
path = '.. /input/'
x, y, test = get_sample(path + "train.tsv", path + "test_stg2.tsv", max_text_length=60 )<save_to_csv>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv' )
m = model()
m.summary()
m.fit(x, y - np.mean(y), 2048, epochs=3)
 pred = m.predict(test, 2048)+ np.mean(y)
 pred = np.expm1(pred)
 pred[pred < 3] = 3
pred[pred > 1000] = 1000
test_id= pd.read_csv(path + "test_stg2.tsv", sep='\t', encoding='utf-8')[["test_id"]]
test_id["price"] = pred
test_id.to_csv("ans.csv", index=False )<choose_model_class>
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
lemma = WordNetLemmatizer()<categorify>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )
def clean(text):
text = re.sub('\s+', ' ', text)
 text = re.sub("'", "", text)
 return text
def doc_to_words(doc, lemma):
words = [w for w in gensim.utils.simple_preprocess(str(doc), deacc=True)]
words = [lemma.lemmatize(w)for w in words]
return words<string_transform>
train_targets_scored.sum() [1:].sort_values()
def get_dict_with_split(text_arr):
clean_text_arr = [clean(text)for text in text_arr]
words = [doc_to_words(text, lemma)for text in clean_text_arr]
words_dict = [{w: 1 for w in ww} for ww in words]
return words_dict<string_transform>
n_comp = 50
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
def get_dict_without_split(text_arr):
words_dict = [{w: 1} for w in text_arr]
return words_dict<drop_column>
n_comp = 15
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
def df_drop_price(df):
try:
df = df.drop(['price'], axis=1)
 except KeyError:
pass
return df<data_type_conversions>
var_thresh = VarianceThreshold(threshold=0.5)
 data = train_features.append(test_features)
 data_transformed = var_thresh.fit_transform(data.iloc[:, 4:])
 train_features_transformed = data_transformed[ : train_features.shape[0]]
test_features_transformed = data_transformed[-test_features.shape[0] : ]
train_features = pd.DataFrame(train_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 train_features = pd.concat([train_features, pd.DataFrame(train_features_transformed)], axis=1)
 test_features = pd.DataFrame(test_features[['sig_id','cp_type','cp_time','cp_dose']].values.reshape(-1, 4),\
columns=['sig_id','cp_type','cp_time','cp_dose'])
 test_features = pd.concat([test_features, pd.DataFrame(test_features_transformed)], axis=1)
 train_features
def df_fillna(df):
df['category_name'] = df['category_name'].fillna('NAN')
 df['brand_name'] = df['brand_name'].fillna('NAN')
 df['item_description'] = df['item_description'].fillna('NAN')
 return df<feature_engineering>
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]
def df_append_cats(df):
df['category_level1'] = df['category_name']
df['category_level2'] = df['category_name']
df['category_level3'] = df['category_name']
return df<feature_engineering>
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )
def split_cats(arr):
for i in range(len(arr)) :
try:
s = arr[i,3].split('/')
 arr[i,7] = s[0]
arr[i,8] = s[1]
arr[i,9] = s[2]
except Exception:
arr[i,7] = 'NAN'
arr[i,8] = 'NAN'
arr[i,9] = 'NAN'
return arr<categorify>
def fill_na_descriptions(arr):
arr[np.where(arr[:,6]=='No description yet')[0],6] = arr[np.where(arr[:,6]=='No description yet')[0],1]
arr[np.where(arr[:,6]=='NAN')[0],6] = arr[np.where(arr[:,6]=='NAN')[0],1]
return arr<categorify>
def get_X_from_df(df):
df = df_drop_price(df)
 df = df_fillna(df)
 df = df_append_cats(df)
 X = np.array(df)
 X = split_cats(X)
 X = fill_na_descriptions(X)
 return X<define_variables>
INPUT_PATH = r'.. /input'<load_from_csv>
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()
df_train = pd.read_table(os.path.join(INPUT_PATH, 'train.tsv'), engine='c' )<define_variables>
folds = train.copy()
mskf = MultilabelStratifiedKFold(n_splits=5)
 for f,(t_idx, v_idx)in enumerate(mskf.split(X=train, y=target)) :
folds.loc[v_idx, 'kfold'] = int(f)
 folds['kfold'] = folds['kfold'].astype(int)
 folds
logprice = np.log1p(df_train['price'] )<prepare_x_and_y>
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct
X_train = get_X_from_df(df_train )<feature_engineering>
def train_fn(model, optimizer, scheduler, loss_fn, dataloader, device):
model.train()
final_loss = 0
for data in dataloader:
optimizer.zero_grad()
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 loss.backward()
optimizer.step()
scheduler.step()
final_loss += loss.item()
final_loss /= len(dataloader)
 return final_loss
def valid_fn(model, loss_fn, dataloader, device):
model.eval()
final_loss = 0
valid_preds = []
for data in dataloader:
inputs, targets = data['x'].to(device), data['y'].to(device)
 outputs = model(inputs)
 loss = loss_fn(outputs, targets)
 final_loss += loss.item()
valid_preds.append(outputs.sigmoid().detach().cpu().numpy())
 final_loss /= len(dataloader)
 valid_preds = np.concatenate(valid_preds)
 return final_loss, valid_preds
def inference_fn(model, dataloader, device):
model.eval()
preds = []
for data in dataloader:
inputs = data['x'].to(device)
 with torch.no_grad() :
outputs = model(inputs)
 preds.append(outputs.sigmoid().detach().cpu().numpy())
 preds = np.concatenate(preds)
 return preds
print('names')
 words_dict = get_dict_with_split(X_train[:,1])
 print('dv_names')
 dv_names = DictVectorizer(sparse=True)
 dv_names.fit(words_dict)
 names_vec_matrix = dv_names.transform(words_dict )<define_variables>
class Model(nn.Module):
def __init__(self, num_features, num_targets, hidden_size):
super(Model, self ).__init__()
cha_1 = 256
cha_2 = 512
cha_3 = 512
cha_1_reshape = int(hidden_size/cha_1)
 cha_po_1 = int(hidden_size/cha_1/2)
 cha_po_2 = int(hidden_size/cha_1/2/2)* cha_3
self.cha_1 = cha_1
self.cha_2 = cha_2
self.cha_3 = cha_3
self.cha_1_reshape = cha_1_reshape
self.cha_po_1 = cha_po_1
self.cha_po_2 = cha_po_2
self.batch_norm1 = nn.BatchNorm1d(num_features)
 self.dropout1 = nn.Dropout(0.1)
 self.dense1 = nn.utils.weight_norm(nn.Linear(num_features, hidden_size))
self.batch_norm_c1 = nn.BatchNorm1d(cha_1)
 self.dropout_c1 = nn.Dropout(0.1)
 self.conv1 = nn.utils.weight_norm(nn.Conv1d(cha_1,cha_2, kernel_size = 5, stride = 1, padding=2, bias=False),dim=None)
 self.ave_po_c1 = nn.AdaptiveAvgPool1d(output_size = cha_po_1)
 self.batch_norm_c2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2 = nn.Dropout(0.1)
 self.conv2 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_2, kernel_size = 3, stride = 1, padding=1, bias=True),dim=None)
 self.batch_norm_c2_1 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_1 = nn.Dropout(0.3)
 self.conv2_1 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_2, kernel_size = 3, stride = 1, padding=1, bias=True),dim=None)
 self.batch_norm_c2_2 = nn.BatchNorm1d(cha_2)
 self.dropout_c2_2 = nn.Dropout(0.2)
 self.conv2_2 = nn.utils.weight_norm(nn.Conv1d(cha_2,cha_3, kernel_size = 5, stride = 1, padding=2, bias=True),dim=None)
 self.max_po_c2 = nn.MaxPool1d(kernel_size=4, stride=2, padding=1)
 self.flt = nn.Flatten()
self.batch_norm3 = nn.BatchNorm1d(cha_po_2)
 self.dropout3 = nn.Dropout(0.2)
 self.dense3 = nn.utils.weight_norm(nn.Linear(cha_po_2, num_targets))
def forward(self, x):
x = self.batch_norm1(x)
 x = self.dropout1(x)
 x = F.celu(self.dense1(x), alpha=0.06)
 x = x.reshape(x.shape[0],self.cha_1,
self.cha_1_reshape)
 x = self.batch_norm_c1(x)
 x = self.dropout_c1(x)
 x = F.relu(self.conv1(x))
x = self.ave_po_c1(x)
 x = self.batch_norm_c2(x)
 x = self.dropout_c2(x)
 x = F.relu(self.conv2(x))
x_s = x
x = self.batch_norm_c2_1(x)
 x = self.dropout_c2_1(x)
 x = F.relu(self.conv2_1(x))
x = self.batch_norm_c2_2(x)
 x = self.dropout_c2_2(x)
 x = F.relu(self.conv2_2(x))
x = x * x_s
x = self.max_po_c2(x)
 x = self.flt(x)
 x = self.batch_norm3(x)
 x = self.dropout3(x)
 x = self.dense3(x)
 return x
dv_names = 0<feature_engineering>
def process_data(data):
data = pd.get_dummies(data, columns=['cp_time','cp_dose'])
 return data
print('category1')
 words_dict = get_dict_with_split(X_train[:,7])
 print('dv_cat1')
 dv_cat1 = DictVectorizer(sparse=True)
 dv_cat1.fit(words_dict)
 cat1d_vec_matrix = dv_cat1.transform(words_dict )<define_variables>
feature_cols = [c for c in process_data(folds ).columns if c not in target_cols]
feature_cols = [c for c in feature_cols if c not in ['kfold','sig_id']]
len(feature_cols )
dv_cat1 = 0<feature_engineering>
DEVICE =('cuda' if torch.cuda.is_available() else 'cpu')
 EPOCHS = 25
BATCH_SIZE = 128
LEARNING_RATE = 1e-3
WEIGHT_DECAY = 1e-5
NFOLDS = 5
EARLY_STOPPING_STEPS = 10
EARLY_STOP = False
num_features=len(feature_cols)
 num_targets=len(target_cols)
 hidden_size=4096*4
print('category2')
 words_dict = get_dict_with_split(X_train[:,8])
 print('dv_cat2')
 dv_cat2 = DictVectorizer(sparse=True)
 dv_cat2.fit(words_dict)
 cat2d_vec_matrix = dv_cat2.transform(words_dict )<define_variables>
def run_training(fold, seed):
seed_everything(seed)
 train = process_data(folds)
 test_ = process_data(test)
 trn_idx = train[train['kfold'] != fold].index
val_idx = train[train['kfold'] == fold].index
train_df = train[train['kfold'] != fold].reset_index(drop=True)
 valid_df = train[train['kfold'] == fold].reset_index(drop=True)
 x_train, y_train = train_df[feature_cols].values, train_df[target_cols].values
x_valid, y_valid = valid_df[feature_cols].values, valid_df[target_cols].values
train_dataset = MoADataset(x_train, y_train)
 valid_dataset = MoADataset(x_valid, y_valid)
 trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
 validloader = torch.utils.data.DataLoader(valid_dataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.to(DEVICE)
 optimizer = torch.optim.Adam(model.parameters() , lr=LEARNING_RATE, weight_decay=WEIGHT_DECAY)
 scheduler = optim.lr_scheduler.OneCycleLR(optimizer=optimizer, pct_start=0.01, div_factor=1e3, max_lr=1e-2, epochs=EPOCHS, steps_per_epoch=len(trainloader))
loss_fn = nn.BCEWithLogitsLoss()
early_stopping_steps = EARLY_STOPPING_STEPS
early_step = 0
oof = np.zeros(( len(train), target.iloc[:, 1:].shape[1]))
best_loss = np.inf
for epoch in range(EPOCHS):
train_loss = train_fn(model, optimizer,scheduler, loss_fn, trainloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, train_loss: {train_loss}")
 valid_loss, valid_preds = valid_fn(model, loss_fn, validloader, DEVICE)
 print(f"FOLD: {fold}, EPOCH: {epoch}, valid_loss: {valid_loss}")
 if valid_loss < best_loss:
best_loss = valid_loss
oof[val_idx] = valid_preds
torch.save(model.state_dict() , f"FOLD{fold}_.pth")
 elif(EARLY_STOP == True):
early_step += 1
if(early_step >= early_stopping_steps):
break
x_test = test_[feature_cols].values
testdataset = TestDataset(x_test)
 testloader = torch.utils.data.DataLoader(testdataset, batch_size=BATCH_SIZE, shuffle=False)
 model = Model(
 num_features=num_features,
num_targets=num_targets,
hidden_size=hidden_size, 
)
 model.load_state_dict(torch.load(f"FOLD{fold}_.pth"))
model.to(DEVICE)
 predictions = np.zeros(( len(test_), target.iloc[:, 1:].shape[1]))
predictions = inference_fn(model, testloader, DEVICE)
 return oof, predictions
dv_cat2 = 0<feature_engineering>
def run_k_fold(NFOLDS, seed):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(NFOLDS):
oof_, pred_ = run_training(fold, seed)
 predictions += pred_ / NFOLDS
oof += oof_
return oof, predictions
print('category3')
 words_dict = get_dict_with_split(X_train[:,9])
 print('dv_cat3')
 dv_cat3 = DictVectorizer(sparse=True)
 dv_cat3.fit(words_dict)
 cat3d_vec_matrix = dv_cat3.transform(words_dict )<define_variables>
EPOCHS=25
SEED = [0, 1, 2]
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for seed in SEED:
oof_, predictions_ = run_k_fold(NFOLDS, seed)
 oof += oof_ / len(SEED)
 predictions += predictions_ / len(SEED)
 train[target_cols] = oof
test[target_cols] = predictions
dv_cat3 = 0<feature_engineering>
valid_results = train_targets_scored.drop(columns=target_cols ).merge(train[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = valid_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / target.shape[1]
print("CV log_loss: ", score)
print('description')
 words_dict = get_dict_with_split(X_train[:,6])
 print('dv_des')
 dv_des = DictVectorizer(sparse=True)
 dv_des.fit(words_dict)
 des_vec_matrix = dv_des.transform(words_dict )<define_variables>
sub = sample_submission.drop(columns=target_cols ).merge(test[['sig_id']+target_cols], on='sig_id', how='left' ).fillna(0)
 sub.to_csv('submission.csv', index=False )