From 5cc0627e49abc11488f57c4f6d31cfce3aec34c2 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Wed, 9 Aug 2023 21:13:59 +0200 Subject: [PATCH 1/6] model paralel example --- dicee/dataset_classes.py | 27 ++----- script_model_paralelisim.py | 136 +++++++++++++++++++----------------- 2 files changed, 78 insertions(+), 85 deletions(-) diff --git a/dicee/dataset_classes.py b/dicee/dataset_classes.py index d63a84f0..77ab90c7 100644 --- a/dicee/dataset_classes.py +++ b/dicee/dataset_classes.py @@ -322,7 +322,7 @@ def __init__(self, train_set: np.ndarray, num_entities: int, num_relations: int, self.neg_sample_ratio = torch.tensor( neg_sample_ratio) # 0 Implies that we do not add negative samples. This is needed during testing and validation self.train_set = torch.from_numpy(train_set).unsqueeze(1) - #assert num_entities >= max(self.train_set[:, 0]) and num_entities >= max(self.train_set[:, 2]) + # assert num_entities >= max(self.train_set[:, 0]) and num_entities >= max(self.train_set[:, 2]) self.length = len(self.train_set) self.num_entities = torch.tensor(num_entities) self.num_relations = torch.tensor(num_relations) @@ -335,27 +335,12 @@ def __getitem__(self, idx): triple = self.train_set[idx] - y = torch.ones(1) + corr_entities = torch.randint(0, high=self.num_entities, size=(1,)) + negative_triple = torch.cat((triple[:, 0], triple[:, 1], corr_entities), dim=0).unsqueeze(0) - - - - - negative_triple=torch.cat((triple[:,0],triple[:,1],triple[:,2]),dim=0) - - print(triple.shape) - print(negative_triple.shape) - exit(1) - - y = torch.ones(0) - - print(triple.shape) - x=torch.cat((triple,negative_triple),dim=1) - - print(x) - exit(1) - # Workaround to create negative triples - return triple, y + x = torch.cat((triple, negative_triple), dim=0) + y=torch.tensor([1.0, 0.0]) + return x,y class TriplePredictionDataset(torch.utils.data.Dataset): diff --git a/script_model_paralelisim.py b/script_model_paralelisim.py index 736c45cf..04c3ecc6 100644 --- a/script_model_paralelisim.py +++ b/script_model_paralelisim.py @@ -1,83 +1,91 @@ import torch import dicee from dicee import DistMult +from dicee import NegSampleDataset import polars as pl import time +import pandas as pd print("Reading KG...", end=" ") -start_time=time.time() -data = pl.read_parquet("dbpedia-2022-12-nt.parquet.snappy")#,n_rows=100_000_000) -print(f"took {time.time()-start_time}") +start_time = time.time() + +# data = pl.read_parquet("dbpedia-2022-12-nt.parquet.snappy") # ,n_rows=100_000_000) +data = pl.read_csv("KGs/UMLS/train.txt", + has_header=False, + low_memory=False, + columns=[0, 1, 2], + dtypes=[pl.Utf8], # str + new_columns=['subject', 'relation', 'object'], + separator="\t") + +print(f"took {time.time() - start_time}") print("Unique entities...", end=" ") -start_time=time.time() -unique_entities=pl.concat((data.get_column('subject'),data.get_column('object'))).unique().rename('entity').to_list() -print(f"took {time.time()-start_time}") +start_time = time.time() +unique_entities = pl.concat((data.get_column('subject'), data.get_column('object'))).unique().rename('entity').to_list() +print(f"took {time.time() - start_time}") print("Unique relations...", end=" ") -start_time=time.time() -unique_relations=data.unique(subset=["relation"]).select("relation").to_series().to_list() -print(f"took {time.time()-start_time}") +start_time = time.time() +unique_relations = data.unique(subset=["relation"]).select("relation").to_series().to_list() +print(f"took {time.time() - start_time}") print("Entity index mapping...", end=" ") -start_time=time.time() -entity_to_idx={ ent:idx for idx, ent in enumerate(unique_entities)} +start_time = time.time() +entity_to_idx = {ent: idx for idx, ent in enumerate(unique_entities)} +print(f"took {time.time() - start_time}") print("Relation index mapping...", end=" ") -start_time=time.time() -rel_to_idx={ rel:idx for idx, rel in enumerate(unique_relations)} -print(f"took {time.time()-start_time}") - -print("Construcing training data...", end=" ") -start_time=time.time() -data=data.with_columns( pl.col("subject").map_dict(entity_to_idx).alias("subject"), pl.col("relation").map_dict(rel_to_idx).alias("relation"), pl.col("object").map_dict(entity_to_idx).alias("object")).to_numpy() -print(f"took {time.time()-start_time}") -print("Deleting dataframe...", end=" ") - - +start_time = time.time() +rel_to_idx = {rel: idx for idx, rel in enumerate(unique_relations)} +print(f"took {time.time() - start_time}") + +print("Constructing training data...", end=" ") +start_time = time.time() +data = data.with_columns(pl.col("subject").map_dict(entity_to_idx).alias("subject"), + pl.col("relation").map_dict(rel_to_idx).alias("relation"), + pl.col("object").map_dict(entity_to_idx).alias("object")).to_numpy() +print(f"took {time.time() - start_time}") + +data = NegSampleDataset(train_set=data, + num_entities=len(unique_entities), num_relations=len(unique_relations), + neg_sample_ratio=1.0) +data = torch.utils.data.DataLoader(data, batch_size=64, shuffle=True) print("KGE model...", end=" ") -start_time=time.time() -model=DistMult(args={"num_entities":len(entity_to_idx),"num_relations":len(rel_to_idx),"embedding_dim":20}) - -model.to(torch.device("cuda:0")) - -print(f"took {time.time()-start_time}", end=" ") +start_time = time.time() +model1 = DistMult(args={"num_entities": len(entity_to_idx), "num_relations": len(rel_to_idx), + "embedding_dim": 20, 'learning_rate': 0.01}) +model2 = DistMult(args={"num_entities": len(entity_to_idx), "num_relations": len(rel_to_idx), + "embedding_dim": 20, 'learning_rate': 0.01}) +model1.to(torch.device("cuda:0")) +model2.to(torch.device("cuda:1")) + +print(f"took {time.time() - start_time}", end=" ") print("Optimizer...") -start_time=time.time() -optimizer = model.configure_optimizers() - - -loss_function = model.loss_function +start_time = time.time() +optim1 = model1.configure_optimizers() +optim2 = model2.configure_optimizers() -batch_size=10_000 -start_index=0 +loss_function = model1.loss_function print("Training...") -i_step=0 -for x in data[start_index:start_index+batch_size]: - - optimizer.zero_grad(set_to_none=True) - - x=torch.from_numpy(x).int().to("cuda:0").unsqueeze(0) - - yhat=model.forward(x) - y=torch.ones(len(yhat),device="cuda:0") - - - - batch_positive_loss=loss_function(yhat,y) - - if i_step%100==0: - print(i_step,batch_size,batch_positive_loss) - - start_index+=batch_size - i_step +=1 - - if i_step<=10: - batch_size+=batch_size - - if i_step==1000: - break - - -print(torch.cuda.memory_summary()) -print(torch.cuda.mem_get_info()) +for e in range(1): + epoch_loss = 0 + for (x, y) in data: + x = x.flatten(start_dim=0, end_dim=1) + y = y.flatten(start_dim=0, end_dim=1) + + optim1.zero_grad(set_to_none=True) + optim2.zero_grad(set_to_none=True) + # cpu = + yhat = (model1.forward(x.pin_memory().to("cuda:0", non_blocking=True)).to("cpu") + model2.forward(x.pin_memory().to("cuda:1", non_blocking=True)).to("cpu")) / 2 + + batch_positive_loss = loss_function(yhat, y) + epoch_loss += batch_positive_loss.item() + batch_positive_loss.backward() + optim1.step() + optim2.step() + + print(l / len(data)) + +print(model1.entity_embeddings.weight[:3]) +print(model2.entity_embeddings.weight[:3]) print('DONE') From 76903169815cb8d8479d40b6a30673963e91de81 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 10 Aug 2023 15:37:06 +0200 Subject: [PATCH 2/6] Unsed pykeen model init. related function rempved --- dicee/executer.py | 4 +--- dicee/static_funcs.py | 22 +++++----------------- dicee/trainer/dice_trainer.py | 6 ++---- main.py | 2 +- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/dicee/executer.py b/dicee/executer.py index 587fca24..30dd7e9f 100644 --- a/dicee/executer.py +++ b/dicee/executer.py @@ -198,9 +198,7 @@ def start(self) -> dict: self.trainer = DICE_Trainer(args=self.args, is_continual_training=self.is_continual_training, storage_path=self.storage_path, - evaluator=self.evaluator, - dataset=self.dataset # only used for Pykeen's models - ) + evaluator=self.evaluator) # (4) Start the training self.trained_model, form_of_labelling = self.trainer.start(dataset=self.dataset) return self.end(form_of_labelling) diff --git a/dicee/static_funcs.py b/dicee/static_funcs.py index 4b34628c..f340ad76 100644 --- a/dicee/static_funcs.py +++ b/dicee/static_funcs.py @@ -40,7 +40,7 @@ def load_pickle(file_path=str): # @TODO: Could these funcs can be merged? -def select_model(args: dict, is_continual_training: bool = None, storage_path: str = None, dataset=None): +def select_model(args: dict, is_continual_training: bool = None, storage_path: str = None): isinstance(args, dict) assert len(args) > 0 assert isinstance(is_continual_training, bool) @@ -58,7 +58,7 @@ def select_model(args: dict, is_continual_training: bool = None, storage_path: s print(f"{storage_path}/model.pt is not found. The model will be trained with random weights") return model, _ else: - return intialize_model(args, dataset) + return intialize_model(args) def load_model(path_of_experiment_folder, model_name='model.pt') -> Tuple[object, dict, dict]: @@ -194,13 +194,11 @@ def save_checkpoint_model(model, path: str) -> None: print(model.name) print('Could not save the model correctly') else: - # Pykeen torch.save(model.model.state_dict(), path) def store(trainer, - trained_model, model_name: str = 'model', full_storage_path: str = None, - dataset=None, save_embeddings_as_csv=False) -> None: + trained_model, model_name: str = 'model', full_storage_path: str = None, save_embeddings_as_csv=False) -> None: """ Store trained_model model and save embeddings into csv file. :param trainer: an instance of trainer class @@ -287,22 +285,12 @@ def read_or_load_kg(args, cls): return kg -def get_pykeen_model(model_name: str, args, dataset): - if dataset is None: - # (1) Load a pretrained Pykeen Model - return PykeenKGE(args=args) - elif args['scoring_technique'] in ['KvsAll', "NegSample"]: - return PykeenKGE(args=args) - else: - raise NotImplementedError("Incorrect scoring technique") - - -def intialize_model(args: dict, dataset=None) -> Tuple[object, str]: +def intialize_model(args: dict) -> Tuple[object, str]: # @TODO: Apply construct_krone as callback? or use KronE_QMult as a prefix. # @TODO: Remove form_of_labelling model_name = args['model'] if "pykeen" in model_name.lower(): - model = get_pykeen_model(model_name, args, dataset) + model = PykeenKGE(args=args) form_of_labelling = "EntityPrediction" elif model_name == 'Shallom': model = Shallom(args=args) diff --git a/dicee/trainer/dice_trainer.py b/dicee/trainer/dice_trainer.py index 2413f2e3..d2367fec 100644 --- a/dicee/trainer/dice_trainer.py +++ b/dicee/trainer/dice_trainer.py @@ -92,7 +92,7 @@ class DICE_Trainer: report:dict """ - def __init__(self, args, is_continual_training, storage_path, evaluator=None, dataset=None): + def __init__(self, args, is_continual_training, storage_path, evaluator=None): self.report = dict() self.args = args self.trainer = None @@ -101,7 +101,6 @@ def __init__(self, args, is_continual_training, storage_path, evaluator=None, da # Required for CV. self.evaluator = evaluator self.form_of_labelling = None - self.dataset = dataset print( f'# of CPUs:{os.cpu_count()} | # of GPUs:{torch.cuda.device_count()} | # of CPUs for dataloader:{self.args.num_core}') @@ -144,8 +143,7 @@ def initialize_trainer(self, callbacks: List, plugins: List) -> pl.Trainer: @timeit def initialize_or_load_model(self): print('Initializing Model...', end='\t') - model, form_of_labelling = select_model(vars(self.args), self.is_continual_training, self.storage_path, - self.dataset) + model, form_of_labelling = select_model(vars(self.args), self.is_continual_training, self.storage_path) self.report['form_of_labelling'] = form_of_labelling assert form_of_labelling in ['EntityPrediction', 'RelationPrediction'] return model, form_of_labelling diff --git a/main.py b/main.py index 26d6a1a4..89b59414 100755 --- a/main.py +++ b/main.py @@ -27,7 +27,7 @@ def get_default_arguments(description=None): help="A flag for saving embeddings in csv file.") # Model related arguments parser.add_argument("--model", type=str, - default="Pykeen_MuRE", + default="Keci", choices=["ConEx", "AConEx", "ConvQ", "AConvQ", "ConvO", "AConvO", "QMult", "OMult", "Shallom", "DistMult", "TransE", "ComplEx", "Keci", "Pykeen_QuatE", "Pykeen_DistMult", "Pykeen_BoxE", "Pykeen_CP", From d2e86c41b2092aace66b56d95e8304cb3e02f161 Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 10 Aug 2023 15:54:00 +0200 Subject: [PATCH 3/6] Increased the new version --- dicee/__init__.py | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/dicee/__init__.py b/dicee/__init__.py index 3d0accfe..29da1a62 100644 --- a/dicee/__init__.py +++ b/dicee/__init__.py @@ -4,4 +4,4 @@ from .knowledge_graph_embeddings import KGE # noqa from .executer import Execute # noqa from .dataset_classes import * # noqa -__version__ = '0.0.4' +__version__ = '0.0.5' diff --git a/setup.py b/setup.py index a2c444a9..f008553d 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ setup( name='dicee', description='Dice embedding is an hardware-agnostic framework for large-scale knowledge graph embedding applications', - version='0.0.4', + version='0.0.5', packages=find_packages(), install_requires=[ "torch>=2.0.0", From 87b8eedb82c7aa02d7477ba2c03ed2f427ca942e Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 10 Aug 2023 16:08:07 +0200 Subject: [PATCH 4/6] Aligning dependencies listed in the README, requirements and setup --- README.md | 10 +++++----- requirements.txt | 13 ++++++------- setup.py | 1 + 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b4874d3d..1b89d87a 100644 --- a/README.md +++ b/README.md @@ -40,19 +40,19 @@ pip install dicee ``` or ```bash -pip3 install "pandas>=1.5.1" pip3 install "torch>=2.0.0" +pip3 install "pandas>=1.5.1" pip3 install "polars>=0.16.14" pip3 install "scikit-learn>=1.2.2" pip3 install "pyarrow>=11.0.0" -pip3 install "pytest>=7.2.2" -pip3 install "gradio>=3.23.0" -pip3 install "psutil>=5.9.4" pip3 install "pytorch-lightning==1.6.4" pip3 install "pykeen==1.10.1" pip3 install "zstandard>=0.21.0" +pip3 install "pytest>=7.2.2" +pip3 install "psutil>=5.9.4" +pip3 install "ruff>=0.0.284" +pip3 install "gradio>=3.23.0" pip3 install "rdflib>=7.0.0" -pip3 install "ruff>=0.0.283" ``` To test the Installation diff --git a/requirements.txt b/requirements.txt index 0a18a2cf..2f11d8c3 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,14 +1,13 @@ -pandas>=1.5.1 torch>=2.0.0 -torch --index-url https://download.pytorch.org/whl/cpu +pandas>=1.5.1 polars>=0.16.14 scikit-learn>=1.2.2 pyarrow>=11.0.0 -pytest>=7.2.2 -gradio>=3.23.0 -psutil>=5.9.4 pytorch-lightning==1.6.4 pykeen==1.10.1 zstandard>=0.21.0 -ruff -rdflib>=7.0.0 \ No newline at end of file +pytest>=7.2.2 +psutil>=5.9.4 +ruff>=0.0.284 +gradio>=3.23.0 +rdflib>=7.0.0 diff --git a/setup.py b/setup.py index f008553d..7eb90e39 100644 --- a/setup.py +++ b/setup.py @@ -17,6 +17,7 @@ "zstandard>=0.21.0", "pytest>=7.2.2", "psutil>=5.9.4", + "ruff>=0.0.284", "gradio>=3.23.0", "rdflib>=7.0.0"], author='Caglar Demir', From a341d35cee71647fc8b236384bee7c65a6dd7c0b Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 10 Aug 2023 16:53:36 +0200 Subject: [PATCH 5/6] Model paralelisim with cpu-offloading example script added --- dicee/models/real.py | 3 +- examples/script_model_paralelisim.py | 117 +++++++++++++++++++++++++++ script_model_paralelisim.py | 91 --------------------- 3 files changed, 119 insertions(+), 92 deletions(-) create mode 100644 examples/script_model_paralelisim.py delete mode 100644 script_model_paralelisim.py diff --git a/dicee/models/real.py b/dicee/models/real.py index efa16fed..0647804b 100644 --- a/dicee/models/real.py +++ b/dicee/models/real.py @@ -33,7 +33,8 @@ def forward_k_vs_sample(self, x: torch.LongTensor, target_entity_idx: torch.Long t = self.entity_embeddings(target_entity_idx).transpose(1, 2) return torch.bmm(hr, t).squeeze(1) - + def score(self,h,r,t): + return (self.hidden_dropout(self.hidden_normalizer(h * r)) * t).sum(dim=1) class TransE(BaseKGE): """ Translating Embeddings for Modeling diff --git a/examples/script_model_paralelisim.py b/examples/script_model_paralelisim.py new file mode 100644 index 00000000..a0e565a4 --- /dev/null +++ b/examples/script_model_paralelisim.py @@ -0,0 +1,117 @@ +import torch +import dicee +from dicee import DistMult +from dicee import NegSampleDataset +import polars as pl +import time +import pandas as pd +import numpy as np + +if True: + print("Reading KG...") + start_time = time.time() + data = pl.read_parquet("dbpedia-2022-12-nt.parquet.snappy",n_rows=10000) +#data = pl.read_csv("KGs/UMLS/train.txt", +# has_header=False, +# low_memory=False, +# columns=[0, 1, 2], +# dtypes=[pl.Utf8], # str +# new_columns=['subject', 'relation', 'object'], +# separator="\t") + print(f"took {time.time() - start_time}") + print("Unique entities...") + start_time = time.time() + unique_entities = pl.concat((data.get_column('subject'), data.get_column('object'))).unique().rename('entity').to_list() + print(f"took {time.time() - start_time}") + + print("Unique relations...") + start_time = time.time() + unique_relations = data.unique(subset=["relation"]).select("relation").to_series().to_list() + print(f"took {time.time() - start_time}") + + print("Entity index mapping...") + start_time = time.time() + entity_to_idx = {ent: idx for idx, ent in enumerate(unique_entities)} + print(f"took {time.time() - start_time}") + + print("Relation index mapping...") + start_time = time.time() + rel_to_idx = {rel: idx for idx, rel in enumerate(unique_relations)} + print(f"took {time.time() - start_time}") + + print("Constructing training data...") + start_time = time.time() + data = data.with_columns(pl.col("subject").map_dict(entity_to_idx).alias("subject"), + pl.col("relation").map_dict(rel_to_idx).alias("relation"), + pl.col("object").map_dict(entity_to_idx).alias("object")).to_numpy() + print(f"took {time.time() - start_time}") + + num_entities=len(unique_entities) + num_relations=len(unique_relations) + + + + with open("data.npy", 'wb') as f: + np.save(f, data) +else: + with open("data.npy", 'rb') as f: + data = np.load(f) + + num_entities=1+max(max(data[:,0]),max(data[:,2])) + num_relations=1+max(data[:,1]) + +data = NegSampleDataset(train_set=data, + num_entities=num_entities, num_relations=num_relations, + neg_sample_ratio=1.0) +data = torch.utils.data.DataLoader(data, batch_size=1024, shuffle=True) +print("KGE model...") +start_time = time.time() +model1 = DistMult(args={"optim":"SGD","num_entities": num_entities, "num_relations": num_relations,"embedding_dim": 10, 'learning_rate': 0.001}) +print(model1) +model2 = DistMult(args={"optim":"SGD","num_entities": num_entities, "num_relations": num_relations,"embedding_dim": 10, 'learning_rate': 0.001}) +print(model2) + +#model1 = torch.compile(model1) +#model2 = torch.compile(model2) +# TODO: We need to cpu-offlaoding +# Do not sent the eintiere model to GPU but only the batch +#model1.to(device=torch.device("cuda:0"),dtype=torch.float16) +#model2.to(device=torch.device("cuda:1"),dtype=torch.float16) + + +print(f"took {time.time() - start_time}") + +print("Optimizer...") +start_time = time.time() +optim1 = model1.configure_optimizers() +optim2 = model2.configure_optimizers() + +from tqdm import tqdm + + +loss_function = model1.loss_function +print("Training...") +for e in range(1): + epoch_loss = 0 + + for (x, y) in tqdm(data): + # if we have space in GPU, get the next batch + x = x.flatten(start_dim=0, end_dim=1) + y = y.flatten(start_dim=0, end_dim=1) + optim1.zero_grad(set_to_none=True) + optim2.zero_grad(set_to_none=True) + + start_time=time.time() + # CPU + + yhat=(model1.score(*model1.get_triple_representation(x)) + model2.score(*model2.get_triple_representation(x)))/2 + + batch_positive_loss = loss_function(yhat, y) + epoch_loss += batch_positive_loss.item() + batch_positive_loss.backward() + optim1.step() + optim2.step() + + print(epoch_loss / len(data)) + +print('DONE') diff --git a/script_model_paralelisim.py b/script_model_paralelisim.py deleted file mode 100644 index 04c3ecc6..00000000 --- a/script_model_paralelisim.py +++ /dev/null @@ -1,91 +0,0 @@ -import torch -import dicee -from dicee import DistMult -from dicee import NegSampleDataset -import polars as pl -import time -import pandas as pd - -print("Reading KG...", end=" ") -start_time = time.time() - -# data = pl.read_parquet("dbpedia-2022-12-nt.parquet.snappy") # ,n_rows=100_000_000) -data = pl.read_csv("KGs/UMLS/train.txt", - has_header=False, - low_memory=False, - columns=[0, 1, 2], - dtypes=[pl.Utf8], # str - new_columns=['subject', 'relation', 'object'], - separator="\t") - -print(f"took {time.time() - start_time}") -print("Unique entities...", end=" ") -start_time = time.time() -unique_entities = pl.concat((data.get_column('subject'), data.get_column('object'))).unique().rename('entity').to_list() -print(f"took {time.time() - start_time}") - -print("Unique relations...", end=" ") -start_time = time.time() -unique_relations = data.unique(subset=["relation"]).select("relation").to_series().to_list() -print(f"took {time.time() - start_time}") - -print("Entity index mapping...", end=" ") -start_time = time.time() -entity_to_idx = {ent: idx for idx, ent in enumerate(unique_entities)} -print(f"took {time.time() - start_time}") - -print("Relation index mapping...", end=" ") -start_time = time.time() -rel_to_idx = {rel: idx for idx, rel in enumerate(unique_relations)} -print(f"took {time.time() - start_time}") - -print("Constructing training data...", end=" ") -start_time = time.time() -data = data.with_columns(pl.col("subject").map_dict(entity_to_idx).alias("subject"), - pl.col("relation").map_dict(rel_to_idx).alias("relation"), - pl.col("object").map_dict(entity_to_idx).alias("object")).to_numpy() -print(f"took {time.time() - start_time}") - -data = NegSampleDataset(train_set=data, - num_entities=len(unique_entities), num_relations=len(unique_relations), - neg_sample_ratio=1.0) -data = torch.utils.data.DataLoader(data, batch_size=64, shuffle=True) -print("KGE model...", end=" ") -start_time = time.time() -model1 = DistMult(args={"num_entities": len(entity_to_idx), "num_relations": len(rel_to_idx), - "embedding_dim": 20, 'learning_rate': 0.01}) -model2 = DistMult(args={"num_entities": len(entity_to_idx), "num_relations": len(rel_to_idx), - "embedding_dim": 20, 'learning_rate': 0.01}) -model1.to(torch.device("cuda:0")) -model2.to(torch.device("cuda:1")) - -print(f"took {time.time() - start_time}", end=" ") -print("Optimizer...") -start_time = time.time() -optim1 = model1.configure_optimizers() -optim2 = model2.configure_optimizers() - -loss_function = model1.loss_function -print("Training...") -for e in range(1): - epoch_loss = 0 - for (x, y) in data: - x = x.flatten(start_dim=0, end_dim=1) - y = y.flatten(start_dim=0, end_dim=1) - - optim1.zero_grad(set_to_none=True) - optim2.zero_grad(set_to_none=True) - # cpu = - yhat = (model1.forward(x.pin_memory().to("cuda:0", non_blocking=True)).to("cpu") + model2.forward(x.pin_memory().to("cuda:1", non_blocking=True)).to("cpu")) / 2 - - batch_positive_loss = loss_function(yhat, y) - epoch_loss += batch_positive_loss.item() - batch_positive_loss.backward() - optim1.step() - optim2.step() - - print(l / len(data)) - -print(model1.entity_embeddings.weight[:3]) -print(model2.entity_embeddings.weight[:3]) -print('DONE') From 85ca41da6323918710859168993ef2a6250b2a6b Mon Sep 17 00:00:00 2001 From: Caglar Demir Date: Thu, 10 Aug 2023 16:56:05 +0200 Subject: [PATCH 6/6] unsed file deleted --- environment.yml | 135 ------------------------------------------------ 1 file changed, 135 deletions(-) delete mode 100644 environment.yml diff --git a/environment.yml b/environment.yml deleted file mode 100644 index f669c9b1..00000000 --- a/environment.yml +++ /dev/null @@ -1,135 +0,0 @@ -name: daikiri -channels: - - defaults -dependencies: - - _libgcc_mutex=0.1=main - - _openmp_mutex=4.5=1_gnu - - ca-certificates=2021.10.26=h06a4308_2 - - certifi=2021.10.8=py39h06a4308_2 - - ld_impl_linux-64=2.35.1=h7274673_9 - - libffi=3.3=he6710b0_2 - - libgcc-ng=9.3.0=h5101ec6_17 - - libgomp=9.3.0=h5101ec6_17 - - libstdcxx-ng=9.3.0=hd4cf53a_17 - - ncurses=6.3=h7f8727e_2 - - openssl=1.1.1m=h7f8727e_0 - - pip=21.2.4=py39h06a4308_0 - - python=3.9.7=h12debd9_1 - - readline=8.1.2=h7f8727e_1 - - sqlite=3.37.0=hc218d9a_0 - - tk=8.6.11=h1ccaba5_0 - - tzdata=2021e=hda174b7_0 - - wheel=0.37.1=pyhd3eb1b0_0 - - xz=5.2.5=h7b6447c_0 - - zlib=1.2.11=h7f8727e_4 - - pip: - - absl-py==1.0.0 - - aiohttp==3.8.1 - - aiosignal==1.2.0 - - analytics-python==1.4.0 - - anyio==3.5.0 - - asdfghjkl==0.1a2 - - asgiref==3.5.0 - - async-timeout==4.0.2 - - attrs==21.4.0 - - backoff==1.10.0 - - backpack-for-pytorch==1.4.0 - - bcrypt==3.2.0 - - bokeh==2.4.2 - - cachetools==5.0.0 - - cffi==1.15.0 - - charset-normalizer==2.0.11 - - click==8.0.3 - - cloudpickle==2.0.0 - - cryptography==36.0.1 - - cycler==0.11.0 - - dask==2022.1.0 - - distributed==2022.1.0 - - einops==0.4.0 - - fastapi==0.73.0 - - ffmpy==0.3.0 - - fonttools==4.29.1 - - frozenlist==1.3.0 - - fsspec==2022.1.0 - - future==0.18.2 - - google-auth==2.6.0 - - google-auth-oauthlib==0.4.6 - - gradio==2.7.5.2 - - grpcio==1.43.0 - - h11==0.13.0 - - heapdict==1.0.1 - - idna==3.3 - - importlib-metadata==4.10.1 - - iniconfig==1.1.1 - - isodate==0.6.1 - - jinja2==3.0.3 - - joblib==1.1.0 - - kiwisolver==1.3.2 - - laplace-torch==0.1a2 - - locket==0.2.1 - - markdown==3.3.6 - - markdown2==2.4.2 - - markupsafe==2.0.1 - - matplotlib==3.5.1 - - monotonic==1.6 - - msgpack==1.0.3 - - multidict==6.0.2 - - numpy==1.22.2 - - oauthlib==3.2.0 - - packaging==21.3 - - pandas==1.4.0 - - paramiko==2.9.2 - - partd==1.2.0 - - pillow==9.0.1 - - pluggy==1.0.0 - - protobuf==3.19.4 - - psutil==5.9.0 - - py==1.11.0 - - pyarrow==6.0.1 - - pyasn1==0.4.8 - - pyasn1-modules==0.2.8 - - pycparser==2.21 - - pycryptodome==3.14.1 - - pydantic==1.9.0 - - pydeprecate==0.3.1 - - pydub==0.25.1 - - pynacl==1.5.0 - - pyparsing==3.0.7 - - pytest==6.2.5 - - python-dateutil==2.8.2 - - python-multipart==0.0.5 - - pytorch-lightning==1.5.9 - - pytz==2021.3 - - pyyaml==6.0 - - rdflib==6.1.1 - - requests==2.27.1 - - requests-oauthlib==1.3.1 - - rsa==4.8 - - scikit-learn==1.0.2 - - scipy==1.7.3 - - setuptools==59.5.0 - - six==1.16.0 - - sniffio==1.2.0 - - sortedcontainers==2.4.0 - - starlette==0.17.1 - - tblib==1.7.0 - - tensorboard==2.8.0 - - tensorboard-data-server==0.6.1 - - tensorboard-plugin-wit==1.8.1 - - threadpoolctl==3.1.0 - - toml==0.10.2 - - toolz==0.11.2 - - torch==1.10.2 - - torchaudio==0.10.2 - - torchmetrics==0.7.1 - - torchvision==0.11.3 - - tornado==6.1 - - tqdm==4.62.3 - - typing-extensions==4.0.1 - - urllib3==1.26.8 - - uvicorn==0.17.4 - - werkzeug==2.0.2 - - yarl==1.7.2 - - zict==2.0.0 - - zipp==3.7.0 -prefix: /home/demir/anaconda3/envs/daikiri