当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python tf.compat.v1.layers.flatten用法及代码示例


展平输入张量,同时保留批处理轴(轴 0)。

用法

tf.compat.v1.layers.flatten(
    inputs, name=None, data_format='channels_last'
)

参数

  • inputs 张量输入。
  • name 图层的名称(字符串)。
  • data_format 一个字符串,是 channels_last (默认)或 channels_first 之一。输入中维度的排序。 channels_last 对应于形状为 (batch, height, width, channels) 的输入,而 channels_first 对应于形状为 (batch, channels, height, width) 的输入。

返回

  • 重塑张量。

迁移到 TF2

警告:这个 API 是为 TensorFlow v1 设计的。继续阅读有关如何从该 API 迁移到本机 TensorFlow v2 等效项的详细信息。见TensorFlow v1 到 TensorFlow v2 迁移指南有关如何迁移其余代码的说明。

此 API 是一个遗留 api,仅与 Eager Execution 和 tf.function 兼容,如果您将其与 tf.compat.v1.keras.utils.track_tf1_style_variables 结合使用

请参阅迁移指南的 tf.layers 模型映射部分,了解如何在 TF2 中将 TensorFlow v1 模型与 Keras 一起使用。

对应的 TensorFlow v2 层是 tf.keras.layers.Flatten

到原生 TF2 的结构映射

支持的参数均未更改名称。

前:

y = tf.compat.v1.layers.flatten(x)

后:

要使用 TF1 函数层迁移代码,请使用 Keras 函数 API:

x = tf.keras.Input((28, 28, 1))
 y = tf.keras.layers.Flatten()(x)
 model = tf.keras.Model(x, y)

例子:

x = tf.compat.v1.placeholder(shape=(None, 4, 4), dtype='float32')
  y = flatten(x)
  # now `y` has shape `(None, 16)`

  x = tf.compat.v1.placeholder(shape=(None, 3, None), dtype='float32')
  y = flatten(x)
  # now `y` has shape `(None, None)`

相关用法


注:本文由纯净天空筛选整理自tensorflow.org大神的英文原创作品 tf.compat.v1.layers.flatten。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。