Skip to content

Commit

Permalink
style: removed unused imports and improved code style consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
mwong009 committed Mar 7, 2024
1 parent a256506 commit 9d9ab9f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 41 deletions.
15 changes: 3 additions & 12 deletions pycmtensor/models/MNL.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
from time import perf_counter

import aesara
import aesara.tensor as aet

from pycmtensor.expressions import Beta
from pycmtensor.functions import (
errors,
first_order_derivative,
log_likelihood,
logit,
second_order_derivative,
)
from pycmtensor.functions import log_likelihood, logit
from pycmtensor.logger import info
from pycmtensor.models.basic import BaseModel
from pycmtensor.utils import time_format
Expand Down Expand Up @@ -51,6 +44,7 @@ def __init__(self, ds, variables, utility, av=None, **kwargs):
self.ll = log_likelihood(self.p_y_given_x, self.y, self.index)
self.cost = -self.ll
self.pred = aet.argmax(self.p_y_given_x, axis=0)

self.params = self.extract_params(self.cost, variables)
self.betas = [p for p in self.params if isinstance(p, Beta)]

Expand All @@ -60,14 +54,11 @@ def __init__(self, ds, variables, utility, av=None, **kwargs):

self.x = ds.x
self.xy = self.x + [self.y]
info(f"choice: {self.y}")
info(f"inputs in {self.name}: {self.x}")

start_time = perf_counter()
self.build_cost_fn()
build_time = round(perf_counter() - start_time, 3)

self.results.build_time = time_format(build_time)
info(f"inputs in {self.name}: {self.x}")
info(f"Build time = {self.results.build_time}")

@property
Expand Down
22 changes: 4 additions & 18 deletions pycmtensor/models/ResLogit.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from collections import OrderedDict
from time import perf_counter

import aesara
import aesara.tensor as aet
import numpy as np
from aesara import pprint

import pycmtensor.models.layers as layers
from pycmtensor.expressions import Beta, Bias, Weight
from pycmtensor.functions import (
errors,
first_order_derivative,
log_likelihood,
logit,
Expand All @@ -23,43 +19,33 @@
class ResLogit(BaseModel):
def __init__(self, ds, variables, utility, av=None, **kwargs):
BaseModel.__init__(self, ds, variables, utility, av, **kwargs)
self.name = "ResLogit"
self.params = []
self.weights = []
self.biases = []
self.betas = []
self.updates = []
self.index = aet.ivector("index")
self.learning_rate = aet.scalar("learning_rate")
start_time = perf_counter()

self.name = "ResLogit"
self.y = ds.y
self.p_y_given_x = logit(utility, av)
self.ll = log_likelihood(self.p_y_given_x, self.y, self.index)
self.cost = -self.ll
self.pred = aet.argmax(self.p_y_given_x, axis=0)

start_time = perf_counter()

self.layer_params = self.extract_layer_params(variables)
self.params = self.extract_params(self.cost, variables)
self.params += self.layer_params
self.betas = [p for p in self.params if isinstance(p, Beta)]
self.weights = [p for p in self.params if isinstance(p, Weight)]
self.biases = [p for p in self.params if isinstance(p, Bias)]

# drop unused variables from dataset
drop_unused = self.drop_unused_variables(self.cost, self.params, ds())
ds.drop(drop_unused)

self.x = ds.x
self.xy = self.x + [self.y]
info(f"choice: {self.y}")
info(f"inputs in {self.name}: {self.x}")

self.build_cost_fn()

build_time = round(perf_counter() - start_time, 3)

self.results.build_time = time_format(build_time)
info(f"inputs in {self.name}: {self.x}")
info(f"Build time = {self.results.build_time}")

@property
Expand Down
14 changes: 3 additions & 11 deletions pycmtensor/models/TasteNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@

import pycmtensor.models.layers as layers
from pycmtensor.expressions import Beta, Bias, Weight
from pycmtensor.functions import (
errors,
first_order_derivative,
log_likelihood,
logit,
second_order_derivative,
)
from pycmtensor.functions import log_likelihood, logit
from pycmtensor.logger import info
from pycmtensor.models.basic import BaseModel
from pycmtensor.utils import time_format
Expand Down Expand Up @@ -73,19 +67,17 @@ def __init__(self, ds, variables, utility, av=None, **kwargs):
self.weights = [p for p in self.params if isinstance(p, Weight)]
self.biases = [p for p in self.params if isinstance(p, Bias)]

# drop unused variables from dataset
drop_unused = self.drop_unused_variables(self.cost, self.params, ds())
ds.drop(drop_unused)

self.x = ds.x
self.xy = self.x + [self.y]
info(f"choice: {self.y}")
info(f"inputs in {self.name}: {self.x}")

self.build_cost_fn()

build_time = round(perf_counter() - start_time, 3)

self.results.build_time = time_format(build_time)
info(f"inputs in {self.name}: {self.x}")
info(f"Build time = {self.results.build_time}")

@property
Expand Down

0 comments on commit 9d9ab9f

Please sign in to comment.