This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from keras import backend as K | |
from keras import callbacks | |
from keras import layers | |
from keras import models | |
from keras.wrappers.scikit_learn import KerasClassifier | |
import pandas as pd | |
import tensorflow as tf | |
from sklearn import metrics |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import torch | |
import torch.nn as nn | |
import torch.nn.functional as F | |
# Focal loss implementation inspired by | |
# https://github.com/c0nn3r/RetinaNet/blob/master/focal_loss.py | |
# https://github.com/doiken23/pytorch_toolbox/blob/master/focalloss2d.py | |
class MultiClassBCELoss(nn.Module): | |
def __init__(self, | |
use_weight_mask=False, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def load_toxic_data(tox_path): | |
tox = pd.read_csv(tox_path) | |
#remove ' ' before and after text | |
tox['text'] = tox['text'].map(lambda x: str(x).lstrip().rstrip()) | |
#toxic = 1, other = 0 | |
tox['sentiment'] = tox['sentiment'].map(lambda x: 0 if x in ['positive','neutral'] else 1) | |
toxic_text, toxic_labels = tox.text.values, tox.sentiment.values | |
return toxic_text, toxic_labels |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def load_ner_data(ner_path, seq_len=24): | |
data = pd.read_csv(ner_path, encoding= 'unicode_escape', sep=',') | |
data = data.fillna(method='ffill') | |
grouped_s = data.groupby('Sentence #', as_index=True)['Word'].apply(lambda g: ' '.join(g)) | |
grouped_t = data.groupby('Sentence #', as_index=True)['Tag'].apply(lambda g: ' '.join(g)) | |
ner_tr = pd.DataFrame({}, columns=['sentence', 'tag'] ) | |
ner_tr['sentence'] = [st for st in grouped_s.values if len(st.split())<=seq_len] | |
ner_tr['tag'] = [ tg.split() for tg in grouped_t if len(tg.split())<=seq_len] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TripletGenerator: | |
def __init__(self, datadict, hard_frac = 0.5, batch_size=256): | |
self.datadict = datadict | |
self._anchor_idx = np.array(list(self.datadict.keys())) | |
self._hard_frac = hard_frac | |
self.generator = self.generate_batch(batch_size) | |
def generate_batch(self, size): | |
while True: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MulticlassGenerator: | |
def __init__(self, data_tuple, batch_size=256): | |
self._data = data_tuple | |
self._idx = np.arange(len(data_tuple[-1])) | |
self.generator = self.generate_batch(batch_size) | |
def generate_batch(self, size): | |
while True: | |
px_ids = np.random.choice(self._idx, size, replace=False) | |
samples = [p[px_ids] for p in self._data[:-1]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MultitaskDataGenerator: | |
def __init__(self, generators): | |
self.generators = generators | |
self.generator = self.generate_batch() | |
def generate_batch(self, batch_size=None): | |
while True: | |
batch = self.__next__() | |
yield batch | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def softmax_loss(vectors): | |
anc, pos, neg = vectors | |
c = 0.5 | |
anc = c * anc | |
pos = c * pos | |
neg = c * neg | |
pos_sim = tf.reduce_sum((anc * pos), axis=-1, keepdims=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SBERT: | |
def __init__(self, config): | |
self.loss = 0 | |
self.metrics = [] | |
self.inputs = [] | |
self.config = config | |
self.build() | |
def build(self): | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AucCallback(Callback): | |
def __init__(self, dataset, call_model=None, savepath=None, name="AUC"): | |
self.call_model = call_model | |
self.dataset = dataset | |
self.best = 0 | |
self.name = name | |
self.savepath = savepath | |
super(AucCallback, self).__init__() |
OlderNewer