TensorFlow2基本操作之合并分割与统计
作者:我是小白呀 发布时间:2022-01-01 21:47:39
合并与分割
tf.concat
tf.concat
可以帮助我们实现拼接操作.
格式:
tf.concat(
values, axis, name='concat'
)
参数:
values: 一个 tensor 或 tensor list
axis: 操作的维度
name: 数据名称, 默认为 “concat”
例子:
part_1 = tf.zeros([5, 3])
print(part_1)
part_2 = tf.ones([5, 3])
print(part_2)
# 竖向拼接
result_1 = tf.concat([part_1, part_2], axis=0)
print(result_1)
# 横向拼接
result_2 = tf.concat([part_1, part_2], axis=1)
print(result_2)
输出结果:
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(10, 3), dtype=float32)
tf.Tensor(
[[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]
[0. 0. 0. 1. 1. 1.]], shape=(5, 6), dtype=float32)
tf.stack
rf.stack
可以创建一个新的维度来合并两个张量.
格式:
tf.stack(
values, axis=0, name='stack'
)
参数:
values: 一个 tensor list
axis: 操作的维度
name: 数据名称, 默认为 “stack”
例子:
part_1 = tf.zeros([5, 3])
print(part_1)
part_2 = tf.ones([5, 3])
print(part_2)
# 头拼接
result_1 = tf.stack([part_1, part_2], axis=0)
print(result_1)
# 尾拼接
result_2 = tf.stack([part_1, part_2], axis=2)
print(result_2)
输出结果:
tf.Tensor(
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]], shape=(5, 3), dtype=float32)
tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]][[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
tf.Tensor(
[[[0. 1.]
[0. 1.]
[0. 1.]][[0. 1.]
[0. 1.]
[0. 1.]][[0. 1.]
[0. 1.]
[0. 1.]][[0. 1.]
[0. 1.]
[0. 1.]][[0. 1.]
[0. 1.]
[0. 1.]]], shape=(5, 3, 2), dtype=float32)
tf.unstack
tf.unstack
是一个矩阵分解函数.
格式:
# unstack
tf.unstack(
value, num=None, axis=0, name='unstack'
)
参数:
values: 一个 tensor, 维度大于 0
num: 轴的长度
axis: 操作的维度
name: 数据名称, 默认为 “unstack”
例子:
a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)
b = tf.unstack(a, axis=0)
print(b)
输出结果:
tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]][[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
[<tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>, <tf.Tensor: shape=(5, 3), dtype=float32, numpy=
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=float32)>]
tf.split
tf.split()
可以把一个张量划分为几个子张量.
格式:
tf.split(
value, num_or_size_splits, axis=0, num=None, name='split'
)
参数:
value: 待切分的张量
num_or_size_splits: 切成几份
axis: 操作的维度
num: num_or_size_splits 不能实现的情况下使用
name: 数据名称, 默认为 “split”
例子:
# split
a = tf.stack([tf.zeros([5, 3]), tf.ones([5, 3])], axis=0)
print(a)
b = tf.split(a, 2)
print(b)
输出结果:
tf.Tensor(
[[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]][[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]], shape=(2, 5, 3), dtype=float32)
[<tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
array([[[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]]], dtype=float32)>, <tf.Tensor: shape=(1, 5, 3), dtype=float32, numpy=
array([[[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]]], dtype=float32)>]
数据统计
tf.norm
tf.norm
可以帮助我们计算向量, 矩阵, 张量的范数.
格式:
tf.norm(
tensor, ord='euclidean', axis=None, keepdims=None, name=None
)
参数:
tensor: 输入的张量
ord: 范数的顺序
axis: 操作的维度
keep_dims: 如果为 True, 则 axis 中指定的轴将保持为大小 1
name: 数据名称
例子:
a = tf.fill([2, 2], 2.0)
print(a)
# sqrt(2^2 * 4) = sqrt(16) = 4
b = tf.norm(a)
print(b)
# [2 + 2, 2 + 2] = [4, 4]
c = tf.norm(a, ord=1, axis= 0)
print(c)
# [sqrt(2^2 + 2^2), sqrt(2^2 + 2^2)] = [sqrt(8), sqrt(8)]
d = tf.norm(a, ord=2, axis= 0)
print(d)
输出结果:
tf.Tensor(
[[2. 2.]
[2. 2.]], shape=(2, 2), dtype=float32)
tf.Tensor(4.0, shape=(), dtype=float32)
tf.Tensor([4. 4.], shape=(2,), dtype=float32)
tf.Tensor([2.828427 2.828427], shape=(2,), dtype=float32)
reduce_min/max/mean
计算一个张量各个维度上元素的最小值 / 最大值 / 平均值.
格式:
tf.math.reduce_min / reduce_max / reduce_mean(
input_tensor, axis=None, keepdims=False, name=None
)
参数:
input_tensor: 传入的张量
axis: 维度, 默认计算所有维度
keepdims: 如果为真保留维度, 默认为 False
name: 数据名称
例子:
a = tf.reshape(tf.range(9), [3, 3])
print(a)
min = tf.reduce_min(a)
print(min)
max = tf.reduce_max(a)
print(max)
输出结果:
tf.Tensor(
[[0 1 2]
[3 4 5]
[6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor(0, shape=(), dtype=int32)
tf.Tensor(8, shape=(), dtype=int32)
argmax / argmin
tf.argmax
/tf.argmin
可以帮我们找到最大 / 最小值所在的索引 (index).
格式:
tf.math.argmax(
input, axis=None, output_type=tf.dtypes.int64, name=None
)
参数:
input: 输入
axis: 操作的维度
output_type: 输出数据类型, 默认为 int64
name: 数据名称
例子:
# argmax / argmin
a = tf.reshape(tf.range(9), [3, 3])
print(a)
max = tf.argmax(a)
print(max)
min = tf.argmin(a)
print(min)
输出结果:
tf.Tensor(
[[0 1 2]
[3 4 5]
[6 7 8]], shape=(3, 3), dtype=int32)
tf.Tensor([2 2 2], shape=(3,), dtype=int64)
tf.Tensor([0 0 0], shape=(3,), dtype=int64)
tf.equal
tf.equal
可以帮助我们判断两个张量是否相等. 返回 True / False.
格式:
tf.math.equal(
x, y, name=None
)
例子:
a = tf.zeros(5, dtype=tf.float32)
print(a)
b = tf.range(5, dtype=tf.float32)
print(b)
print(tf.equal(a, b))
输出结果:
tf.Tensor([0. 0. 0. 0. 0.], shape=(5,), dtype=float32)
tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)
tf.Tensor([ True False False False False], shape=(5,), dtype=bool)
tf.unique
tf.unique
可以帮我们找出张量中不重复的值
格式:
tf.unique(
x, out_idx=tf.dtypes.int32, name=None
)
参数:
input: 输入
output_type: 输出数据类型, 默认为 int32
name: 数据名称
例子:
a = tf.range(5)
print(tf.unique(a))
b = tf.constant([4, 2, 2, 4, 3])
print(tf.unique(b))
输出结果:
Unique(y=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>)
Unique(y=<tf.Tensor: shape=(3,), dtype=int32, numpy=array([4, 2, 3])>, idx=<tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 1, 0, 2])>)
来源:https://iamarookie.blog.csdn.net/article/details/117755839
猜你喜欢
- 本文实例讲述了PHP实现的DES加密解密类定义与用法。分享给大家供大家参考,具体如下:今天写App接口的时候需要传递加密数据给APP端,于是
- 说明C# 调用 Python 程序有多种方式,本篇用的是第 4 种:nuget的ironPython;用 c/c++ 调用python,再封
- 《色彩解答》系列之一 色彩层次《色彩解答》系列之二 色彩比例我们知道在设计中有很多对比,大小的对比,形状的对比,长短的对比,多少的对比,这些
- Python 在 2.2 版本中引入了descriptor(描述符)功能,也正是基于这个功能实现了新式类(new-styel class)的
- Python内建的filter()函数用于过滤序列。和map()类似,filter()也接收一个函数和一个序列。和map()不同的时,fil
- 如何选择速度最快的站点? <html><head><meta http-equiv=&qu
- 简介scrapy 是一个 python 下面功能丰富、使用快捷方便的爬虫框架。用 scrapy 可以快速的开发一个简单的爬虫,官方给出的一个
- 如何在第10000名来访者访问时显示中奖页面?看看下面的代码:< SCRIPT LANGUAGE=VBScript
- 前言Go的错误处理这块是日常被大家吐槽较多的地方,我在工作中也观察到一些现象,比较严重的是在各层级的逻辑代码中对错误的处理有些重复。比如,有
- 前言本文的文字及图片来源于网络,仅供学习、交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理。from math
- 前期准备首先,使用Python内置的Turtle绘图库需要在程序前添加以下代码:import turtle也可以写成这样:from turt
- Python 变量类型变量存储在内存中的值。这就意味着在创建变量时会在内存中开辟一个空间。基于变量的数据类型,解释器会分配指定内存,并决定什
- 需求说明:将单个或者多个Excel文件数据进行去重操作,去重的列可以通过自定义制定。开始源码说明之前,先说明一下工具的使用过程。1、准备需要
- 1、python多进程编程背景python中的多进程最大的好处就是充分利用多核cpu的资源,不像python中的多线程,受制于GIL的限制,
- 1、下载mysql-python官网地址:http://sourceforge.net/projects/mysql-python/2、安装
- 简介ICO是一种图标文件格式,图标文件可以存储单个图案、多尺寸、多色板的图标文件。一个图标实际上是多张不同格式的图片的集合体,并且还包含了一
- 前言静态文件是指 网站中的 js, css, 图片,视频等文件,本文主要给大家介绍了关于Django学习之静态文件调用的相关内容,分享出来供
- 显示图像: Image img = Image.From
- 小编曾经有过这样的经历,就是在安装使用django框架时候,遇到了部分模块不能够使用,检查了很久,才发现是因为版本问题,需要重新编译安装一个
- 好了,看看我们的代码吧:upload.htm' 上传页面<html> <body>&nb