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

Jahanvi rajput patch 1 #44

Open
wants to merge 58 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
58 commits
Select commit Hold shift + click to select a range
6936688
setCover.py
JahanviRajput Jan 7, 2024
a266b9d
setCover.py
JahanviRajput Jan 7, 2024
78f51b0
Create NaiveGreedyOptimizer.py
JahanviRajput Jan 16, 2024
47f571e
Delete cpp/optimizers/NaiveGreedy.py
JahanviRajput Jan 16, 2024
8a2603d
Create SetFunction.py
JahanviRajput Jan 16, 2024
95e7cd2
Create LazierThanLazyGreedyOptimizer.py
JahanviRajput Jan 16, 2024
0715bb1
Create LazyGreedyOptimizer.py
JahanviRajput Jan 16, 2024
3820a32
Create NaiveGreedyOptimizer.py
JahanviRajput Jan 16, 2024
5dfbdf4
Create StochasticGreedyOptimizer.py
JahanviRajput Jan 16, 2024
75bfd10
Create SetCover.py
JahanviRajput Jan 16, 2024
f9b07b9
Delete cpp/SetFunction.py
JahanviRajput Jan 17, 2024
40178a3
Create SetFunction.py
JahanviRajput Jan 17, 2024
4386d15
Create SetCover.py
JahanviRajput Jan 17, 2024
b5659d8
Delete cpp/submod/SetCover.py
JahanviRajput Jan 17, 2024
820c78f
Create LazierThanLazyGreedyOptimizer.py
JahanviRajput Jan 17, 2024
63c9e06
Create LazyGreedyOptimizer.py
JahanviRajput Jan 17, 2024
ba15c2f
Create NaiveGreedyOptimizer.py
JahanviRajput Jan 17, 2024
1259666
Create StochasticGreedyOptimizer.py
JahanviRajput Jan 17, 2024
99f9870
Update SetFunction.py
JahanviRajput Jan 17, 2024
eba36f7
Delete pytorch/optimizer/StochasticGreedyOptimizer.py
JahanviRajput Jan 17, 2024
eef8085
Create StochasticGreedyOptimizer
JahanviRajput Jan 17, 2024
cb235d9
Create __init__.py
JahanviRajput Jan 17, 2024
92be253
Delete cpp/optimizers/LazierThanLazyGreedyOptimizer.py
JahanviRajput Jan 17, 2024
d2a9aba
Delete cpp/optimizers/LazyGreedyOptimizer.py
JahanviRajput Jan 17, 2024
50ec126
Delete cpp/optimizers/NaiveGreedyOptimizer.py
JahanviRajput Jan 17, 2024
4276916
Delete cpp/optimizers/StochasticGreedyOptimizer.py
JahanviRajput Jan 17, 2024
9c997e7
Create ProbabilisticSetCover.py
JahanviRajput Jan 17, 2024
52a30c5
Update SetCover.py
JahanviRajput Jan 17, 2024
036d04b
Create __init__.py
JahanviRajput Jan 17, 2024
1d7d014
Update ProbabilisticSetCover.py
JahanviRajput Jan 17, 2024
694f358
Update __init__.py
JahanviRajput Jan 17, 2024
698f1f6
Rename StochasticGreedyOptimizer to StochasticGreedyOptimize.pyr
JahanviRajput Jan 19, 2024
00f7f9f
Rename StochasticGreedyOptimize.pyr to StochasticGreedyOptimize.py
JahanviRajput Jan 19, 2024
f268159
Update setCover.py
JahanviRajput Jan 19, 2024
4bc9555
Update setCover.py
JahanviRajput Jan 23, 2024
2a27493
Update setup.py
JahanviRajput Jan 23, 2024
aeaaaef
Fixed dependency tree for submodlib GPU implementation
amajee11us Jan 23, 2024
57397c7
Added fix for null object issue
amajee11us Jan 24, 2024
ed70d00
Create GraphCut.py
JahanviRajput Jan 30, 2024
52828be
Update __init__.py
JahanviRajput Jan 30, 2024
f8765d9
Update GraphCut.py
JahanviRajput Jan 30, 2024
72e82c9
pytorch version of helper.py
JahanviRajput Jan 31, 2024
7cc626e
dense mode of with helper functions GraphCut.py
JahanviRajput Feb 4, 2024
766e432
Required functions of helper.py
JahanviRajput Feb 4, 2024
1efe9bf
Function of dense mode only FacilityLocation.py
JahanviRajput Feb 4, 2024
7fac287
Function with all modes implemented DisparityMin.py
JahanviRajput Feb 6, 2024
1cac380
All modes are implemented DisparitySum.py
JahanviRajput Feb 6, 2024
9464fb8
Dense mode is implemented GraphCut.py
JahanviRajput Feb 6, 2024
fbbd7ba
Dense mode is implemented GraphCut.py
JahanviRajput Feb 6, 2024
0fb08d6
Function with all modes implemented DisparityMin.py
JahanviRajput Feb 6, 2024
a026e5b
Function with all modes implemented DisparitySum.py
JahanviRajput Feb 6, 2024
078194a
Function of dense mode only FacilityLocation.py
JahanviRajput Feb 6, 2024
5265d84
Update SetCover.py
JahanviRajput Feb 6, 2024
6cf311b
Update __init__.py
JahanviRajput Feb 6, 2024
de6ae41
Dense mode is done LogDeterminant.py
JahanviRajput Feb 6, 2024
6e9eee5
Create_kernel_sklearn updated for batchwise calculation on cuda
JahanviRajput Feb 12, 2024
4e2eab6
Cuda facilityLocation.py
JahanviRajput Feb 13, 2024
b5a0f2a
coda facilityLocation.py
JahanviRajput Feb 13, 2024
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
Prev Previous commit
Next Next commit
dense mode of with helper functions GraphCut.py
  • Loading branch information
JahanviRajput committed Feb 4, 2024
commit 7cc626e337ea16fe62c95a56a59439e6fc8e2673
121 changes: 78 additions & 43 deletions pytorch/submod/GraphCut.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from typing import List, Set

class GraphCut(SetFunction):
def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=None, ggsijs=None, data=None, data_rep=None, metric="cosine", num_neighbors=None,
import random
from helper import *

class GraphCutpy(SetFunction):
# def __init__(self, n: int, mode: str, metric: str, master_ground_kernel: List[List[float]] = None,
# ground_ground_kernel: List[List[float]] = None, arr_val: List[float] = None,
# arr_count: List[int] = None, arr_col: List[int] = None, partial: bool = False,
# ground: Set[int] = None, lambdaVal: float = 0.0):
def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=None, ggsijs=None, data=None, data_rep=None, metric="cosine", num_neighbors=None,
master_ground_kernel: List[List[float]] = None,
ground_ground_kernel: List[List[float]] = None, arr_val: List[float] = None,
arr_count: List[int] = None, arr_col: List[int] = None, partial: bool = False,
Expand All @@ -27,7 +33,6 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
self.cluster_map=None
self.ggsijs = None
self.mgsijs = None
# self.cpp_ground_sub = {-1} #Provide a dummy set for pybind11 binding to be successful
self.content = None
self.effective_ground = None

Expand All @@ -38,14 +43,14 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
raise Exception("ERROR: Incorrect mode. Must be one of 'dense' or 'sparse'")
if self.separate_rep == True:
if self.n_rep is None or self.n_rep <=0:
raise Exception("ERROR: separate represented intended but number of elements in represented not specified or not positive")
raise Exception("ERROR: separate represented intended but number of elements in represented not specified or not positive")
if self.mode != "dense":
raise Exception("Only dense mode supported if separate_rep = True")
if (type(self.mgsijs) != type(None)) and (type(self.mgsijs) != np.ndarray):
raise Exception("mgsijs provided, but is not dense")
if (type(self.ggsijs) != type(None)) and (type(self.ggsijs) != np.ndarray):
raise Exception("ggsijs provided, but is not dense")

if mode == "dense":
self.master_ground_kernel = master_ground_kernel
self.ground_ground_kernel = ground_ground_kernel
Expand All @@ -66,14 +71,16 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
if partial:
self.original_to_partial_index_map = {elem: ind for ind, elem in enumerate(self.effective_ground_set)}

self.total_similarity_with_subset = [0] * self.num_effective_ground_set
self.total_similarity_with_master = [0] * self.num_effective_ground_set
self.total_similarity_with_subset = [random.random() for _ in range(self.num_effective_ground_set)]
self.total_similarity_with_master = [random.random() for _ in range(self.num_effective_ground_set)]
self.master_ground_kernel = [[random.random() for _ in range(self.num_effective_ground_set)] for _ in range(self.num_effective_ground_set)]
self.ground_ground_kernel = [[random.random() for _ in range(self.num_effective_ground_set)] for _ in range(self.num_effective_ground_set)]
for elem in self.effective_ground_set:
index = self.original_to_partial_index_map[elem] if partial else elem
self.total_similarity_with_subset[index] = 0
self.total_similarity_with_master[index] = 0
# for j in self.master_set:
# self.total_similarity_with_master[index] += self.master_ground_kernel[j][elem]
self.total_similarity_with_subset[index] = 1
self.total_similarity_with_master[index] = 1
for j in self.master_set:
self.total_similarity_with_master[index] += self.master_ground_kernel[j][elem]

if self.separate_rep == True:
if type(self.mgsijs) == type(None):
Expand All @@ -82,7 +89,9 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
raise Exception("Data missing to compute mgsijs")
if np.shape(self.data)[0]!=self.n or np.shape(self.data_rep)[0]!=self.n_rep:
raise Exception("ERROR: Inconsistentcy between n, n_rep and no of examples in the given ground data matrix and represented data matrix")
self.mgsijs = np.array(subcp.create_kernel_NS(self.data.tolist(),self.data_rep.tolist(), self.metric))

#create_kernel_NS is there .................... find it and define it not found in helper.py but used as here
# self.mgsijs = np.array(subcp.create_kernel_NS(self.data.tolist(),self.data_rep.tolist(), self.metric))
else:
#provided mgsijs - verify it's dimensionality
if np.shape(self.mgsijs)[1]!=self.n or np.shape(self.mgsijs)[0]!=self.n_rep:
Expand All @@ -94,8 +103,7 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
if self.num_neighbors is not None:
raise Exception("num_neighbors wrongly provided for dense mode")
self.num_neighbors = np.shape(self.data)[0] #Using all data as num_neighbors in case of dense mode
self.content = np.array(subcp.create_kernel(self.data.tolist(), self.metric, self.num_neighbors))
print(self.content)
self.content = np.array(create_kernel(X = torch.tensor(self.data), metric = self.metric, num_neigh = self.num_neighbors).to_dense())
val = self.cpp_content[0]
row = list(self.cpp_content[1].astype(int))
col = list(self.cpp_content[2].astype(int))
Expand All @@ -114,7 +122,7 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
if self.num_neighbors is not None:
raise Exception("num_neighbors wrongly provided for dense mode")
self.num_neighbors = np.shape(self.data)[0] #Using all data as num_neighbors in case of dense mode
self.content = np.array(subcp.create_kernel(self.data.tolist(), self.metric, self.num_neighbors))
self.content = np.array(create_kernel(X = torch.tensor(self.data), metric = self.metric, num_neigh = self.num_neighbors).to_dense())
val = self.content[0]
row = list(self.content[1].astype(int))
col = list(self.content[2].astype(int))
Expand Down Expand Up @@ -143,7 +151,7 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non
#no kernel is provided make ggsij sparse kernel
if type(data) == type(None):
raise Exception("Data missing to compute ggsijs")
self.content = np.array(subcp.create_kernel(self.data.tolist(), self.metric, self.num_neighbors))
self.content = np.array(create_kernel(X = torch.tensor(self.data), metric = self.metric, num_neigh = self.num_neighbors).to_dense())
val = self.content[0]
row = list(self.content[1].astype(int))
col = list(self.content[2].astype(int))
Expand All @@ -168,26 +176,24 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non

if self.mode=="dense" and self.separate_rep == False :
self.ggsijs = self.ggsijs.tolist() #break numpy ndarray to native list of list datastructure

if type(self.ggsijs[0])==int or type(self.ggsijs[0])==float: #Its critical that we pass a list of list to pybind11
#This condition ensures the same in case of a 1D numpy array (for 1x1 sim matrix)
l=[]
l.append(self.ggsijs)
self.ggsijs=l

# self.cpp_obj = GraphCut(self.n, self.cpp_ggsijs, False, self.cpp_ground_sub, self.lambdaVal)

elif self.mode=="dense" and self.separate_rep == True :
self.ggsijs = self.ggsijs.tolist() #break numpy ndarray to native list of list datastructure

if type(self.ggsijs[0])==int or type(self.ggsijs[0])==float: #Its critical that we pass a list of list to pybind11
#This condition ensures the same in case of a 1D numpy array (for 1x1 sim matrix)
l=[]
l.append(self.ggsijs)
self.ggsijs=l

self.mgsijs = self.mgsijs.tolist() #break numpy ndarray to native list of list datastructure

if type(self.mgsijs[0])==int or type(self.mgsijs[0])==float: #Its critical that we pass a list of list to pybind11
#This condition ensures the same in case of a 1D numpy array (for 1x1 sim matrix)
l=[]
Expand All @@ -198,17 +204,18 @@ def __init__(self, n, mode, lambdaVal, separate_rep=None, n_rep=None, mgsijs=Non

elif self.mode == "sparse":
self.ggsijs = {}
self.ggsijs['arr_val'] = self.ggsijs.data.tolist() #contains non-zero values in matrix (row major traversal)
self.ggsijs['arr_count'] = self.ggsijs.indptr.tolist() #cumulitive count of non-zero elements upto but not including current row
self.ggsijs['arr_col'] = self.ggsijs.indices.tolist() #contains col index corrosponding to non-zero values in arr_val
# self.cpp_obj = GraphCutpy(self.n, self.cpp_ggsijs['arr_val'], self.cpp_ggsijs['arr_count'], self.cpp_ggsijs['arr_col'], lambdaVal)
# self.ggsijs['arr_val'] = self.ggsijs.data.tolist() #contains non-zero values in matrix (row major traversal)
# self.ggsijs['arr_count'] = self.ggsijs.indptr.tolist() #cumulitive count of non-zero elements upto but not including current row
# self.ggsijs['arr_col'] = self.ggsijs.indices.tolist() #contains col index corrosponding to non-zero values in arr_val
# # self.cpp_obj = GraphCutpy(self.n, self.cpp_ggsijs['arr_val'], self.cpp_ggsijs['arr_count'], self.cpp_ggsijs['arr_col'], lambdaVal)
else:
raise Exception("Invalid")

self.effective_ground = self.get_effective_ground_set()
print("it is done")

# mode == "sparse":
# if mode == "dense":

# elif mode == "sparse":
# if not arr_val or not arr_count or not arr_col:
# raise ValueError("Error: Empty/Corrupt sparse similarity kernel")

Expand Down Expand Up @@ -291,31 +298,59 @@ def marginal_gain(self, X: Set[int], item: int) -> float:
for elem in effective_x:
gain -= 2 * self.lambda_ * self.sparse_kernel.get_val(item, elem)
gain -= self.lambda_ * self.sparse_kernel.get_val(item, item)

return gain

def marginal_gain_with_memoization(self, X: Set[int], item: int, enable_checks: bool = True) -> float:
effective_x = X.intersection(self.effective_ground_set) if self.partial else X
# def marginal_gain_with_memoization(self, X: Set[int], item: int, enable_checks: bool = True) -> float:
# effective_x = X.intersection(self.effective_ground_set) if self.partial else X

if enable_checks and item in effective_x:
return 0
# if enable_checks and item in effective_x:
# return 0

if self.partial and item not in self.effective_ground_set:
return 0
# if self.partial and item not in self.effective_ground_set:
# return 0

# gain = 0

# if self.mode == "dense":
# index = self.original_to_partial_index_map[item] if self.partial else item
# gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index]
# gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index] - self.lambda_ * self.ground_ground_kernel[item][item]

# elif self.mode == "sparse":
# index = self.original_to_partial_index_map[item] if self.partial else item
# gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index] - self.lambda_ * self.sparse_kernel.get_val(item, item)

# return gain


def marginal_gain_with_memoization(self, X: Set[int], item: int, enable_checks: bool) -> float:
effective_X = set()
gain = 0
if self.partial:
effective_X = X.intersection(self.effective_ground_set)
else:
effective_X = X

if self.mode == "dense":
index = self.original_to_partial_index_map[item] if self.partial else item
gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index]
# gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index] - self.lambda_ * self.ground_ground_kernel[item][item]
if enable_checks and item in effective_X:
return 0

elif self.mode == "sparse":
index = self.original_to_partial_index_map[item] if self.partial else item
gain = self.total_similarity_with_master[index] - 2 * self.lambda_ * self.total_similarity_with_subset[index] - self.lambda_ * self.sparse_kernel.get_val(item, item)
if self.partial and item not in self.effective_ground_set:
return 0

if self.mode == 'dense':
gain = self.total_similarity_with_master[self.original_to_partial_index_map[item] if self.partial else item] \
- 2 * self.lambda_ * self.total_similarity_with_subset[self.original_to_partial_index_map[item] if self.partial else item] \
- self.lambda_ * self.ground_ground_kernel[item][item]
elif self.mode == 'sparse':
gain = self.total_similarity_with_master[self.original_to_partial_index_map[item] if self.partial else item] \
- 2 * self.lambda_ * self.total_similarity_with_subset[self.original_to_partial_index_map[item] if self.partial else item] \
- self.lambda_ * self.sparse_kernel.get_val(item, item)
else:
raise ValueError("Error: Only dense and sparse mode supported")
# print("gain value",gain)
return gain


def update_memoization(self, X: Set[int], item: int):
effective_x = X.intersection(self.effective_ground_set) if self.partial else X

Expand Down