【问题标题】:AttributeError : module 'tensorflow' has no attribute 'contrib' - python 3.8AttributeError:模块'tensorflow'没有属性'contrib'-python 3.8
【发布时间】:2021-08-14 14:51:41
【问题描述】:

我正在进行从 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 中实现同样的效果

【问题讨论】:

    标签: python-3.x tensorflow tensorflow2.0


    【解决方案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()
    

    【讨论】:

    • 在最近的tensorflow版本中,AUTOTUNEtf.data.AUTOTUNE
    • @jakub 是的。我忘记改了。谢谢
    猜你喜欢
    • 1970-01-01
    • 2021-06-23
    • 2022-11-09
    • 2020-08-17
    • 2020-03-17
    • 1970-01-01
    • 2019-09-16
    • 2018-04-28
    • 1970-01-01
    相关资源
    最近更新 更多