Skip to content

Commit

Permalink
chore: commit local changes from pre-commit hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
benwandrew committed Sep 20, 2023
1 parent 6cf8b03 commit bc8fcab
Show file tree
Hide file tree
Showing 10 changed files with 764 additions and 198 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ repos:
rev: "v1.5.1"
hooks:
- id: mypy
additional_dependencies: [types-requests,scipy,pytest]
additional_dependencies: [types-requests,types-tqdm,autora-core,scipy,pytest]
language_version: python3.8
args:
- "--namespace-packages"
Expand Down
93 changes: 29 additions & 64 deletions docs/Basic Usage.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@
{
"attachments": {},
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"# Basic Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": false
},
"metadata": {},
"outputs": [],
"source": [
"# Uncomment the following line when running on Google Colab\n",
Expand All @@ -24,20 +20,16 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"Let's generate a simple data set with two features $x_1, x_2 \\in [0, 1]$ and a target $y$. We will use the following generative model:\n",
"$y = 2 x_1 - e^{(5 x_2)}$"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
Expand All @@ -51,19 +43,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"Now let us define the search space, that is, the space of operations to consider when searching over the space of computation graphs."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"primitives = [\n",
Expand All @@ -79,9 +67,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"## Set Up The DARTS Regressor\n",
"\n",
Expand All @@ -101,10 +87,8 @@
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from autora.theorist.darts import DARTSRegressor\n",
Expand All @@ -123,19 +107,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"Now we have everything to run differentiable architecture search and visualize the model resulting from the highest architecture weights. Note that the current model corresponds to the model with the highest architecture weights."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -216,7 +196,7 @@
"<graphviz.graphs.Digraph at 0x288b726d0>"
]
},
"execution_count": 4,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -228,19 +208,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"We can refine the fit by running the `fit` method again, after changing the parameters. This allows us to keep the same architecture but refit the parameters in the final sampled model, for example:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -321,7 +297,7 @@
"<graphviz.graphs.Digraph at 0x174355df0>"
]
},
"execution_count": 5,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -337,19 +313,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"We can also change how the model is sampled from the architecture weight distribution:"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -430,7 +402,7 @@
"<graphviz.graphs.Digraph at 0x1746b7dc0>"
]
},
"execution_count": 6,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -447,19 +419,15 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"To recover the initial model, we need to return the sampling strategy to the default `\"max\"`:"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -540,7 +508,7 @@
"<graphviz.graphs.Digraph at 0x119bba670>"
]
},
"execution_count": 7,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -557,9 +525,7 @@
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"metadata": {},
"source": [
"As long as the architecture has not been refitted in the meantime, the architecture should be identical to the initial result, as the `sampling_strategy=\"max\"` is deterministic. The coefficients of the architecture functions may, however, be different, as they have different starting values compared to when they were initially set.\n"
]
Expand All @@ -580,8 +546,7 @@
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.6"
"pygments_lexer": "ipython2"
}
},
"nbformat": 4,
Expand Down
819 changes: 709 additions & 110 deletions docs/Weber Fechner Example.ipynb

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ dev = [
"autora-synthetic"
]

[tool.isort]
profile = "black"

[tool.mypy]
mypy_path="./src"

[[tool.mypy.overrides]]
module = ["matplotlib.*", "autora.*", "sklearn.*", "torch.*", "graphviz.*", "mkdocs_gen_files.*", "pandas.*", "numpy.*","scipy.*"]
ignore_missing_imports=true

[project.urls]
homepage = "http://www.empiricalresearch.ai"
repository = "https://github.com/AutoResearch/autora-theorist-darts"
Expand Down
2 changes: 1 addition & 1 deletion src/autora/theorist/darts/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from .model_search import DARTSType
from .operations import PRIMITIVES
from .regressor import DARTSRegressor, DARTSExecutionMonitor
from .regressor import DARTSExecutionMonitor, DARTSRegressor
3 changes: 1 addition & 2 deletions src/autora/theorist/darts/model_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,7 @@ def genotype(self, sample: bool = False) -> Genotype:
Returns:
genotype: genotype describing the current (sampled) architecture
"""

# this function uses the architecture weights to retrieve the
# operations with the highest weights
def _parse(weights):
Expand Down Expand Up @@ -613,9 +614,7 @@ def count_parameters(self, print_parameters: bool = False) -> Tuple[int, int, li

tmp_param_list = list()
if isiterable(op._ops[maxIdx[0].item(0)]): # Zero is not iterable

for subop in op._ops[maxIdx[0].item(0)]:

for parameter in subop.parameters():
tmp_param_list.append(parameter.data.numpy().squeeze())
if parameter.requires_grad is True:
Expand Down
3 changes: 0 additions & 3 deletions src/autora/theorist/darts/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,13 @@ def get_operation_label(
else:
classifier_str = input_var + " .* ("
for param_idx, param in enumerate(params):

if param_idx > 0:
if output_format == "latex":
classifier_str += " + \\left("
else:
classifier_str += " .+ ("

if isiterable(param.tolist()):

param_formatted = list()
for value in param.tolist():
param_formatted.append(format_string.format(value))
Expand Down Expand Up @@ -541,7 +539,6 @@ def forward(self, x: torch.Tensor) -> torch.Tensor:


def operation_factory(name):

if name == "none":
return Zero(1)
elif name == "add":
Expand Down
16 changes: 10 additions & 6 deletions src/autora/theorist/darts/regressor.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@

from autora.variable import ValueType

from .operations import PRIMITIVES
from .architect import Architect
from .dataset import darts_dataset_from_ndarray
from .model_search import Network, DARTSType
from .utils import get_loss_function, get_output_format, AvgrageMeter, \
format_input_target, get_output_str
from .model_search import DARTSType, Network
from .operations import PRIMITIVES
from .utils import (
AvgrageMeter,
format_input_target,
get_loss_function,
get_output_format,
get_output_str,
)
from .visualize import darts_model_plot

_logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -161,7 +166,6 @@ def _general_darts(
network.train()

for epoch in _progress_indicator(range(max_epochs)):

_logger.debug(f"Running fit, epoch {epoch}")

data_iterator = _get_data_iterator(data_loader)
Expand Down Expand Up @@ -842,7 +846,7 @@ def display(self):

arch_fig.suptitle("Architecture Weights", fontsize=10)

for (edge_i, ax) in zip(range(num_edges), arch_axes.flat):
for edge_i, ax in zip(range(num_edges), arch_axes.flat):
for primitive_i in range(num_primitives):
print(f"{edge_i}, {primitive_i}, {ax}")
ax.plot(
Expand Down
6 changes: 2 additions & 4 deletions src/autora/theorist/darts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ def compute_BIC(
elif output_type == ValueType.PROBABILITY_SAMPLE:
llik = 0
for idx in range(len(target)):

# fail safe if model doesn't produce probabilities
if prediction[idx] > 1:
prediction[idx] = 1
Expand Down Expand Up @@ -166,7 +165,7 @@ def compute_BIC(


def compute_BIC_AIC(
soft_targets: np.array, soft_prediction: np.array, model: Network
soft_targets: np.ndarray, soft_prediction: np.ndarray, model: Network
) -> Tuple:
"""
Returns the Bayesian information criterion (BIC) as well as the
Expand Down Expand Up @@ -269,7 +268,7 @@ def count_parameters_in_MB(model: Network) -> int:
model: model to count the parameters for
"""
return (
np.sum(
np.sum( # type: ignore
np.prod(v.size())
for name, v in model.named_parameters()
if "auxiliary" not in name
Expand Down Expand Up @@ -363,7 +362,6 @@ def read_log_files(results_path: str, winning_architecture_only: bool = False) -

print("Reading log files... ")
for file in filelist:

with open(file) as csvfile:
readCSV = csv.reader(csvfile, delimiter=",")
for row in readCSV:
Expand Down
Loading

0 comments on commit bc8fcab

Please sign in to comment.