Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexicons #14

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
163 changes: 163 additions & 0 deletions examples/basic_model_DRNN.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
import numpy as np
import pandas as pd
import torch
import torch.nn as nn
from torch.optim import Adam

from ignite.metrics import Loss, Accuracy
from torch.utils.data import DataLoader, SubsetRandomSampler
from torchvision.transforms import Compose
from sklearn.model_selection import KFold

from slp.data.collators import SequenceClassificationCollator
from slp.data.therapy_title import PsychologicalDataset, TupleDataset
from slp.data.transforms import SpacyTokenizer, ToTokenIds, ToTensor, ReplaceUnknownToken
from slp.modules.basic_model_DRNN import HierAttNet
from slp.util.embeddings import EmbeddingsLoader
from slp.trainer.trainer_title_no_validation import SequentialTrainer

#DEVICE = 'cpu'
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'

COLLATE_FN = SequenceClassificationCollator(device=DEVICE)

DEBUG = False
KFOLD = True
MAX_EPOCHS = 50

def dataloaders_from_indices(dataset, train_indices, val_indices, batch_train, batch_val):
train_sampler = SubsetRandomSampler(train_indices)
val_sampler = SubsetRandomSampler(val_indices)

train_loader = DataLoader(
dataset,
batch_size=batch_train,
sampler=train_sampler,
drop_last=False,
num_workers=0,
collate_fn=COLLATE_FN)
val_loader = DataLoader(
dataset,
batch_size=batch_val,
num_workers=0,
sampler=val_sampler,
drop_last=False,
collate_fn=COLLATE_FN)

return train_loader, val_loader

def train_test_split(dataset, batch_train, batch_val,
test_size=0.1, shuffle=True, seed=42):
dataset_size = len(dataset)
indices = list(range(dataset_size))
test_split = int(np.floor(test_size * dataset_size))
if shuffle:
if seed is not None:
np.random.seed(seed)
np.random.shuffle(indices)

train_indices = indices[test_split:]
val_indices = indices[:test_split]

return dataloaders_from_indices(dataset, train_indices, val_indices, batch_train, batch_val)


def kfold_split(dataset, batch_train, batch_val, k=5, shuffle=True, seed=None):
kfold = KFold(n_splits=k, shuffle=shuffle, random_state=seed)
for train_indices, val_indices in kfold.split(dataset):
yield dataloaders_from_indices(dataset, train_indices, val_indices, batch_train, batch_val)

def trainer_factory(embeddings, device=DEVICE):
model = HierAttNet(
hidden_size, batch_size, num_classes, max_sent_length, len(embeddings), embeddings)
model = model.to(DEVICE)
criterion = nn.CrossEntropyLoss()
optimizer = Adam(model.parameters(), lr=0.0005)

metrics = {
'accuracy': Accuracy(),
'loss': Loss(criterion)
}

trainer = SequentialTrainer(
model,
optimizer,
checkpoint_dir='../checkpoints' if not DEBUG else None,
metrics=metrics,
non_blocking=True,
patience=10,
loss_fn=criterion,
device=DEVICE)

return trainer


if __name__ == '__main__':

####### Parameters ########
batch_train = 4
batch_val = 4

max_sent_length = 500 #max number of sentences (turns) in transcript - after padding
max_word_length = 150 #max length of each sentence (turn) - after padding
num_classes = 2
batch_size = 4
hidden_size = 300

epochs = 40

# loader = EmbeddingsLoader('../data/glove.6B.300d.txt', 300)
loader = EmbeddingsLoader('/data/embeddings/glove.840B.300d.txt', 300)
word2idx, idx2word, embeddings = loader.load()
embeddings = torch.tensor(embeddings)

tokenizer = SpacyTokenizer()
replace_unknowns = ReplaceUnknownToken()
to_token_ids = ToTokenIds(word2idx)
to_tensor = ToTensor(device=DEVICE)

bio = PsychologicalDataset(
'../data/balanced_new_csv.csv', '../../../test_CEL/slp/data/psychotherapy/',
max_word_length,
text_transforms = Compose([
tokenizer,
replace_unknowns,
to_token_ids,
to_tensor]))




if KFOLD:
cv_scores = []
import gc
for train_loader, val_loader in kfold_split(bio, batch_train, batch_val):
trainer = trainer_factory(embeddings, device=DEVICE)
fold_score = trainer.fit(train_loader, val_loader, epochs=MAX_EPOCHS)
cv_scores.append(fold_score)
print("**********************")
print("edw")
print(fold_score)
del trainer
gc.collect()
final_score = float(sum(cv_scores)) / len(cv_scores)
else:
train_loader, val_loader = train_test_split(bio, batch_train, batch_val)
trainer = trainer_factory(embeddings, device=DEVICE)
final_score = trainer.fit(train_loader, val_loader, epochs=MAX_EPOCHS)

print(f'Final score: {final_score}')




if DEBUG:
print("Starting end to end test")
print("-----------------------------------------------------------------------")
trainer.fit_debug(train_loader, val_loader)
print("Overfitting single batch")
print("-----------------------------------------------------------------------")
trainer.overfit_single_batch(train_loader)
# else:
# print("started the else part")
# trainer.fit(train_loader, val_loader, epochs = epochs)
59 changes: 59 additions & 0 deletions examples/kernel-lexicon-algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import padasip as pa
import numpy as np
import matplotlib.pylab as plt

from torch.utils.data import DataLoader, SubsetRandomSampler

#from slp.data.diction import seeds_diction
from slp.util.embeddings import EmbeddingsLoader
from slp.data.transforms import SpacyTokenizer
from slp.data.therapy_lexicon import PsychologicalDataset, TupleDataset
from sklearn.metrics.pairwise import cosine_similarity

DATASET = '../../../whole-dataset.csv'


if __name__ == '__main__':

Kseeds = 200
max_word_length = 150
# seed_set = list(seeds_diction.keys())

loader = EmbeddingsLoader('/data/embeddings/glove.840B.300d.txt', 300)
word2idx, idx2word, embeddings = loader.load()

tokenizer = SpacyTokenizer()
bio = PsychologicalDataset(
DATASET,
'../../../test_CEL/slp/data/psychotherapy',
max_word_length,
text_transforms = tokenizer)

corpus = []
for i, (text, title, lab) in enumerate(bio):
corpus.extend(text)



import pdb; pdb.set_trace()
corpus = np.unique(corpus)
vocabulary = [word for word in corpus if word not in seed_set]
Nwords = len(vocabulary)

#x-input matrix initialization
x = np.zeros(Kseeds, Nwords)
i = 0
for word in vocabulary:
wv = word2idx[word]
j = 0
for seed in seed_set:
ws = word2idx[seed]
d = cosine_similarity(wv, ws)
x[i][j] = d * seeds_diction[seed]
j += 1
i += 1

#filter definition
f = pa.filters.FilterLMS(n=Nwords, mu=0.01, w="random")
mul = np.matmul(x, d)
f.run(mul, x)
Loading