Comments (7)
@marcobornstein Could you try to use CustomRescaling layer that is defined to take a scaling vector as an input and scale the inputs accordingly. It overrides the get_config and from_config methods to ensure proper serialization and deserialization. For any further queries please raise the issue in Keras repository. Thank you!
from tensorflow.
Sorry @sushreebarsa, I am a bit confused. I have not defined a CustomRescaling layer in the code above. Previously, this Rescaling layer would work. I have also tried to implement a custom Rescalying layer (which you may be mentioning), however I still get issues with the get_config and from_config even if I write those functions myself. What other options exist?
from tensorflow.
@marcobornstein If the TensorFlow versions seem compatible, there's a chance the model saving format might have changed between them. Could you try saving the model with the same TensorFlow version you used previously. This might resolve the loading issue?
Thank you!
from tensorflow.
@sushreebarsa yes, the previous TensorFlow version does allow correct model saving of the Rescaling layers. My issue is that this no longer works in TensorFlow 2.16. Is it possible to have this bug fixed? I am hoping to continue usage of TensorFlow 2.16 for my work.
from tensorflow.
@marcobornstein By saving your model again using the exact same TensorFlow version you used for training, please ensure the model is saved in the format that version expects. When you then load the model with the same version, there's a much higher chance of successful loading because the format and information about the re-scaling layer will be consistent.
Thank you!
from tensorflow.
@sushreebarsa, I am training and reloading the model using the exact same TensorFlow method. This can be shown in the "Standalone code to reproduce the issue" section of my Issue. How can this issue be fixed?
from tensorflow.
@marcobornstein Thank you for the update!
In TensorFlow/Keras, after you save and load a model, you need to recompile it before using it again.
Please follow the below code and let us know if it helps?
import numpy as np
import tensorflow as tf
# Fake data
X = np.random.rand(100, 10)
Y = np.random.rand(100, 5)
r = np.random.rand(5)
# Build/compile/fit model
model = tf.keras.Sequential([
tf.keras.layers.Dense(100, activation="relu", name="layer1"),
tf.keras.layers.Dense(10, activation="relu", name="layer2"),
tf.keras.layers.Dense(5, name="layer3"),
])
model.compile(optimizer="adam", loss="mse")
model.fit(X, Y, epochs=50)
# Save model
model.save('model.keras')
# Load model
model = tf.keras.models.load_model('model.keras')
# Add rescaling layer after loading
model.add(tf.keras.layers.Rescaling(r))
# Test point
x_tst = np.random.rand(1, 10)
# Print prediction (should work after recompiling)
print(model(x_tst))
# Summary of the model
model.summary()
After loading the model, adding the Rescaling layer, and before using the model for prediction, we should recompile it:
# Recompile the model after adding Rescaling layer
model.compile(optimizer="adam", loss="mse")
# Now the prediction would run successful
print(model(x_tst))
Please find the gist here for reference.
Thank you!
from tensorflow.
Related Issues (20)
- Unable to use tf and its modules including keras and etc, where the tensorflow==2.15.1 and keras==2.15.0 HOT 5
- Relu activation in TF Lite model return the negative values HOT 2
- LSTM model conversion failed from 'from_keras_model' HOT 3
- C-API support for .keras models HOT 4
- tfm.nlp.layers.RelativePositionEmbedding HOT 2
- Document tf.Dataset.zip/map triggers tf.Dataset.shuffle HOT 1
- Data Service Dynamic sharding policy seems extremely slow HOT 11
- ModuleNotFoundError: No module named 'tflite_support.task' HOT 3
- Notebook is not working anymore HOT 3
- Kubernetes cluster resolver fails when running from within a K8S cluster.
- Tried multiple builds, Bazel, pip, file. non of it is working HOT 5
- tensorflow uninstallable HOT 1
- Python interpreter set from ./configure does not go with py_strict_test HOT 3
- TensorFlow Lite label_image fails to build with cmake HOT 6
- Tflite model fails on Android GPU delegate due to 'null pointer dereference' HOT 3
- how to assign tensorflow operation running-time device HOT 2
- tf.matmul gives inconsistent results for same data HOT 1
- Indexing error (graph execution error) by tf.keras.metrics.OneHotIoU metric HOT 5
- Tflite inference with multiple inputs and outputs on Android
- Data augmentation for object detection with bounding boxes HOT 2
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 tensorflow.