-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from ASEM000/batchnorm,-tree_evaluation
edits and state/eval addition
- Loading branch information
Showing
19 changed files
with
643 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Copyright 2023 Serket authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Define dispatchers for custom tree evaluation.""" | ||
|
||
from __future__ import annotations | ||
|
||
import functools as ft | ||
from typing import Any, Callable, TypeVar | ||
|
||
import jax | ||
|
||
T = TypeVar("T") | ||
|
||
|
||
def tree_evaluation(tree: T) -> T: | ||
"""Modify tree layers to disable any trainning related behavior. | ||
For example, `Dropout` layers drop probability is set to 0.0. and `BatchNorm` | ||
layer `track_running_stats` is set to False when evaluating the tree. | ||
Args: | ||
tree: A tree of layers. | ||
Returns: | ||
A tree of layers with evaluation behavior. | ||
Example: | ||
>>> # dropout is replaced by an identity layer in evaluation mode | ||
>>> # by registering `tree_evaluation.def_evaluation(sk.nn.Dropout, sk.nn.Identity)` | ||
>>> import jax.numpy as jnp | ||
>>> import serket as sk | ||
>>> layer = sk.nn.Dropout(0.5) | ||
>>> sk.tree_evaluation(layer) | ||
Identity() | ||
""" | ||
|
||
def is_leaf(x: Callable[[Any], bool]) -> bool: | ||
types = set(tree_evaluation.evaluation_dispatcher.registry.keys()) | ||
types.discard(object) | ||
return isinstance(x, tuple(types)) | ||
|
||
return jax.tree_map(tree_evaluation.evaluation_dispatcher, tree, is_leaf=is_leaf) | ||
|
||
|
||
tree_evaluation.evaluation_dispatcher = ft.singledispatch(lambda x: x) | ||
tree_evaluation.def_evalutation = tree_evaluation.evaluation_dispatcher.register |
Oops, something went wrong.