一小时学会TensorFlow2之基本操作2实例代码


Posted in Python onSeptember 04, 2021

索引操作

一小时学会TensorFlow2之基本操作2实例代码

简单索引

索引 (index) 可以帮助我们快速的找到张量中的特定信息.

例子:

a = tf.reshape(tf.range(12), [2, 2, 3])
print(a)

print(a[0])
print(a[0][0])

输出结果:

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

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

Numpy 式索引

我们也可以按照 numpy 的写法来操作索引.

例子:

a = tf.reshape(tf.range(12), [2, 2, 3])
print(a)

print(a[0])
print(a[0, 0])

输出结果:

tf.Tensor(
[[[ 0 1 2]
[ 3 4 5]]

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

使用 : 进行索引

例子:

c = tf.ones([4, 14, 14, 4])
print(c[0, :, :, :].shape)
print(c[0, 1, :, :].shape)

输出结果:

(14, 14, 4)
(14, 4)

tf.gather

我们假设一个有 3 个餐馆, 每个餐馆有 8 种菜系, 128 道菜data: [resturants, cuisines, dishes].

一小时学会TensorFlow2之基本操作2实例代码

例子:

data = tf.zeros([3, 8, 128])

g1 = tf.gather(data, axis=0, indices=[0, 2])
print(g1.shape)

g2 = tf.gather(data, axis=1, indices=[0, 1, 2, 3])
print(g2.shape)

输出结果:

(2, 8, 128)
(3, 4, 128)

tf.gather_nd

例子:

g1 = tf.gather_nd(data, [0])
print(g1.shape)

g2 = tf.gather_nd(data, [0, 1])
print(g2.shape)

g3 = tf.gather_nd(data, [0, 1, 2])
print(g3.shape)

输出结果:

(8, 128)
(128,)
()

tf.boolean_mask

格式:

tf.boolean_mask(
    tensor, mask, axis=None, name='boolean_mask'
)

例子:

data = tf.zeros([3, 8, 128])

b1 = tf.boolean_mask(data, mask=[True, True, False])
print(b1.shape)

b2 = tf.boolean_mask(data, mask=[True, False, True, False, True, False, True, False], axis=1)
print(b2.shape)

输出结果:

(2, 8, 128)
(3, 4, 128)

切片操作

借助切片技术, 我们可以灵活的处理张量对象.

一小时学会TensorFlow2之基本操作2实例代码

简单切片

格式:

tensor[start : end]

其中 start 为开始索引, end 为结束索引 (不包括)

例子:

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

step 切片

格式:

tensor[start : end: step]

例子:

d = tf.range(6)
print(d[::-1])  # 实现倒序
print(d[::2])  # 步长为2

输出结果:

tf.Tensor([5 4 3 2 1 0], shape=(6,), dtype=int32)
tf.Tensor([0 2 4], shape=(3,), dtype=int32)

维度变换

一小时学会TensorFlow2之基本操作2实例代码

tf.reshape

tf.reshape 可以帮助我们进行维度转换.

格式:

tf.reshape(
    tensor, shape, name=None
)

参数:

  • tensor: 传入的张量
  • shape: 张量的形状
  • name: 数据名称

例子:

a = tf.random.normal([3, 8, 128])
print(a.shape)

b = tf.reshape(a, [3, 1024])
print(b.shape)

c = tf.reshape(a, [3, -1])
print(c.shape)

输出结果:

(3, 8, 128)
(3, 1024)
(3, 1024)

tf.transpose

格式:

tf.transpose(
    a, perm=None, conjugate=False, name='transpose'
)

例子:

a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.transpose(a)
print(b.shape)

c = tf.transpose(a, perm=[0, 1, 3, 2])
print(c.shape)

输出结果:

(4, 3, 2, 1)
(1, 2, 3, 4)
(4, 3, 1, 2)

tf.expand_dims

格式:

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

参数:

  • input: 输入
  • axis: 操作的维度
  • name: 数据名称

例子:

a = tf.random.normal([4, 3, 2, 1])
print(a.shape)

b = tf.expand_dims(a, axis=0)
print(b.shape)

c = tf.expand_dims(a, axis=1)
print(c.shape)

d = tf.expand_dims(a, axis=-1)
print(d.shape)

输出结果:

(4, 3, 2, 1)
(1, 4, 3, 2, 1)
(4, 1, 3, 2, 1)
(4, 3, 2, 1, 1)

tf.squeeze

tf.squeeze 可以帮助我们删去所有维度为1 的维度.

一小时学会TensorFlow2之基本操作2实例代码

格式:

tf.squeeze(
    input, axis=None, name=None
)

参数:

  • input: 输入
  • axis: 操作的维度
  • name: 数据名称

例子:

a = tf.zeros([2, 1, 1, 3, 5])

s1 = tf.squeeze(a)
print(s1.shape)

s2 = tf.squeeze(a, axis=1)
print(s2.shape)

s3 = tf.squeeze(a, axis=2)
print(s3.shape)

输出结果:

(2, 3, 5)
(2, 1, 3, 5)
(2, 1, 3, 5)

Boardcasting

广播机制 (Boardcasting) 是一种张量复制的手段. Boardcasting 可以帮助我们扩张张量的形状但无需实际复制数据.

一小时学会TensorFlow2之基本操作2实例代码

广播机制允许我们在隐式情况下进行填充, 从而使得我们的代码更加简洁, 更有效率地使用内存.

tf.boardcast_to

boardcast_to:

tf.broadcast_to(
    input, shape, name=None
)

参数:

  • input: 输入
  • shape: 数据形状
  • name: 数据名称

例子:

a = tf.broadcast_to(tf.random.normal([4, 1, 1, 1]), [4, 32, 32, 3])
print(a.shape)

b = tf.broadcast_to(tf.zeros([128, 1, 1, 1]), [128, 32, 32, 3])
print(b.shape)

输出结果:

(4, 32, 32, 3)
(128, 32, 32, 3)

tf.tile

格式:

tf.tile(
    input, multiples, name=None
)

参数:

  • input: 输入
  • multiples: 同一纬度上复制的次数
  • name: 数据名称

例子:

a = tf.zeros([4, 1, 1, 1])
print(a.shape)

b = tf.tile(a, [1, 32, 32, 3])
print(b.shape)

输出结果:

(4, 1, 1, 1)
(4, 32, 32, 3)

注: boardcast_to 和 tile 的区别在于 boardcast_to 可以在不复制内存的情况下自动扩张 tensor.

数学运算

一小时学会TensorFlow2之基本操作2实例代码

加减乘除

例子:

# 定义张量
t1 = tf.ones([3, 3])
t2 = tf.fill([3, 3], 3.0)

# 加
add = t1 + t2
print(add)

# 减
minus = t1 - t2
print(minus)

# 乘
multiply = t1 * t2
print(multiply)

# 除
divide = t1 / t2
print(divide)

输出结果:

tf.Tensor(
[[4. 4. 4.]
[4. 4. 4.]
[4. 4. 4.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[-2. -2. -2.]
[-2. -2. -2.]
[-2. -2. -2.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[3. 3. 3.]
[3. 3. 3.]
[3. 3. 3.]], shape=(3, 3), dtype=float32)
tf.Tensor(
[[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]
[0.33333334 0.33333334 0.33333334]], shape=(3, 3), dtype=float32)

log & exp

例子:

# log
a = tf.fill([2], 100.0)
print(a)

b = tf.math.log(a)  # 以e为底
print(b)

# exp
c = tf.ones([2])
print(c)

d = tf.exp(c)
print(d)

输出结果:

tf.Tensor([100. 100.], shape=(2,), dtype=float32)
tf.Tensor([4.6051702 4.6051702], shape=(2,), dtype=float32)
tf.Tensor([1. 1.], shape=(2,), dtype=float32)
tf.Tensor([2.7182817 2.7182817], shape=(2,), dtype=float32)

pow & sqrt

例子:

# 定义张量
a = tf.fill([2], 4.0)
print(a)

# pow
b = tf.pow(a, 2)
print(b)

# sqrt
c = tf.sqrt(a, 2)
print(c)

输出结果:

tf.Tensor([4. 4.], shape=(2,), dtype=float32)
tf.Tensor([16. 16.], shape=(2,), dtype=float32)
tf.Tensor([2. 2.], shape=(2,), dtype=float32)

矩阵相乘 @

我们可以使用tf.matmul@来实现矩阵相乘.

一小时学会TensorFlow2之基本操作2实例代码

例子:

# 定义张量
a = tf.fill([2, 2], 2)
b = tf.fill([2, 2], 3)

# matmul
c = tf.matmul(a, b)
print(c)

# @
d = a@b
print(d)

输出结果:

tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)
tf.Tensor(
[[12 12]
[12 12]], shape=(2, 2), dtype=int32)

到此这篇关于一小时学会TensorFlow2之基本操作2实例代码的文章就介绍到这了,更多相关TensorFlow2基本操作内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Python 相关文章推荐
详解Python中的条件判断语句
May 14 Python
Python实现字符串的逆序 C++字符串逆序算法
May 28 Python
对numpy Array [: ,] 的取值方法详解
Jul 02 Python
python二维列表一维列表的互相转换实例
Jul 02 Python
Python2包含中文报错的解决方法
Jul 09 Python
详解Django解决ajax跨域访问问题
Aug 24 Python
win8.1安装Python 2.7版环境图文详解
Jul 01 Python
wxPython实现绘图小例子
Nov 19 Python
python 爬取古诗文存入mysql数据库的方法
Jan 08 Python
Ranorex通过Python将报告发送到邮箱的方法
Jan 12 Python
Django利用elasticsearch(搜索引擎)实现搜索功能
Nov 26 Python
Python基础之条件语句详解
Jun 16 Python
Python torch.flatten()函数案例详解
Aug 30 #Python
Python之基础函数案例详解
Aug 30 #Python
python中使用 unittest.TestCase单元测试的用例详解
Aug 30 #Python
python使用matplotlib绘制图片时x轴的刻度处理
使用Python+OpenCV进行卡类型及16位卡号数字的OCR功能
Aug 30 #Python
OpenCV绘制圆端矩形的示例代码
Aug 30 #Python
python中super()函数的理解与基本使用
You might like
ThinkPHP查询语句与关联查询用法实例
2014/11/01 PHP
php中实现可以返回多个值的函数实例
2015/03/21 PHP
学习php设计模式 php实现模板方法模式
2015/12/08 PHP
phplist及phpmailer(组合使用)通过gmail发送邮件的配置方法
2016/03/30 PHP
浅谈PHP安全防护之Web攻击
2017/01/03 PHP
广告代码静态化js通用函数
2007/05/09 Javascript
Prototype Number对象 学习
2009/07/19 Javascript
cnblogs 代码高亮显示后的代码复制问题解决实现代码
2011/12/14 Javascript
《JavaScript高级程序设计》阅读笔记(三) ECMAScript中的引用类型
2012/02/27 Javascript
js 判断js函数、变量是否存在的简单示例代码
2014/03/04 Javascript
jquery图片播放浏览插件prettyPhoto使用详解
2014/12/19 Javascript
使用JS画图之点、线、面
2015/01/12 Javascript
JavaScript汉诺塔问题解决方法
2015/04/21 Javascript
javascript生成不重复的随机数
2015/07/17 Javascript
javascript数组去重的六种方法汇总
2015/08/16 Javascript
AngularJS教程 ng-style 指令简单示例
2016/08/03 Javascript
基于JavaScript Array数组方法(新手必看篇)
2016/08/20 Javascript
微信开发之企业付款到银行卡接口开发的示例代码
2018/09/18 Javascript
nodejs更新package.json中的dependencies依赖到最新版本的方法
2018/10/10 NodeJs
JavaScript中关于base64的一些事
2019/05/06 Javascript
微信小程序 确认框的实现(附代码)
2019/07/23 Javascript
微信小程序 冒泡事件原理解析
2019/09/27 Javascript
对python cv2批量灰度图片并保存的实例讲解
2018/11/09 Python
Python使用mongodb保存爬取豆瓣电影的数据过程解析
2019/08/14 Python
Django中Aggregation聚合的基本使用方法
2020/07/09 Python
Python类型转换的魔术方法详解
2020/12/23 Python
HTML5中微数据概述及在搜索引擎中的使用举例
2013/02/07 HTML / CSS
KEEN美国官网:美国人气户外休闲鞋品牌
2021/03/09 全球购物
自荐信怎么写呢?
2013/12/09 职场文书
学生安全承诺书
2014/05/22 职场文书
以幸福为主题的活动方案
2014/08/22 职场文书
社会体育专业大学生职业生涯规划书
2014/09/17 职场文书
2014老师三严三实对照检查材料思想汇报
2014/09/18 职场文书
2015年艾滋病宣传活动总结
2015/03/27 职场文书
什么是执行力?9个故事告诉您:成功绝非偶然!
2019/07/05 职场文书
创业计划书之家政服务
2019/09/18 职场文书