我用
layers.Normalization()
Run Code Online (Sandbox Code Playgroud)
在 Keras 中,keras.Sequential
当我尝试运行它时,出现以下错误:
模块“tensorflow.keras.layers”没有属性“标准化”
我看到layers.Normalization()
很多代码中都使用了该命令,所以我不知道出了什么问题。有什么改变吗?
我正在尝试运行https://www.tensorflow.org/probability/examples/Probabilistic_Layers_VAE中找到的代码。
我使用的是 Python 版本 3.9,我的 TensorFlow 版本是 >2.0。代码如下:
import tensorflow.compat.v2 as tf
tf.enable_v2_behavior()
import tensorflow_datasets as tfds
import tensorflow_probability as tfp
tfk = tf.keras
tfkl = tf.keras.layers
tfpl = tfp.layers
tfd = tfp.distributions
datasets, datasets_info = tfds.load(name='mnist', with_info=True, as_supervised=False)
def _preprocess(sample):
image = tf.cast(sample['image'], tf.float32) / 255 #Scale to [0, 1]
image = image < tf.random.uniform(tf.shape(image)) #Gives 0, 1 when compared to a random number
return image, image
train_dataset = (datasets['train']
.map(_preprocess)
.batch(256)
.prefetch(tf.data.experimental.AUTOTUNE)
.shuffle(int(10e3)))
Run Code Online (Sandbox Code Playgroud)
我得到的是以下警告:
WARNING:tensorflow:AutoGraph could not …
Run Code Online (Sandbox Code Playgroud)