Giter VIP home page Giter VIP logo

Comments (7)

sushreebarsa avatar sushreebarsa commented on July 19, 2024

@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.

marcobornstein avatar marcobornstein commented on July 19, 2024

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.

sushreebarsa avatar sushreebarsa commented on July 19, 2024

@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.

marcobornstein avatar marcobornstein commented on July 19, 2024

@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.

sushreebarsa avatar sushreebarsa commented on July 19, 2024

@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.

marcobornstein avatar marcobornstein commented on July 19, 2024

@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.

sushreebarsa avatar sushreebarsa commented on July 19, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.