AttributeError: module 'tensorflow._api.v1.compat.v1.nn' has no attribute 'avg_pool2d'

AttributeError: module 'tensorflow._api.v1.compat.v1.nn' has no attribute 'avg_pool2d'

有人可以帮我解决这个问题吗?

  File "C:\tensorflow1\models\research\slim\nets\mobilenet\mobilenet.py", line 397, in <module>
    def global_pool(input_tensor, pool_op=tf.compat.v1.nn.avg_pool2d):
AttributeError: module 'tensorflow._api.v1.compat.v1.nn' has no attribute 'avg_pool2d'

这是代码

def global_pool(input_tensor, pool_op=tf.nn.avg_pool2d):
  shape = input_tensor.get_shape().as_list()
  if shape[1] is None or shape[2] is None:
    kernel_size = tf.convert_to_tensor(
        [1, tf.shape(input_tensor)[1],
         tf.shape(input_tensor)[2], 1])
  else:
    kernel_size = [1, shape[1], shape[2], 1]
  output = pool_op(
      input_tensor, ksize=kernel_size, strides=[1, 1, 1, 1], padding='VALID')
  # Recover output shape, for unknown shape.
  output.set_shape([None, 1, 1, None])
  return output

应该是tf.nn.avg_pool2d(...)这样吧?我确实在寻找解决方案。但我真的不明白。

您是否尝试过将其更改为 tf.nn.avg_pool? 它似乎对我有用。