Giter Club home page Giter Club logo

Comments (3)

fchollet avatar fchollet commented on July 18, 2024 1

That worked because tf.random.normal can operate over symbolic TF tensors. To write equivalent code, just make a Normal() Keras layer -- since layers can operate over symbolic tensors too. But in practice I think you're going to want to put more logic in your layer than a single RNG call, for good factoring.

from keras.

fchollet avatar fchollet commented on July 18, 2024

You cannot create an eager random tensor with an undefined shape (here, (None, 1024, 12)). This has never worked because it is conceptually impossible. I am not sure what setup you are referring to when you say, "this used to work", but in any case that's not it.

You can simply use a layer to do what you describe. Like this.

import keras


class MyLayer(keras.Layer):
    def __init__(self, **kwargs):
        super().__init__()
        self.seed_generator = keras.random.SeedGenerator(seed=1337)

    def call(self, z_mean):
        eps = keras.random.normal(shape=keras.ops.shape(z_mean), seed=self.seed_generator)
        return z_mean + eps


z_mean = keras.Input(shape=(1024, 12))
z_mean_plus_eps = MyLayer()(z_mean)

from keras.

lcs-crr avatar lcs-crr commented on July 18, 2024

Thank you for your reply and the proposed solution.
I realised that perhaps I simplified my question too much.
When I said that

Sampling a partially None-shaped tensor worked fine in Keras 2

I was refering to the following "old" code:
import tensorflow as tf # ==2.10.1
Z_mean = tf.keras.layers.Input(shape=(1024, 12))
eps = tf.random.normal(shape=tf.shape(Z_mean))

If I didn't make a mistake, the keras 3 equivalent should be:
import keras # ==3.3.3
Z_mean = keras.Input(shape=(1024, 12))
eps = keras.random.normal(shape=keras.ops.shape(Z_mean))
which, as you pointed out, does not work.
Why so? What would be the working equivalent of the "old" code?

from keras.

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.