[TensorFlow系列-13]:TensorFlow基础 - 张量的操作 - 筛选、过滤

作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119651022


目录

 第1章 Tensor运算概述

1.1 概述

1.3 张量的操作与变换

1.4 环境准备

1.5 张量的操作 - 筛选过滤

第2章 单一维度方向的任意顺序的切片:tf.gather(a, axis,indices)

2.1 源张量是一维数据

2.2 源张量是二维数据

第3章 全维度方向的任意顺序的切片:gather_nd(a, indices)

3.1 任意取一个元素,构建一个一维张量

3.2 任意取多个元素,构建一个一维张量

3.3 任意取一个维度的所有数据,构建一个一维张量

3.4 任意取多个元素,构建一个二维张量


 第1章 Tensor运算概述

https://tensorflow.google.cn/api_docs/python/tf

1.1 概述

Tensorflow提供了大量的张量运算,基本上可以对标Numpy多维数组的运算,以支持对张量的各种复杂的运算。

这些操作运算中大多是对数组中每个元素执行相同的函数运算,并获得每个元素函数运算的结果序列,这些序列生成一个新的同维度的数组。

1.2 运算分类

(1)算术运算:加、减、系数乘、系数除

(2)函数运算:sin,cos

(3)取整运算:上取整、下取整

(4)统计运算:最大值、最小值、均值

(5)比较运算:大于,等于,小于、排序

(6)线性代数运算:矩阵、点乘、叉乘

1.3 张量的操作与变换

(1)变换形态

(2)变换内容

1.4 环境准备

#环境准备
import numpy as np
import tensorflow as tf
print("hello world")
print("tensorflow version:", tf.__version__)

1.5 张量的操作 - 筛选过滤

Tensorflow提供了tf.gather和tf.gather_nd对张量元素进行过滤,生成一个新的张量。

tf.gather:单一维度方向的数据,进行任意顺序的切片。

tf.gather_nd:任意维度方向的数据,进行任意维度的切片。

第2章 单一维度方向的任意顺序的切片:tf.gather(a, axis,indices)

2.1 源张量是一维数据

# 代码示例

# 数据元素限制
print("源张量")
a = tf.constant([0,1,2,3,4,5,6,7,8,9])
print(a)

b = tf.gather(a, indices=[0,2,4,6,8,1,3,5,7,9])
print(b)

b = tf.gather(a, indices=[0,2,4])
print(b)

输出:

源张量
tf.Tensor([0 1 2 3 4 5 6 7 8 9], shape=(10,), dtype=int32)
tf.Tensor([0 2 4 6 8 1 3 5 7 9], shape=(10,), dtype=int32)
tf.Tensor([0 2 4], shape=(3,), dtype=int32)

2.2 源张量是二维数据

#代码示例:

print("原张量")
a = tf.constant([[0,1,2,3,4],[5,6,7,8,9]])
print(a)

print("\n过滤之后张量(axis=0):")
b = tf.gather(a, axis=0, indices=[0]) #只能过滤一个维度方向
print(b)

print("\n过滤之后张量(axis=1):") 
b = tf.gather(a, axis=1, indices=[1,3]) #只能过滤一个维度方向
print(b)

输出:

原张量
tf.Tensor(
[[0 1 2 3 4]
 [5 6 7 8 9]], shape=(2, 5), dtype=int32)

过滤之后张量(axis=0):
tf.Tensor([[0 1 2 3 4]], shape=(1, 5), dtype=int32)

过滤之后张量(axis=1):
tf.Tensor(
[[1 3]
 [6 8]], shape=(2, 2), dtype=int32)

第3章 全维度方向的任意顺序的切片:gather_nd(a, indices)

3.1 任意取一个元素,构建一个一维张量

# 代码示例

print("原张量")
a = tf.constant([[0,1,2,3,4],[5,6,7,8,9]])
print(a)

print("\n从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[1,3]] ")
b = tf.gather_nd(a, [1,3])
print(b)

输出:

原张量
tf.Tensor(
[[0 1 2 3 4]
 [5 6 7 8 9]], shape=(2, 5), dtype=int32)

从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[1,3]] 
tf.Tensor(8, shape=(), dtype=int32)

3.2 任意取多个元素,构建一个一维张量

# 代码示例:

print("原张量")
a = tf.constant([[0,1,2,3,4],[5,6,7,8,9]])
print(a)

print("\n从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[1,2], a[1,3], a[0,4]] ")
b = tf.gather_nd(a, [[1,2],[1,3],[0,4]])
print(b)

输出:

原张量
tf.Tensor(
[[0 1 2 3 4]
 [5 6 7 8 9]], shape=(2, 5), dtype=int32)

从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[1,2], a[1,3], a[0,4]] 
tf.Tensor([7 8 4], shape=(3,), dtype=int32)

3.3 任意取一个维度的所有数据,构建一个一维张量

# 代码示例

print("原张量")
a = tf.constant([[0,1,2,3,4],[5,6,7,8,9]])
print(a)

print("\n从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[0,0],a[0,1],[0,2].......] ")
b = tf.gather_nd(a, [0]) 
print(b)

输出:

原张量
tf.Tensor(
[[0 1 2 3 4]
 [5 6 7 8 9]], shape=(2, 5), dtype=int32)

从2维的原张量数据中构建一个新的1维张量[], 所有元素为:[a[0,0],a[0,1],[0,2].......] 
tf.Tensor([0 1 2 3 4], shape=(5,), dtype=int32)

3.4 任意取多个元素,构建一个二维张量

# 代码示例:

print("原张量")
a = tf.constant([[0,1,2,3,4],[5,6,7,8,9]])
print(a)

print("\n从2维的原张量数据中构建一个新的2维张量[], 所有元素为:[[a[1,2],a[1,3],a[0,4]], [a[0,1],a[0,3],a[1,4]]] ")
b = tf.gather_nd(a, [[[1,2],[1,3],[0,4]],[[0,1],[0,3],[1,4]]])
print(b)

print("\n从2维的原张量数据中构建一个新的2维张量[], 所有元素为:[[a[1,2],a[0,1]], [a[1,3],a[0,3]], [a[0,4],a[1,4]] ")
b = tf.gather_nd(a, [[[1,2],[0,1]], [[1,3],[0,3]], [[0,4],[1,4]]])
print(b)

输出:

原张量
tf.Tensor(
[[0 1 2 3 4]
 [5 6 7 8 9]], shape=(2, 5), dtype=int32)

从2维的原张量数据中构建一个新的2维张量[], 所有元素为:[[a[1,2],a[1,3],a[0,4]], [a[0,1],a[0,3],a[1,4]]] 
tf.Tensor(
[[7 8 4]
 [1 3 9]], shape=(2, 3), dtype=int32)

从2维的原张量数据中构建一个新的2维张量[], 所有元素为:[[a[1,2],a[0,1]], [a[1,3],a[0,3]], [a[0,4],a[1,4]] 
tf.Tensor(
[[7 1]
 [8 3]
 [4 9]], shape=(3, 2), dtype=int32)

解读:


 

作者主页(文火冰糖的硅基工坊):https://blog.csdn.net/HiWangWenBing

本文网址:https://blog.csdn.net/HiWangWenBing/article/details/119651022

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

文火冰糖的硅基工坊

你的鼓励是我前进的动力

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

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

打赏作者

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

抵扣说明:

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

余额充值