Skip to content

Commit

Permalink
formatting (#65)
Browse files Browse the repository at this point in the history
* tiling kwargs

* Better tiling check.

* add another warning

* first draft

* Remove the other PR's contents from this one.

* format

* Making model args optional for all cuts.

* Testing of changes ongoing.

* work

* remove debug statements

* format, upgrade torch, disable CUDA for local tests

* note about tolerance with cuda

* revert arg names when not needed

* ignore notebook test generated files

* make changes more seamless with existing tests

* format

* starting work on nlp demo notebook

* starting

* working on tests

* Makefile to help run tests locally.

* add a link to requirements from tests folder which didn't have one

* finished tiling for fprop tests and implementation for pytorch

* finished tf1 wrapper updates to fprop

* progress towards tf_keras fprop updates

* working on nlp demo notebook

* Writing up the nlp torch demo content.

* Finished nlp models notebook.

* Fixed what I broke.

* Removing notebook-related changes from this PR.

* revert formatting things for now

* revert more things

* one more

* and one more

* and again one more

* one more

* Addressing PR comments.

* Addressing PR comments.

* use older pytest

* add isort and run format

* format after pulling in master

Co-authored-by: piotrm <[email protected]>
  • Loading branch information
piotrm0 and piotrm authored Mar 14, 2022
1 parent 7c9c95f commit b28c438
Show file tree
Hide file tree
Showing 77 changed files with 1,779 additions and 939 deletions.
2 changes: 2 additions & 0 deletions .isort.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[settings]
profile=google
4 changes: 4 additions & 0 deletions .style.yapf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[style]
based_on_style = google
DEDENT_CLOSING_BRACKETS=true
SPLIT_BEFORE_FIRST_ARGUMENT=true
16 changes: 8 additions & 8 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,42 +12,44 @@
#
import os
import sys

os.environ['TRULENS_BACKEND'] = 'keras'
sys.path.insert(0, os.path.abspath('.'))
sys.path.insert(0, os.path.abspath('../'))


# -- Project information -----------------------------------------------------

project = 'trulens'
copyright = '2020, Klas Leino'
author = 'Klas Leino'


# -- General configuration ---------------------------------------------------

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'recommonmark',
'sphinx.ext.mathjax',
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'recommonmark',
'sphinx.ext.mathjax',
]

# napoleon_google_docstring = False
# napoleon_use_param = False
# napoleon_use_ivar = True


def skip(app, what, name, obj, would_skip, options):
if name == '__init__' or name == '__call__':
return False
return would_skip


def setup(app):
app.connect('autodoc-skip-member', skip)


# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']

Expand All @@ -56,7 +58,6 @@ def setup(app):
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']


# -- Options for HTML output -------------------------------------------------

# The theme to use for HTML and HTML Help pages. See the documentation for
Expand All @@ -69,7 +70,6 @@ def setup(app):
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']


from recommonmark.parser import CommonMarkParser

source_parsers = {'.md': CommonMarkParser}
Expand Down
5 changes: 3 additions & 2 deletions format.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#/bin/bash

yapf --style='{based_on_style: google, split_before_first_argument: true}' -r -i trulens/
yapf --style='{based_on_style: google, split_before_first_argument: true}' -r -i tests/
isort .

yapf --style .style.yapf -r -i --verbose --parallel -r -i .
7 changes: 5 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import datetime
from os import path
import sys
from setuptools import setup, find_namespace_packages

from setuptools import find_namespace_packages
from setuptools import setup

version = "0.0.3"
versionArgument = "--customVersion"
Expand Down Expand Up @@ -36,4 +38,5 @@
"Development Status :: 3 - Alpha",
"License :: OSI Approved :: GNU Affero General Public License v3",
],
python_requires='>=3.6')
python_requires='>=3.6'
)
18 changes: 13 additions & 5 deletions tests/keras/unit/attribution_axioms_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import os

os.environ['TRULENS_BACKEND'] = 'keras'

from tensorflow.python.util import deprecation

deprecation._PRINT_DEPRECATION_WARNINGS = False

from keras.layers import Activation, Dense, Input
from unittest import main
from unittest import TestCase

from keras.layers import Activation
from keras.layers import Dense
from keras.layers import Input
from keras.models import Model
from unittest import TestCase, main

from trulens.nn.models import get_model_wrapper
from tests.unit.attribution_axioms_test_base import AxiomsTestBase
from trulens.nn.models import get_model_wrapper


class AxiomsTest(AxiomsTestBase, TestCase):
Expand All @@ -24,7 +30,8 @@ def setUp(self):
self.model_lin = get_model_wrapper(Model(x_lin, y_lin))

self.model_lin._model.set_weights(
[self.model_lin_weights, self.model_lin_bias])
[self.model_lin_weights, self.model_lin_bias]
)

# Make a deeper model for testing.
x_deep = Input((self.input_size,))
Expand All @@ -41,7 +48,8 @@ def setUp(self):
self.model_deep_weights_1, self.model_deep_bias_1,
self.model_deep_weights_2, self.model_deep_bias_2,
self.model_deep_weights_3, self.model_deep_bias_3
])
]
)

self.layer2 = 2
self.layer3 = 3
Expand Down
17 changes: 12 additions & 5 deletions tests/keras/unit/batch_tests.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
import os

os.environ['TRULENS_BACKEND'] = 'keras'

from unittest import TestCase, main
from keras.layers import Activation, Dense, Input
from unittest import main
from unittest import TestCase

from keras.layers import Activation
from keras.layers import Dense
from keras.layers import Input
from keras.models import Model

from trulens.nn.models import get_model_wrapper
from tests.unit.batch_test_base import BatchTestBase
from trulens.nn.models import get_model_wrapper


class BatchTest(BatchTestBase, TestCase):
Expand All @@ -21,7 +26,8 @@ def setUp(self):
self.model_lin = get_model_wrapper(Model(x_lin, y_lin))

self.model_lin._model.set_weights(
[self.model_lin_weights, self.model_lin_bias])
[self.model_lin_weights, self.model_lin_bias]
)

# Make a deeper model for testing.
x_deep = Input((self.input_size,))
Expand All @@ -38,7 +44,8 @@ def setUp(self):
self.model_deep_weights_1, self.model_deep_bias_1,
self.model_deep_weights_2, self.model_deep_bias_2,
self.model_deep_weights_3, self.model_deep_bias_3
])
]
)


if __name__ == '__main__':
Expand Down
15 changes: 10 additions & 5 deletions tests/keras/unit/doi_test.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import os

os.environ['TRULENS_BACKEND'] = 'keras'

from unittest import TestCase, main
from unittest import main
from unittest import TestCase

from tensorflow.python.util import deprecation

deprecation._PRINT_DEPRECATION_WARNINGS = False
from trulens.nn.models import get_model_wrapper
from keras.layers import Input, Lambda
from keras.layers import Input
from keras.layers import Lambda
from keras.models import Model

from tests.unit.doi_test_base import DoiTestBase
from trulens.nn.models import get_model_wrapper


class DoiTest(DoiTestBase, TestCase):

def setUp(self):
super(DoiTest, self).setUp()

l0 = Input((1,))
l1 = Lambda(lambda input: self.l1_coeff * (input ** self.l1_exp))(l0)
l2 = Lambda(lambda input: self.l2_coeff * (input ** self.l2_exp))(l1)
l1 = Lambda(lambda input: self.l1_coeff * (input**self.l1_exp))(l0)
l2 = Lambda(lambda input: self.l2_coeff * (input**self.l2_exp))(l1)

self.model = get_model_wrapper(Model(l0, l2))

Expand Down
10 changes: 6 additions & 4 deletions tests/keras/unit/environment_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from unittest import TestCase, main
from keras.layers import Dense, Input
from unittest import main
from unittest import TestCase

from keras.layers import Dense
from keras.layers import Input
from keras.models import Model

from tests.unit.environment_test_base import EnvironmentTestBase

from trulens.nn.models.keras import KerasModelWrapper
from trulens.nn.backend import Backend
from trulens.nn.models.keras import KerasModelWrapper


class EnvironmentTest(EnvironmentTestBase, TestCase):
Expand Down
71 changes: 54 additions & 17 deletions tests/keras/unit/ffn_edge_case_architectures_test.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import os

os.environ['TRULENS_BACKEND'] = 'keras'

from unittest import TestCase, main
from unittest import main
from unittest import TestCase

import numpy as np

from tensorflow.python.util import deprecation

deprecation._PRINT_DEPRECATION_WARNINGS = False

from trulens.nn.backend import get_backend
from keras.layers import Activation
from keras.layers import Concatenate
from keras.layers import Dense
from keras.layers import Input
from keras.models import Model

from trulens.nn.attribution import InternalInfluence
from trulens.nn.backend import get_backend
from trulens.nn.distributions import PointDoi
from trulens.nn.models import get_model_wrapper
from trulens.nn.quantities import ClassQoI
from trulens.nn.slices import InputCut, Cut

from keras.layers import Activation, Dense, Input, Concatenate
from keras.models import Model
from trulens.nn.slices import Cut
from trulens.nn.slices import InputCut


class FfnEdgeCaseArchitecturesTest(TestCase):
Expand All @@ -35,7 +41,29 @@ def test_multiple_inputs_as_one_arg(self):

res = infl.attributions(
[np.array([[1., 2., 3., 4., 5.]]),
np.array([[1.]])])
np.array([[1.]])]
)

self.assertEqual(len(res), 2)
self.assertEqual(res[0].shape, (1, 5))
self.assertEqual(res[1].shape, (1, 1))

def test_multiple_inputs_as_multiple_args(self):
x1 = Input((5,))
z1 = Dense(6)(x1)
x2 = Input((1,))
z2 = Concatenate()([z1, x2])
z3 = Dense(7)(z2)
y = Dense(3)(z3)

model = get_model_wrapper(Model([x1, x2], y))

infl = InternalInfluence(model, InputCut(), ClassQoI(1), PointDoi())

res = infl.attributions(
np.array([[1., 2., 3., 4., 5.]]), np.array([[1.]])
)
# note above we are sending two args instead of one as in the prior test

self.assertEqual(len(res), 2)
self.assertEqual(res[0].shape, (1, 5))
Expand All @@ -54,7 +82,8 @@ def test_multiple_inputs_as_multiple_args(self):
infl = InternalInfluence(model, InputCut(), ClassQoI(1), PointDoi())

res = infl.attributions(
np.array([[1., 2., 3., 4., 5.]]), np.array([[1.]]))
np.array([[1., 2., 3., 4., 5.]]), np.array([[1.]])
)
# note above we are sending two args instead of one as in the prior test

self.assertEqual(len(res), 2)
Expand Down Expand Up @@ -84,11 +113,13 @@ def test_internal_multiple_inputs(self):
model = get_model_wrapper(Model([x1, x2], y))

infl = InternalInfluence(
model, Cut('concat', anchor='in'), ClassQoI(1), PointDoi())
model, Cut('concat', anchor='in'), ClassQoI(1), PointDoi()
)

res = infl.attributions(
[np.array([[1., 2., 3., 4., 5.]]),
np.array([[1.]])])
np.array([[1.]])]
)

self.assertEqual(len(res), 2)
self.assertEqual(res[0].shape, (1, 6))
Expand All @@ -107,11 +138,13 @@ def test_internal_slice_multiple_layers(self):
model = get_model_wrapper(Model([x1, x2], y))

infl = InternalInfluence(
model, Cut(['cut_layer1', 'cut_layer2']), ClassQoI(1), PointDoi())
model, Cut(['cut_layer1', 'cut_layer2']), ClassQoI(1), PointDoi()
)

res = infl.attributions(
[np.array([[1., 2., 3., 4., 5.]]),
np.array([[1.]])])
np.array([[1.]])]
)

self.assertEqual(len(res), 2)
self.assertEqual(res[0].shape, (1, 6))
Expand All @@ -130,7 +163,8 @@ def test_anchors(self):
np.array([0., 0.]),
np.array([[1.], [1.]]),
np.array([0.])
])
]
)

model = get_model_wrapper(k_model)

Expand All @@ -139,14 +173,16 @@ def test_anchors(self):
Cut(2, anchor='out'),
ClassQoI(0),
PointDoi(),
multiply_activation=False)
multiply_activation=False
)

infl_in = InternalInfluence(
model,
Cut(2, anchor='in'),
ClassQoI(0),
PointDoi(),
multiply_activation=False)
multiply_activation=False
)

res_out = infl_out.attributions(np.array([[1., 1.]]))
res_in = infl_in.attributions(np.array([[1., 1.]]))
Expand Down Expand Up @@ -179,7 +215,8 @@ def test_catch_cut_name_error(self):

with self.assertRaises(ValueError):
infl = InternalInfluence(
model, Cut('not_a_real_layer'), ClassQoI(0), PointDoi())
model, Cut('not_a_real_layer'), ClassQoI(0), PointDoi()
)

infl.attributions(np.array([[1., 1.]]))

Expand Down
Loading

0 comments on commit b28c438

Please sign in to comment.