Skip to content

Commit

Permalink
Merge pull request #97 from mwong009/1.10.0-beta
Browse files Browse the repository at this point in the history
1.10.0 beta
  • Loading branch information
mwong009 committed Feb 15, 2024
2 parents 2a353de + 88b02f6 commit 8f90f10
Show file tree
Hide file tree
Showing 22 changed files with 1,182 additions and 543 deletions.
1 change: 1 addition & 0 deletions .github/workflows/linting.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
- uses: psf/black@stable
with:
src: "."
version: "23.11.0"

isort:
runs-on: ubuntu-latest
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
- name: Xcode check (macos)
if: matrix.os == 'macos-latest'
run: |
clang -- version
clang --version
xcrun --show-sdk-path
- name: Test pycmtensor import
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
[![Licence](https://img.shields.io/badge/Licence-MIT-blue)](about/licence.md)
![](https://img.shields.io/pypi/pyversions/pycmtensor)
[![PyPI version](https://badge.fury.io/py/pycmtensor.svg)](https://badge.fury.io/py/pycmtensor)
![Conda-Forge|downloads](https://img.shields.io/conda/d/conda-forge/pycmtensor)

[![codecov](https://codecov.io/gh/mwong009/pycmtensor/branch/master/graph/badge.svg?token=LFwgggDyjS)](https://codecov.io/gh/mwong009/pycmtensor)
[![Downloads](https://static.pepy.tech/personalized-badge/pycmtensor?period=month&units=international_system&left_color=grey&right_color=orange&left_text=downloads/month)](https://pepy.tech/project/pycmtensor)
[![DOI](https://zenodo.org/badge/460802394.svg)](https://zenodo.org/badge/latestdoi/460802394)
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/mixed_mnl.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
"\n",
"\n",
"# estimate the model\n",
"from pycmtensor.models import train\n",
"from pycmtensor import train\n",
"train(mymodel, ds)"
]
},
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/mnl.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"\n",
"\n",
"# estimate the model\n",
"from pycmtensor.models import train\n",
"from pycmtensor import train\n",
"train(mymodel, ds)"
]
},
Expand Down
4 changes: 2 additions & 2 deletions docs/getting_started/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Output:
## Estimating the model

```python
from pycmtensor.models import train
from pycmtensor import train
from pycmtensor.optimizers import Adam
from pycmtensor.scheduler import ConstantLR

Expand Down Expand Up @@ -348,7 +348,7 @@ U = [U_walk, U_cycle, U_pt, U_drive]
mymodel = pycmtensor.models.MNL(ds=ds, params=locals(), utility=U, av=None)


from pycmtensor.models import train
from pycmtensor import train
from pycmtensor.optimizers import Adam
from pycmtensor.scheduler import ConstantLR

Expand Down
381 changes: 245 additions & 136 deletions poetry.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pycmtensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# shortcuts for importing commonly used classes
from pycmtensor.dataset import Dataset
from pycmtensor.regularizers import Regularizers
from pycmtensor.run import compute, train

config = defaultconfig.config

Expand Down
77 changes: 68 additions & 9 deletions pycmtensor/expressions.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
"""
The code snippet is a part of the PyCMTensor expressions module. It defines a base class for parsing and manipulating Aesara tensor expressions. The class provides methods for parsing a tensor expression to remove parentheses and tensor operators, and returns a clean list of keywords found in the expression. It also defines a base class for expression objects, which includes overloaded operators for tensor operations such as addition, subtraction, multiplication, division, and comparison.
The code snippet is a part of the PyCMTensor expressions module. It defines a base
class for parsing and manipulating Aesara tensor expressions. The class provides
methods for parsing a tensor expression to remove parentheses and tensor operators, and
returns a clean list of keywords found in the expression. It also defines a base class
for expression objects, which includes overloaded operators for tensor operations such
as addition, subtraction, multiplication, division, and comparison.
"""
from typing import Union

Expand Down Expand Up @@ -51,7 +56,8 @@ def __init__(self, expression=None):
"""Base class for parsing and manipulating Aesara tensor expressions.
Args:
expression (TensorVariable, optional): The tensor expression to parse. Defaults to None.
expression (TensorVariable, optional): The tensor expression to parse.
Defaults to None.
"""
if expression is not None:
self.expression = str(pprint(expression))
Expand Down Expand Up @@ -153,7 +159,7 @@ def __mul__(self, other):
return self() * other()
else:
raise TypeError(
f"__mul__ {other} must be a TensorVariable or TensorShared Variable object"
f"__mul__ {other} must be a TensorVariable or TensorShared Variable"
)

def __rmul__(self, other):
Expand Down Expand Up @@ -399,8 +405,10 @@ def __init__(self, name: str, draw_type: str, n_draws: int):
Args:
name (str): The name of the RandomDraw object.
draw_type (str): The distribution of the draw. Can be "normal", "lognormal", "gumbel", "exponential", "gamma", or "poisson".
n_draws (int): The number of draws, which determines the size of the shared tensor.
draw_type (str): The distribution of the draw. Can be "normal",
"lognormal", "gumbel", "exponential", "gamma", or "poisson".
n_draws (int): The number of draws, which determines the size of the shared
tensor.
Raises:
NotImplementedError: If an unsupported draw_type is provided.
Expand Down Expand Up @@ -447,7 +455,8 @@ def __init__(self, name, size, value=None):
Args:
name (str): The name of the parameter.
size (Union[tuple,list]): The size of the array in 1 dimension.
value (numpy.ndarray): The initial values of the parameter. If `None` is given, it defaults to `0`.
value (numpy.ndarray): The initial values of the parameter. If `None` is
given, it defaults to `0`.
"""
Param.__init__(self, name, lb=None, ub=None)

Expand Down Expand Up @@ -496,7 +505,8 @@ def __init__(self, name, size, value=None, init_type=None):
Args:
name (str): name of the parameter
size (Union[tuple,list]): size of the array
value (numpy.ndarray): initial values of the parameter. Defaults to `random.uniform(-0.1, 0.1, size)`
value (numpy.ndarray): initial values of the parameter. Defaults to `random.
uniform(-0.1, 0.1, size)`
init_type (str): initialization type, see notes
Note:
Expand All @@ -510,8 +520,14 @@ def __init__(self, name, size, value=None, init_type=None):
* `"glorot"`: initialization method that maintains the variance for
symmetric activation functions, e.g. sigm, tanh [^2]
[^1] He, K., Zhang, X., Ren, S. and Sun, J., 2015. Delving deep into rectifiers: Surpassing human-level performance on imagenet classification. In Proceedings of the IEEE international conference on computer vision (pp. 1026-1034).
[^2] Glorot, X. and Bengio, Y., 2010, March. Understanding the difficulty of training deep feedforward neural networks. In Proceedings of the thirteenth international conference on artificial intelligence and statistics (pp. 249-256). JMLR Workshop and Conference Proceedings.
[^1] He, K., Zhang, X., Ren, S. and Sun, J., 2015. Delving deep into
rectifiers: Surpassing human-level performance on imagenet classification.
In Proceedings of the IEEE international conference on computer vision (pp.
1026-1034).
[^2] Glorot, X. and Bengio, Y., 2010, March. Understanding the difficulty
of training deep feedforward neural networks. In Proceedings of the
thirteenth international conference on artificial intelligence and
statistics (pp. 249-256). JMLR Workshop and Conference Proceedings.
!!! example
Specifying a weight array:
Expand Down Expand Up @@ -566,3 +582,46 @@ def T(self):

def __repr__(self):
return f"Weight({self.name}, {self.shape})"


class Gamma(Param):
def __init__(self, name, size):
"""Class object for Gamma parameter
Args:
name (str): The name of the parameter.
size (Union[tuple,list]): The size of the parameter.
"""
Param.__init__(self, name)
value = np.ones(size, dtype=FLOATX)

self._init_value = value
self._param_type = "Gamma"
self.shared_var = aesara.shared(self.init_value, name=name, borrow=True)

@property
def T(self):
return self.shared_var.T

@property
def init_type(self):
return self._param_type

def __repr__(self):
return f"Gamma({self.name}, {self.shape})"

def __rmul__(self, other):
if isinstance(other, (TensorVariable, TensorSharedVariable)):
g = aet.atleast_Nd(self(), n=other.ndim, left=False)
return g * other

else:
return super().__rmul__(other)

def __mul__(self, other):
if isinstance(other, (TensorVariable, TensorSharedVariable)):
g = aet.atleast_Nd(self(), n=other.ndim, left=False)
return g * other

else:
return super().__mul__(other)
3 changes: 2 additions & 1 deletion pycmtensor/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
```
## Inputs
- `level` (int): The level of the logger to be set. It can be one of the predefined levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL`.
- `level` (int): The level of the logger to be set. It can be one of the predefined
levels: `DEBUG`, `INFO`, `WARNING`, `ERROR`, or `CRITICAL`.
## Outputs
- None
Expand Down
8 changes: 1 addition & 7 deletions pycmtensor/models/MNL.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

class MNL(BaseModel):
def __init__(self, ds, variables, utility, av=None, **kwargs):
"""Defines a Multinomial Logit model
"""Initialize the Multinomial Logit model with the given dataset, variables, utility function, and availability.
Args:
ds (pycmtensor.Data): the database object
Expand Down Expand Up @@ -136,12 +136,6 @@ def reset_values(self) -> None:
def build_cost_fn(self):
"""Constructs Aesara functions for calculating the cost and prediction errors of the Multinomial Logit model.
Inputs:
- None
Outputs:
- None
Example Usage:
```python
# Create an instance of the MNL class
Expand Down
Loading

0 comments on commit 8f90f10

Please sign in to comment.