Comments (3)
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.
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.
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)
- Autocompletion/hints fail for keras.Variables HOT 4
- BatchNormalization gives incorrect output with masked inputs > 3 dimensions HOT 3
- `CompileLoss` truncates multiple loss inputs HOT 3
- [Feature Request] Batch Renormalization HOT 1
- [Feature request] Complex dtype input support for layers HOT 1
- Preprocessing API Deprecated HOT 3
- Think about Keras3 preference, should I develop my distributed training CTR model based on Jax or TensorFlow? HOT 1
- TimeDistributed() and integration with a tf.data.Dataset HOT 1
- The documentation for Conv1DTranspose does not state that the CPU does not support dilation rates larger than 1
- Request for groups parameter support in Conv2DTranspose/Conv1DTranspose Layer HOT 1
- ValueError: Passed in object <KerasVariable shape=(1, 64), dtype=float64, path=dense_15/kernel> of type 'Variable', not tf.Tensor or tf.Variable or ExtensionType.
- keras-nightly: cannot import name '_tf_keras' from 'keras.api' HOT 4
- Error while exporting from keras format to TF SavedModel format, tf 2.16.1 and keras 3.3.3 HOT 3
- Keras 3 error with keras.ops.tile
- Why does the sequence length of vectors affect the calculation results of dense under bf16? HOT 2
- Are `ops.shape` vs `KerasTensor.shape` equivalent for most purposes? HOT 3
- [Help] Where is the trace/history of operations that led to a KerasTensor stored? HOT 2
- Should these 2 networks produce a different plot ? (to my eye, they are the same network.) HOT 8
- loss/losses are omitted from get_config/serialization
- Couldn't found `keras.backend` module in keras-nightly HOT 1
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 keras.