Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Zebin Yang committed Apr 9, 2022
1 parent edf0c7c commit d379b0f
Show file tree
Hide file tree
Showing 7 changed files with 221 additions and 322 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
examples/.ipynb_checkpoints/*
examples/__pycache__/*
scripts/*
.ipynb_checkpoints/*
gaminet/__pycache__/*
Expand Down
289 changes: 100 additions & 189 deletions examples/GAMINet-bike-share.ipynb

Large diffs are not rendered by default.

Binary file removed examples/__pycache__/dataset.cpython-38.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion examples/results/demo.eps

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

64 changes: 22 additions & 42 deletions gaminet/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,10 @@ def _validate_input(self, x, y):

def build_teacher_main_effect(self):

indices = np.arange(self.n_samples)
allx = torch.vstack([self.tr_x, self.val_x])
ally = torch.vstack([self.tr_y, self.val_y])
allsw = torch.hstack([self.tr_sw, self.val_sw])
_, suffleidx = train_test_split(indices, test_size=self.gam_sample_size, random_state=self.random_state)
subx = allx[suffleidx]
subx = ((subx - self.mu_list) / self.std_list if self.normalize else subx).detach().cpu().numpy()
suby = ally[suffleidx].detach().cpu().numpy()
subsw = allsw[suffleidx].detach().cpu().numpy()
x = self.training_generator.tensors[0].cpu().numpy()
y = self.training_generator.tensors[1].cpu().numpy()
sw = self.training_generator.tensors[2].cpu().numpy()
tr_x, _, tr_y, _, tr_sw, _ = train_test_split(x, y, sw, test_size=self.gam_sample_size, random_state=self.random_state)

termlist = TermList()
n_splines = max(11 - np.ceil(self.n_features / 100).astype(int), 2)
Expand All @@ -80,7 +75,7 @@ def build_teacher_main_effect(self):
continue

gam = LinearGAM(termlist)
gam.fit(subx, suby, weights=subsw)
gam.fit((tr_x - self.mu_list.cpu().numpy()) / self.std_list.cpu().numpy(), tr_y, weights=tr_sw)

def margial_effect(i):
return lambda x: gam.partial_dependence(i, x)
Expand All @@ -91,16 +86,11 @@ def margial_effect(i):

def build_teacher_interaction(self):

indices = np.arange(self.n_samples)
allx = torch.vstack([self.tr_x, self.val_x])
ally = torch.vstack([self.tr_y, self.val_y])
allsw = torch.hstack([self.tr_sw, self.val_sw])
_, suffleidx = train_test_split(indices, test_size=self.gam_sample_size, random_state=self.random_state)
subx = allx[suffleidx]
subx = ((subx - self.mu_list) / self.std_list if self.normalize else subx).detach().cpu().numpy()
suby = ally[suffleidx].detach().cpu().numpy()
subsw = allsw[suffleidx].detach().cpu().numpy()
residual = suby - self.decision_function(subx, main_effect=True, interaction=False).detach().cpu().numpy()
x = self.training_generator.tensors[0].cpu().numpy()
y = self.training_generator.tensors[1].cpu().numpy()
sw = self.training_generator.tensors[2].cpu().numpy()
tr_x, _, tr_y, _, tr_sw, _ = train_test_split(x, y, sw, test_size=self.gam_sample_size, random_state=self.random_state)
tr_residual = tr_y - self.predict(tr_x, main_effect=True, interaction=False)

termlist = TermList()
n_splines = max(11 - np.ceil(self.n_interactions / 10).astype(int), 2)
Expand All @@ -109,7 +99,7 @@ def build_teacher_interaction(self):
s(idx2, n_splines=n_splines, spline_order=1, lam=0.6))

gam = LinearGAM(termlist)
gam.fit(subx, residual, weights=subsw)
gam.fit((tr_x - self.mu_list.cpu().numpy()) / self.std_list.cpu().numpy(), tr_residual, weights=tr_sw)

def margial_effect(i):
return lambda x: gam.partial_dependence(i, x)
Expand Down Expand Up @@ -207,15 +197,10 @@ def _validate_input(self, x, y):

def build_teacher_main_effect(self):

indices = np.arange(self.n_samples)
allx = torch.vstack([self.tr_x, self.val_x])
ally = torch.vstack([self.tr_y, self.val_y]) * 4 - 2
allsw = torch.hstack([self.tr_sw, self.val_sw])
_, suffleidx = train_test_split(indices, test_size=self.gam_sample_size, stratify=ally, random_state=self.random_state)
subx = allx[suffleidx]
subx = ((subx - self.mu_list) / self.std_list if self.normalize else subx).detach().cpu().numpy()
suby = ally[suffleidx].detach().cpu().numpy()
subsw = allsw[suffleidx].detach().cpu().numpy()
x = self.training_generator.tensors[0].cpu().numpy()
y = self.training_generator.tensors[1].cpu().numpy() * 4 - 2
sw = self.training_generator.tensors[2].cpu().numpy()
tr_x, _, tr_y, _, tr_sw, _ = train_test_split(x, y, sw, test_size=self.gam_sample_size, random_state=self.random_state)

termlist = TermList()
n_splines = max(11 - np.ceil(self.n_features / 100).astype(int), 2)
Expand All @@ -226,7 +211,7 @@ def build_teacher_main_effect(self):
continue

gam = LinearGAM(termlist)
gam.fit(subx, suby, weights=subsw)
gam.fit((tr_x - self.mu_list.cpu().numpy()) / self.std_list.cpu().numpy(), tr_y, weights=tr_sw)

def margial_effect(i):
return lambda x: gam.partial_dependence(i, x)
Expand All @@ -237,16 +222,11 @@ def margial_effect(i):

def build_teacher_interaction(self):

indices = np.arange(self.n_samples)
allx = torch.vstack([self.tr_x, self.val_x])
ally = torch.vstack([self.tr_y, self.val_y])
allsw = torch.hstack([self.tr_sw, self.val_sw])
_, suffleidx = train_test_split(indices, test_size=self.gam_sample_size, stratify=ally, random_state=self.random_state)
subx = allx[suffleidx]
subx = ((subx - self.mu_list) / self.std_list if self.normalize else subx).detach().cpu().numpy()
suby = ally[suffleidx].detach().cpu().numpy()
subsw = allsw[suffleidx].detach().cpu().numpy()
residual = suby - self.predict_proba(subx, main_effect=True, interaction=False)[:, [1]]
x = self.training_generator.tensors[0].cpu().numpy()
y = self.training_generator.tensors[1].cpu().numpy()
sw = self.training_generator.tensors[2].cpu().numpy()
tr_x, _, tr_y, _, tr_sw, _ = train_test_split(x, y, sw, test_size=self.gam_sample_size, random_state=self.random_state)
tr_residual = tr_y - self.predict_proba(tr_x, main_effect=True, interaction=False)[:, [1]]

termlist = TermList()
n_splines = max(11 - np.ceil(self.n_interactions / 10).astype(int), 2)
Expand All @@ -255,7 +235,7 @@ def build_teacher_interaction(self):
s(idx2, n_splines=n_splines, spline_order=1, lam=0.6))

gam = LinearGAM(termlist)
gam.fit(subx, residual, weights=subsw)
gam.fit((tr_x - self.mu_list.cpu().numpy()) / self.std_list.cpu().numpy(), tr_residual, weights=tr_sw)

def margial_effect(i):
return lambda x: gam.partial_dependence(i, x)
Expand Down
Loading

0 comments on commit d379b0f

Please sign in to comment.