Kaggle competition: Gendered Pronoun Resolution
<SOS> metric: MulticlassLoss Kaggle data source: gendered-pronoun-resolution<define_variables>
print(os.listdir(".. /input"))
sample_submission = pd.read_csv(".. /input/gendered-pronoun-resolution/sample_submission_stage_1.csv")
 final_test = pd.read_csv(".. /input/gendered-pronoun-resolution/test_stage_2.tsv", sep = "\t")
 nlp = spacy.load('en_core_web_sm')
 dep = ["ACL", "ACOMP", "ADVCL", "ADVMOD", "AGENT", "AMOD", "APPOS", "ATTR", "AUX", "AUXPASS",
"CASE", "CC", "CCOMP", "COMPOUND", "CONJ", "CSUBJ", "CSUBJPASS", "DATIVE", "DEP", "DET", "DOBJ"
, "EXPL", "INTJ", "MARK", "META", "NEG", "NOUNMOD", "NPMOD", "NSUBJ", "NSUBJPASS", "NUMMOD"
, "OPRD", "PARATAXIS", "PCOMP", "POBJ", "POSS", "PRECONJ", "PREDET", "PREP", "PRT", "PUNCT", "QUANTMOD",
"RELCL", "ROOT", "XCOMP", "COMPLM","INFMOD","PARTMOD","HMOD","HYPH","IOBJ","NUM",
"NUMBER","NMOD","NN","NPADVMOD","POSSESSIVE","RCMOD","SUBTOK"]
paths, labels = getTrainDataset()
keys = np.arange(paths.shape[0], dtype=np.int)
 np.random.seed(SEED)
 np.random.shuffle(keys)
 lastTrainIndex = int(( 1-VAL_RATIO)* paths.shape[0])
 pathsTrain = paths[0:lastTrainIndex]
labelsTrain = labels[0:lastTrainIndex]
pathsVal = paths[lastTrainIndex:]
labelsVal = labels[lastTrainIndex:]
print(paths.shape, labels.shape)
 print(pathsTrain.shape, labelsTrain.shape, pathsVal.shape, labelsVal.shape)
 tg = ProteinDataGenerator(pathsTrain, labelsTrain, BATCH_SIZE, SHAPE, use_cache=True, augment = False, shuffle = False)
 vg = ProteinDataGenerator(pathsVal, labelsVal, BATCH_SIZE, SHAPE, use_cache=True, shuffle = False)
 checkpoint = ModelCheckpoint('./base.model', monitor='val_loss', verbose=1, save_best_only=True, save_weights_only=False, mode='min', period=1)
 reduceLROnPlato = ReduceLROnPlateau(monitor='val_loss', factor=0.5, patience=3, verbose=1, mode='min' )<train_model>
import tensorflow as tf
epochs = 100
use_multiprocessing = False
workers = 1
hist = model.fit_generator(
 tg,
steps_per_epoch=len(tg),
validation_data=vg,
validation_steps=8,
epochs=epochs,
use_multiprocessing=use_multiprocessing,
workers=workers,
verbose=1,
callbacks=[checkpoint] )<load_pretrained>
!wget https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-development.tsv
!wget https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-validation.tsv
!wget https://raw.githubusercontent.com/google-research-datasets/gap-coreference/master/gap-test.tsv
!ls
bestModel = load_model('./base.model', custom_objects={'f1': f1} )<define_variables>
train_data = pd.read_csv("gap-development.tsv", sep = "\t")
 validation_data = pd.read_csv("gap-validation.tsv", sep = "\t")
 test_data = pd.read_csv("gap-test.tsv", sep = "\t" )
fullValGen = vg<predict_on_test>
merge_data = pd.concat([train_data,validation_data] ).reset_index(drop = True)
 merge_data = pd.concat([merge_data,train_data] ).reset_index(drop = True)
 count = 0
lastFullValPred = np.empty(( 0, 28))
lastFullValLabels = np.empty(( 0, 28))
for i in tqdm(range(len(fullValGen))):
im, lbl = fullValGen[i]
scores = bestModel.predict(im)
 lastFullValPred = np.append(lastFullValPred, scores, axis=0)
 lastFullValLabels = np.append(lastFullValLabels, lbl, axis=0)
 print(lastFullValPred.shape, lastFullValLabels.shape )<find_best_params>
feature = get_features(merge_data)
rng = np.arange(0, 1, 0.001)
 f1s = np.zeros(( rng.shape[0], 28))
for j,t in enumerate(tqdm(rng)) :
for i in range(28):
p = np.array(lastFullValPred[:,i]>t, dtype=np.int8)
 scoref1 = off1(lastFullValLabels[:,i], p, average='binary')
 f1s[j,i] = scoref1<compute_test_metric>
Y = merge_data[["A-coref", "B-coref"]]
Y.columns = ["A","B"]
Y["A"] = Y["A"].astype(int)
 Y["B"] = Y["B"].astype(int)
 Y["NEITHER"] = 1-(Y["A"] + Y["B"] )
print('Individual F1-scores for each class:')
 print(np.max(f1s, axis=0))
print('Macro F1-score CV =', np.mean(np.max(f1s, axis=0)) )<load_from_csv>
x1, x2, y1, y2 = model_selection.train_test_split(feature.fillna(-1), Y, test_size=0.2, random_state=1)
 x1.head()
x2.head()
y2
pathsTest, labelsTest = getTestDataset()
testg = ProteinDataGenerator(pathsTest, labelsTest, BATCH_SIZE, SHAPE)
 submit = pd.read_csv(DIR + '/sample_submission.csv')
 P = np.zeros(( pathsTest.shape[0], 28))
for i in tqdm(range(len(testg))):
images, labels = testg[i]
score = bestModel.predict(images)
 P[i*BATCH_SIZE:i*BATCH_SIZE+score.shape[0]] = score<prepare_x_and_y>
scaler = StandardScaler()
x1 = scaler.fit_transform(x1)
 x2 = scaler.transform(x2)
 model = multiclass.OneVsRestClassifier(ensemble.RandomForestClassifier(max_depth = 7, n_estimators=1000, random_state=33))
model.fit(x1, y1)
 print('log_loss', metrics.log_loss(y2, model.predict_proba(x2)) )
PP = np.array(P )<save_to_csv>
final_test = pd.read_csv(".. /input/gendered-pronoun-resolution/test_stage_2.tsv", sep = "\t")
 feature = get_features(final_test)
 print(feature)
prediction = []
for row in tqdm(range(submit.shape[0])) :
str_label = ''
for col in range(PP.shape[1]):
if(PP[row, col] < T[col]):
str_label += ''
else:
str_label += str(col)+ ' '
prediction.append(str_label.strip())
 submit['Predicted'] = np.array(prediction)
 submit.to_csv('4channels_cnn_from_scratch.csv', index=False )<set_options>
feature = feature.fillna(-1)
 print(feature)
warnings.filterwarnings("ignore" )<load_from_csv>
Y = pd.DataFrame(model.predict_proba(feature ).tolist() , columns=["A","B", "NEITHER"])
 r = final_test[["ID"]]
submission = pd.concat([r,Y], axis = 1 )
<train_model><EOS> 
print(submission)
 submission.to_csv('submission.csv', index=False )