Comments (3)
@sachinprasadhs,
I was able to reproduce the issue on tensorflow v2.15 with Keras2. Kindly find the gist of it here.
Triage Note: keras-v3 does not support adding constants to the call method, which is different from the tf-keras implementation.
from tf-keras.
Hi @claCase! Could you try using using the new Keras v3 (.keras) format for saving? Alternatively, we suggest you use .save_weights
instead. In general saved model is not as recommended as newer methods.
from tf-keras.
Hi @grasskin
By utilizing the .keras extension and slightly refactoring the model I was able to successfully save it. Please see the reference code below.
Many thanks! I'm closing the issue.
import tensorflow as tf
from tensorflow.python.keras.layers.recurrent import (
DropoutRNNCellMixin,
_config_for_enable_caching_device,
_caching_device,
)
tf.keras.saving.get_custom_objects().clear()
@tf.keras.saving.register_keras_serializable(package="custom_package")
class RNNWithConstants(
DropoutRNNCellMixin, tf.keras.__internal__.layers.BaseRandomLayer
):
def __init__(
self,
units,
activation,
recurrent_activation,
dropout,
recurrent_dropout,
**kwargs,
):
super(RNNWithConstants, self).__init__(**kwargs)
self.units = units
self.dropout = dropout
self.recurrent_dropout = recurrent_dropout
self.recurrent_activation = recurrent_activation
self.cell = tf.keras.layers.GRUCell(
units=units,
activation=activation,
recurrent_activation=recurrent_activation,
recurrent_dropout=recurrent_dropout,
dropout=dropout,
)
self.state_size = units
self.output_size = units
def build(self, inputs_shape, *args):
print(inputs_shape)
super().build(inputs_shape)
@tf.function
def call(self, inputs, states, constants, **kwargs):
print(f"inputs {inputs.shape}")
print(f"states {states[0].shape}")
print(f"constants {constants[0].shape}")
inputs = tf.concat([inputs, constants[0]], axis=-1)
h, _ = self.cell(inputs, states)
return h, h
@tf.keras.saving.register_keras_serializable(package="custom_package")
class ConstantsModel(tf.keras.models.Model):
def __init__(self, units, **kwargs):
super().__init__(**kwargs)
self.units = units
self.cell = RNNWithConstants(units, "sigmoid", "sigmoid", 0.1, 0.1)
self.rnn = tf.keras.layers.RNN(self.cell)
@tf.function
def call(self, inputs, training):
inputs, constants = inputs[0], inputs[1]
return self.rnn(inputs, constants=constants)
const = ConstantsModel(10)
print("initializing...")
_ = const((tf.random.normal(shape=(100, 50, 10)), tf.random.normal(shape=(100, 10))), False)
const.save("./const_model.keras")
print("\nloading....")
const2 = tf.keras.models.load_model("./const_model.keras")
print("\ninitializing new model....")
_ = const2((tf.random.normal(shape=(100, 50, 10)), tf.random.normal(shape=(100, 10))), False)
initializing...
(100, 10)
inputs (100, 10)
states (100, 10)
constants (100, 10)
inputs (100, 10)
states (100, 10)
constants (100, 10)
loading....
(100, 10)
inputs (100, 10)
states (100, 10)
constants (100, 10)
inputs (100, 10)
states (100, 10)
constants (100, 10)
initializing new model....
inputs (100, 10)
states (100, 10)
constants (100, 10)
from tf-keras.
Related Issues (20)
- Error while importing tf_keras HOT 11
- shape issue for y_pred for a custom made loss function HOT 3
- UNIMPLEMENTED: Cast string to float is not supported; CANCELLED: Function was cancelled before it was started HOT 3
- TextVectorization: output_mode={multi_hot, count} promise int arrays but output floats
- Cloning a TextVectorization Layer with Split Function Doesn't Work HOT 6
- Mirrored strategy model.load_weights() failure HOT 2
- sparse_categorical_crossentropy with ignore_class=-1 makes loss to `nan` HOT 3
- Importing `tf_keras` to use Keras 2 in TensorFlow 2.16 fails HOT 9
- Addition of Reflect Padding Functionality for tf.keras Convolutional Layers HOT 2
- AttributeError: module 'tf_keras.backend' has no attribute 'logsumexp' HOT 6
- ValueError: (F1Score|FBetaScore) expects 2D inputs with shape (batch_size, output_dim). HOT 8
- LSTM - different outputs for same weights across CPU and GPU, when using float32 + tf-keras + NVIDIA A100 HOT 4
- RetinaNet with custom backbone and custom dataset HOT 3
- IntegerLookup with XLA Compilation Fails to Enable JIT in TensorFlow 2.16.1 and Keras 3.2.1 HOT 3
- conda-forge release HOT 1
- Failed to build Python package due to missing layer/experimental build HOT 2
- TF-Keras mixed precision training leads to autograph errors HOT 4
- Showing warning just after importing tf_keras HOT 6
- Error loading the trained model. HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tf-keras.