【深度学习_TensorFlow】维度变换

本文介绍了TensorFlow中处理张量的关键操作,包括tf.shape获取张量形状,tf.ndim获取维度,tf.reshape改变形状,tf.transpose进行转置,以及tf.expand_dims和tf.squeeze用于维度的增减。这些函数对于理解和操作张量至关重要。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

写在前面

以下是 TensorFlow 中的一些常用操作,用于处理张量形状、维度和其他相关属性,希望文章对你有帮助!

  1. tf.shape: 获取张量的形状。

  2. tf.ndim: 获取张量的维度。

  3. tf.reshape: 改变张量的形状。

  4. tf.transpose: 交换张量的维度。

  5. tf.expand_dims: 在指定维度上添加新维度。

  6. tf.squeeze: 从张量中删除指定维度的所有元素。


写在中间

( 1 )shape看形状,ndim识维度

# 导入 TensorFlow 库
import tensorflow as tf
# 定义一个随机生成的张量
tensor = tf.random.normal([2, 3, 4])
# 使用 tf.shape 函数获取张量的形状
shape = tf.shape(tensor)
# 打印张量的形状
print(shape)  # 输出:(2, 3, 4)
# 获取张量的维数
ndim = tf.ndim(tensor)
# 打印张量的维数
print(ndim)  # 输出:2


( 2 )reshape变形状

tf.reshape(tensor, shape)用于将张量(Tensor)的形状(shape)转换为所需的形状。tensor是要转换的张量,shape是新形状的元组,

  • tensor:输入张量,可以是任何类型的Tensor。
  • shape:新形状的元组,必须是一个长度为1的元组。

reshape 是一种用于改变张量形状的操作。我们在此之前,先来理解一个张量tensor = [10, 28, 28, 3],表示10张28×28的RGB三通道彩色照片。

import tensorflow as tf
# 创建一个随机张量
tensor = tf.random.normal([10, 28, 28, 3])
print(tensor.shape, tensor.ndim)  # 输出:(10, 28, 28, 3) 4

# 改变形状,改变后需要保证原来图像的数据和改变后的数据个数相同,这里抹除了行和列的概念,变成784个像素去考虑
tensor = tf.reshape(tensor, [10, 784, 3])
print(tensor.shape, tensor.ndim)  # 输出:(10, 784, 3) 3

# 改变形状,不想计算维度合并后28×28具体值,可以使用-1代替
tensor = tf.reshape(tensor, [10, -1, 3])
print(tensor.shape, tensor.ndim)  # 输出:(10, 784, 3) 3
# 改变形状
tensor = tf.reshape(tensor, [10, 784*3])
print(tensor.shape, tensor.ndim)  # 输出:(10, 2352) 2
# 改变形状
tensor = tf.reshape(tensor, [10, -1])
print(tensor.shape, tensor.ndim)  # 输出:(10, 2352) 2


( 3 )transpose转置维度

tf.transpose(tensor, perm=[])是一个用于张量转置的函数。在这个函数中,我们可以传递一些参数来控制转置的方式。

  • tensor:需要进行转置的张量。

  • perm:一个表示转置顺序的张量。这个张量的形状应该与tensor的形状相同,并且元素取值范围应该为:[[0, 1], [1, 0]]。如果perm为空,则默认按照元素的索引进行转置。

若不指定参数,默认将tensor全部转置,如在矩阵中的行列互换,从3行2列变成2行3列。
若指定参数perm,perm = [0, 2, 1]表示重新排列维度,将原来的维度1和维度2进行调换

不指定轴转置

import tensorflow as tf
tensor = tf.range([12])
tensor = tf.reshape(tensor, [4, 3])

print(tensor)
# 输出:
# tf.Tensor(
# [[ 0  1  2]
#  [ 3  4  5]
#  [ 6  7  8]
#  [ 9 10 11]], shape=(4, 3), dtype=int32)

tensor = tf.transpose(tensor) # 行列转换
print(tensor)
# 输出:
# tf.Tensor(
# [[ 0  3  6  9]
#  [ 1  4  7 10]
#  [ 2  5  8 11]], shape=(3, 4), dtype=int32)

指定轴转置

import tensorflow as tf

tensor = tf.range([36])

tensor = tf.reshape(tensor, [3, 4, 3])
print(tensor)
# 输出:
# tf.Tensor(
# [[[ 0  1  2]
#   [ 3  4  5]
#   [ 6  7  8]
#   [ 9 10 11]]
#
#  [[12 13 14]
#   [15 16 17]
#   [18 19 20]
#   [21 22 23]]
#
#  [[24 25 26]
#   [27 28 29]
#   [30 31 32]
#   [33 34 35]]], shape=(3, 4, 3), dtype=int32)

tensor = tf.transpose(tensor, perm=[0, 2, 1]) # 行列转换
print(tensor)
# 输出:
# tf.Tensor(
# [[[ 0  3  6  9]
#   [ 1  4  7 10]
#   [ 2  5  8 11]]
# 
#  [[12 15 18 21]
#   [13 16 19 22]
#   [14 17 20 23]]
# 
#  [[24 27 30 33]
#   [25 28 31 34]
#   [26 29 32 35]]], shape=(3, 3, 4), dtype=int32)

( 4 )tf.expand_dims增加维度

tf.expand_dims(tensor, axis=None) 是 TensorFlow 提供的一个函数,用于在 TensorFlow 计算图中的给定轴(axis)上扩展一个张量(tensor)。这个函数的主要作用是增加张量的维度。

  • tensor:要扩展的张量。

  • axis:要扩展的轴的索引。轴的索引从 0 开始,表示第一维。

import tensorflow as tf
tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tensor = tf.expand_dims(tensor, axis=0) # 在第一维上扩展
print(tensor)
# 输出:
# tf.Tensor(
# [[[1 2 3]
#   [4 5 6]]], shape=(1, 2, 3), dtype=int32)
tensor = tf.expand_dims(tensor, axis=1) # 在第二维上扩展
print(tensor)
# 输出:
# tf.Tensor(
# [[[[1 2 3]
#    [4 5 6]]]], shape=(1, 1, 2, 3), dtype=int32)


( 5 )tf.squeeze减少维度

tf.squeeze(tensor, axis) 用于删除张量的维度。它接受一个张量作为输入,将原始tensor中所有维度为1的维度都删掉,并返回一个新的张量

  • tensor:作为输入张量

  • axis:表示要删除的维度。

import tensorflow as tf

tensor = tf.constant([[1, 2, 3], [4, 5, 6]])
tensor = tf.expand_dims(tensor, axis=0)  # 在第一维上扩展
tensor = tf.expand_dims(tensor, axis=1)  # 在第二维上扩展
print(tensor)
# 输出:
# tf.Tensor(
# [[[[1 2 3]
#    [4 5 6]]]], shape=(1, 1, 2, 3), dtype=int32)

tensor = tf.squeeze(tensor) # 删除为1的维度
print(tensor)
# 输出:
# tf.Tensor(
# [[1 2 3]
#  [4 5 6]], shape=(2, 3), dtype=int32)


写在最后

👍🏻点赞,你的认可是我创作的动力!
⭐收藏,你的青睐是我努力的方向!
✏️评论,你的意见是我进步的财富!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

畅游星辰大海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值