tf.expand_dims用法详解

看官方讲解一些博客感觉一直不是很懂,下面是我的个人理解结合官方文档,有问题欢迎指出

tf.expand_dims

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

给定的张量input,该操作插入尺寸索引处的1维axisinput的形状。维度索引axis从零开始;如果您为其指定负数,axis则从末开始算起。

如果要将批次尺寸添加到单个元素,此操作很有用。例如,如果您有一个shape的图像[height, width, channels],则可以用制作一批1张图像expand_dims(image, 0),这将使shape成为[1, height, width, channels]

简单来说增加一个维度

import numpy as np
import tensorflow as tf
from numpy import array

current=np.array([
        [0,7,1,2,2],
        [1,7,3,4,3],
        [2,7,5,6,6],
        [3,7,7,8,7],
        [4,7,7,8,7],
        [5,7,7,8,7]
])

current = array(current)
current = tf.constant(current)
points_e = tf.expand_dims(current, axis=0)

注意看处理结果 

官方例子 shape维度

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

# 't2' is a tensor of shape [2, 3, 5]
tf.shape(tf.expand_dims(t2, 0))  # [1, 2, 3, 5]
tf.shape(tf.expand_dims(t2, 2))  # [2, 3, 1, 5]
tf.shape(tf.expand_dims(t2, 3))  # [2, 3, 5, 1]
  • 29
    点赞
  • 68
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值