-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move layer transformer into separate file
Signed-off-by: Tushar Mittal <[email protected]>
- Loading branch information
1 parent
7fbe8df
commit cc95068
Showing
6 changed files
with
141 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import json as _json | ||
from typing import Type | ||
|
||
import tensorflow as tf | ||
|
||
from flytekit.core.context_manager import FlyteContext | ||
from flytekit.core.type_engine import TypeEngine, TypeTransformer, TypeTransformerFailedError | ||
from flytekit.models.literals import Literal, Primitive, Scalar | ||
from flytekit.models.types import LiteralType, SimpleType | ||
|
||
|
||
class TensorflowLayerTransformer(TypeTransformer[tf.keras.layers.Layer]): | ||
def __init__( | ||
self, | ||
): | ||
super().__init__(name="Tensorflow Layer", t=tf.keras.layers.Layer) | ||
|
||
def get_literal_type(self, t: Type[tf.keras.layers.Layer]) -> LiteralType: | ||
return LiteralType(simple=SimpleType.STRING) | ||
|
||
def to_literal( | ||
self, | ||
ctx: FlyteContext, | ||
python_val: tf.keras.layers.Layer, | ||
python_type: Type[tf.keras.layers.Layer], | ||
expected: LiteralType, | ||
) -> Literal: | ||
layer_config = tf.keras.layers.serialize(python_val) | ||
|
||
return Literal(Scalar(primitive=Primitive(string_value=_json.dumps(layer_config)))) | ||
|
||
def to_python_value( | ||
self, ctx: FlyteContext, lv: Literal, expected_python_type: Type[tf.keras.layers.Layer] | ||
) -> tf.keras.layers.Layer: | ||
if not (lv and lv.scalar and lv.scalar.primitive): | ||
raise TypeTransformerFailedError(f"Cannot convert from {lv} to {expected_python_type}") | ||
|
||
layer_config = _json.loads(lv.scalar.primitive.string_value) | ||
return tf.keras.layers.deserialize(layer_config) | ||
|
||
def guess_python_type(self, literal_type: LiteralType) -> Type[tf.keras.layers.Layer]: | ||
if literal_type.simple == literal_type.simple.STRUCT: | ||
return tf.keras.layers.Layer | ||
|
||
raise ValueError(f"Transformer {self} cannot reverse {literal_type}") | ||
|
||
|
||
TypeEngine.register(TensorflowLayerTransformer()) |
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,48 @@ | ||
from typing import Any, List | ||
|
||
import tensorflow as tf | ||
|
||
from flytekit import task, workflow | ||
|
||
|
||
@task | ||
def get_layer() -> tf.keras.layers.Dense: | ||
layer = tf.keras.layers.Dense(10) | ||
layer(tf.ones((10, 1))) | ||
return layer | ||
|
||
|
||
@task | ||
def generate_sequential_model() -> List[tf.keras.layers.Layer]: | ||
model = tf.keras.Sequential( | ||
[ | ||
tf.keras.layers.Input(shape=(32,)), | ||
tf.keras.layers.Dense(1), | ||
] | ||
) | ||
model.compile( | ||
optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), | ||
loss=tf.keras.losses.BinaryCrossentropy(), | ||
metrics=[ | ||
tf.keras.metrics.BinaryAccuracy(), | ||
], | ||
) | ||
return model.layers | ||
|
||
|
||
@task | ||
def get_layers_weights(layers: List[tf.keras.layers.Layer]) -> List[Any]: | ||
return layers[-1].weights | ||
|
||
|
||
@workflow | ||
def wf(): | ||
dense_layer = get_layer() | ||
layers = generate_sequential_model() | ||
get_layers_weights(layers=[dense_layer]) | ||
get_layers_weights(layers=layers) | ||
|
||
|
||
@workflow | ||
def test_wf(): | ||
wf() |
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