Kaggle competition: Ghouls, Goblins, and Ghosts... Boo!
<SOS> metric: categorizationaccuracy Kaggle data source: ghouls,-goblins,-and-ghosts...-boo!<train_on_grid>
import numpy as np
import pandas as pd
n_splits = 5
cv = GroupKFold(n_splits=n_splits)
 kfold_split = list(cv.split(X=train_df.question_body, groups=train_df.question_body))<create_dataframe>
train = pd.read_csv(".. /input/train.csv", index_col=0)
 test = pd.read_csv(".. /input/test.csv", index_col=0 )
n_epochs = 8
patience = 4
scores = []
batch_size = 8
num_features = x_train.shape[2]
num_labels = y_train.shape[1]
for k,(train_idx, valid_idx)in enumerate(kfold_split):
print("k:", k+1)
 x_train_train = x_train[train_idx]
y_train_train = y_train[train_idx]
train_dataset = torch.utils.data.TensorDataset(
 torch.from_numpy(x_train[train_idx]),
torch.from_numpy(y_train[train_idx])
)
 train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True, drop_last=True)
 valid_dataset = torch.utils.data.TensorDataset(
 torch.from_numpy(x_train[valid_idx]),
torch.from_numpy(y_train[valid_idx])
)
 valid_loader = torch.utils.data.DataLoader(valid_dataset, batch_size=batch_size, shuffle=False, drop_last=False)
 model = MODEL_v001(num_features, num_labels)
 model.cuda()
best_weights = copy.deepcopy(model.state_dict())
 loss_fn = nn.BCEWithLogitsLoss()
optimizer = torch.optim.Adam(model.parameters())
 min_loss = np.inf
counter = 0
for epoch in range(n_epochs):
model.train()
train_loss = []
for features, labels in train_loader:
features = torch.tensor(features,dtype=torch.float32 ).cuda()
labels = torch.tensor(labels,dtype=torch.float32 ).cuda()
y_pred = model(features)
 loss = loss_fn(y_pred, labels)
 optimizer.zero_grad()
loss.backward()
optimizer.step()
train_loss.append(loss.item())
 avg_train_loss = np.mean(train_loss)
 model.eval()
val_loss = []
with torch.no_grad() :
for features, labels in valid_loader:
features = torch.tensor(features,dtype=torch.float32 ).cuda()
labels = torch.tensor(labels,dtype=torch.float32 ).cuda()
y_pred = model(features)
 loss = loss_fn(y_pred, labels)
 val_loss.append(loss.item())
 avg_val_loss = np.mean(val_loss)
 if avg_val_loss < min_loss:
min_loss = avg_val_loss
counter = 0
best_weights = copy.deepcopy(model.state_dict())
 else:
counter += 1
if counter >= patience:
break
model.load_state_dict(best_weights)
 model.eval()
valid_preds_fold = []
valid_true_fold = []
with torch.no_grad() :
for features, labels in valid_loader:
features = torch.tensor(features,dtype=torch.float32 ).cuda()
labels = torch.tensor(labels,dtype=torch.float32 ).cuda()
y_pred = model(features)
 valid_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 valid_true_fold.extend(labels.cpu().data.numpy().tolist())
 valid_preds_fold = np.array(valid_preds_fold)
 valid_true_fold = np.array(valid_true_fold)
 add_validation_data(valid_preds_fold, target_columns, valid_idx)
 score = mean_spearmanr_correlation_score(valid_preds_fold, valid_true_fold)
 print('Score:', score)
 scores.append(score)
 test_preds_fold = []
test_dataset = QuestDataset(x_test, None)
 test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=False)
 with torch.no_grad() :
for features in test_loader:
features = torch.tensor(features,dtype=torch.float32 ).cuda()
y_pred = model(features)
 test_preds_fold.extend(torch.sigmoid(y_pred ).cpu().data.numpy().tolist())
 test_preds_fold = np.array(test_preds_fold)
 add_test_pred_data(test_preds_fold, target_columns, 1.0)
 del model, optimizer, loss_fn, best_weights
del train_dataset, train_loader
del valid_dataset, valid_loader
del test_dataset, test_loader
torch.cuda.empty_cache()
gc.collect()
print('================')
 print('Mean score:', np.mean(scores))<prepare_output>
y_train = pd.get_dummies(train[["type"]], prefix="")
 train.drop("type", inplace=True, axis=1)
 train_test = pd.concat([train, test], axis=0)
 train_test.drop("color", inplace=True, axis=1)
 X_train = train_test.iloc[:len(y_train)]
X_test = train_test.iloc[len(y_train):]
del train_test
del train
del test
validationLSTM = pd.DataFrame()
for column in target_columns:
preds = validation_datas[column]
count = validation_counts[column]
count = np.where(count < 0.5, 1.0, count)
 validationLSTM[column] = preds / count<compute_test_metric>
def sigmoid(X):
return 1.0 /(1.0 + np.exp(-X))
def softmax(X):
_sum = np.exp(X ).sum()
return np.exp(X)/ _sum
class neuralnet(object):
def __init__(self, num_input, num_hidden, num_output):
self._W1 =(np.random.random_sample(( num_input, num_hidden)) - 0.5 ).astype(np.float32)
 self._b1 = np.zeros(( 1, num_hidden)).astype(np.float32)
 self._W2 =(np.random.random_sample(( num_hidden, num_output)) - 0.5 ).astype(np.float32)
 self._b2 = np.zeros(( 1, num_output)).astype(np.float32)
 def forward(self,X):
net1 = np.matmul(X, self._W1)+ self._b1
y = sigmoid(net1)
 net2 = np.matmul(y, self._W2)+ self._b2
z = softmax(net2)
 return z,y
def backpropagation(self, X, target, eta):
z, y = self.forward(X)
 d2 =(z - target)
 d1 = y*(1.0-y)* np.matmul(d2, self._W2.T)
 self._W2 -= eta * np.matmul(y.T,d2)
 self._W1 -= eta * np.matmul(X.reshape(( -1,1)) ,d1)
 self._b2 -= eta * d2
self._b1 -= eta * d1
mean_spearmanr_correlation_score(validationLSTM.values, train_df[target_columns].values )<load_from_csv>
num_hidden = 8
n_epochs = 1500
eta = 0.01
nn = neuralnet(X_train.shape[1], num_hidden, y_train.shape[1] )
test_predsLSTM = pd.read_csv(f"{folder}sample_submission.csv")
 for column in target_columns:
output = test_pred_datas[column] / test_pred_weights[column]
test_predsLSTM[column] = output<drop_column>
X = np.array(X_train, dtype=np.float32)
 Y = np.array(y_train, dtype=np.float32)
 for epoch in range(n_epochs):
for monster, target in zip(X,Y):
nn.backpropagation(monster, target, eta )
del train_df, test_df
del x_train, x_test, y_train
del validation_datas, validation_counts
del test_pred_datas, test_pred_weights<load_from_csv>
with open('submission-{}-hidden.csv'.format(num_hidden), 'w')as f:
f.write("id,type
")
 for index, monster in X_test.iterrows() :
probs = nn.forward(np.array(monster, dtype=np.float32)) [0]
f.write("{},{}
".format(index, y_train.columns.values[np.argmax(probs)][1:]))