codecamp

如何在TensorFlow张量形状中插入维度

tf.expand_dims

expand_dims(
    input,
    axis=None,
    name=None,
    dim=None
)

定义在:tensorflow/python/ops/array_ops.py

参见指南:张量变换>形状的确定与改变

在张量形状中插入 1 的维度.

给定一个张量 input,此操作在 input 的形状的维度索引轴中插入1的维度.该维度的索引轴从零开始;如果为该坐标轴指定负数,它将从末尾向后计数.

如果您想将批维度添加到单个元素,此操作非常有用.例如,如果您有一个形状为 [height, width, channels] 的单一图像,您可以将它与 expand_dims(image, 0) 进行批处理,这将生成形状 [1, height, width, channels].

以下是其他的例子:

# 't' is a tensor of shape [2]
shape(expand_dims(t, 0)) ==> [1, 2]
shape(expand_dims(t, 1)) ==> [2, 1]
shape(expand_dims(t, -1)) ==> [2, 1]

# 't2' is a tensor of shape [2, 3, 5]
shape(expand_dims(t2, 0)) ==> [1, 2, 3, 5]
shape(expand_dims(t2, 2)) ==> [2, 3, 1, 5]
shape(expand_dims(t2, 3)) ==> [2, 3, 5, 1]

上述操作要求:

-1-input.dims() <= dim <= input.dims()

该操作与 squeeze() 有关,它删除大小为1的维度.

参数:

  • input:是一个张量.
  • axis:0 维(标量)指定要在其上展开 input 形状的维度索引.
  • name:output 张量的名称.
  • dim:0 维(标量).相当于 axis,不推荐使用.

返回值:

与 input 具有相同数据的张量,但其形状具有附加的大小为1维度.

注意:

  • ValueError:如果指定了 dim 和 axis.


计算TensorFlow元素的指数
构造TensorFlow的单位矩阵
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

TensorFlow 函数介绍

TensorFlow 函数模块:tf

TensorFlow的image模块

TensorFlow使用之tf.io

TensorFlow使用之tf.keras

TensorFlow函数教程:tf.keras.applications

TensorFlow函数教程:tf.keras.backend

TensorFlow使用之tf.metrics

TensorFlow使用之tf.nn

TensorFlow使用之tf.python_io

TensorFlow 功能函数

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }