Skip to content

Commit

Permalink
Generate nb and md files
Browse files Browse the repository at this point in the history
  • Loading branch information
nkovela1 committed Aug 4, 2023
1 parent 1e92e4f commit 80d450a
Show file tree
Hide file tree
Showing 3 changed files with 837 additions and 9 deletions.
19 changes: 10 additions & 9 deletions guides/customizing_saving_and_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Date created: 2023/03/15
Last modified: 2023/03/15
Description: A more advanced guide on customizing saving for your layers and models.
Accelerator: None
"""

"""
Expand Down Expand Up @@ -78,7 +79,9 @@ def save_own_variables(self, store):
class LayerWithCustomVariables(keras.layers.Dense):
def __init__(self, units, **kwargs):
super().__init__(units, **kwargs)
self.stored_variables = tf.Variable(np.random.random((10,)), name="special_arr", dtype=tf.float32)
self.stored_variables = tf.Variable(
np.random.random((10,)), name="special_arr", dtype=tf.float32
)

def save_own_variables(self, store):
super().save_own_variables(store)
Expand All @@ -98,9 +101,7 @@ def call(self, inputs):
return super().call(inputs) * self.stored_variables


model = keras.Sequential([
LayerWithCustomVariables(1)
])
model = keras.Sequential([LayerWithCustomVariables(1)])

ref_input = np.random.random((8, 10))
ref_output = np.random.random((8,))
Expand All @@ -112,7 +113,7 @@ def call(self, inputs):

np.testing.assert_allclose(
model.layers[0].stored_variables.numpy(),
restored_model.layers[0].stored_variables.numpy()
restored_model.layers[0].stored_variables.numpy(),
)

"""
Expand Down Expand Up @@ -149,9 +150,9 @@ def load_assets(self, inner_path):
self.vocab = text.replace("<unk>", "little")


model = keras.Sequential([
LayerWithCustomAssets(vocab='Mary had a <unk> lamb.', units=5)
])
model = keras.Sequential(
[LayerWithCustomAssets(vocab="Mary had a <unk> lamb.", units=5)]
)

x = np.random.random((10, 10))
y = model(x)
Expand Down Expand Up @@ -338,4 +339,4 @@ def compile_from_config(self, config):
- `get_compile_config` and `compile_from_config` save and restore the model's
compiled states.
"""
"""
Loading

0 comments on commit 80d450a

Please sign in to comment.