AttributeError : module 'tensorflow' has no attribute 'contrib' - python 3.8

AttributeError : module 'tensorflow' has no attribute 'contrib' - python 3.8

我正在进行从 Tensorflow 1.15 到 2.4.1 ( python 3.8) 的升级项目。以下是我面临的问题。 我知道问题出在 tf.contrib。我该如何进行这项工作?

if num_epochs is not None and shuffle:
        dataset = dataset.apply(
            tf.contrib.data.shuffle_and_repeat(buffer_size=batch_size * 10, count=num_epochs)
        )
    elif shuffle:
        dataset = dataset.shuffle(buffer_size=batch_size * 10)
    elif num_epochs is not None:
        dataset = dataset.repeat(count=num_epochs)

    dataset = dataset.apply(
        tf.contrib.data.map_and_batch(map_func=parse_csv,
                                      batch_size=batch_size,
                                      num_parallel_calls=tf.data.experimental.AUTOTUNE)
    )

    I have another block below

    distribution_strategy = tf.contrib.distribute.MirroredStrategy(num_gpus=num_gpus,
                                                                   prefetch_on_device=True,
                                                                   auto_shard_dataset=True)

如何在 Tensorflow 2.4.1 中实现同样的功能

tf.contrib 不再存在于 tf2 中。你在 tf2 中转换后的代码是这样的:

if num_epochs is not None and shuffle:
    dataset = dataset.shuffle(batch_size * 10).repeat(num_epochs)
elif shuffle:
    dataset = dataset.shuffle(batch_size * 10)
elif num_epochs is not None:
    dataset = dataset.repeat(num_epochs)

dataset = dataset.map(parse_csv, num_parallel_calls=tf.data.AUTOTUNE).batch(batch_size)

distribution_strategy = tf.distribute.MirroredStrategy()