第五章 TensorFlow进阶(二)

目录:
张量比较、填充与复制、数据限幅
'tf.gather、 tf.where(cond, a, b)、tf.scatter_nd(indices, updates, shape)、tf.meshgrid

"""第五章 TensorFlow进阶(二)"""
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib
from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
'''5.3张量比较'''
# out = tf.random.normal([100, 10])
# out = tf.nn.softmax(out, axis=1)    # 输出转化为概率
# pred = tf.argmax(out, axis=1)   # 计算预测值
# print(pred)
# y = tf.random.uniform([100], dtype=tf.int64, maxval=10)  # 模型生成真实标签
# print(y)
# out = tf.math.equal(pred, y)
# print(out)
# out = tf.cast(out, dtype=tf.float64)    # 布尔型转化为浮点型数据
# correct = tf.reduce_sum(out)
# print(correct)
"""
tf.math.greater 𝑎 > 𝑏 
tf.math.less 𝑎 < 𝑏 
tf.math.greater_equal 𝑎 ≥ 𝑏 
tf.math.less_equal 𝑎 ≤ 𝑏 
tf.math.not_equal 𝑎 ≠ 𝑏 
tf.math.is_nan 𝑎 = nan
"""
'''5.4填充与复制'''
'''5.4.1填充'''
'''
填充操作可以通过 tf.pad(x, paddings)函数实现,参数 paddings 是包含了多个
[Left Padding,Right Padding]的嵌套方案 List,
'''
# a = tf.constant([1, 2, 3, 4, 5, 6])
# b = tf.constant([7, 8, 1, 6])
# b = tf.pad(b, paddings=[[0, 2]])
# print(b)
# c = tf.stack([a, b], axis=0)
# print(c)
'''
以 IMDB 数据集的加载为例,我们来演示如何将不等长的句子变换为等长结构,代码如下:
'''
# total_words = 10000  # 设定词汇量大小
# max_review_len = 80  # 最大句子长度
# embedding_len = 100  # 词向量长度
# # 加载 IMDB 数据集
# (x_train, y_train), (x_test, y_test) = keras.datasets.imdb.load_data(num_words=total_words)
# # 将句子填充或截断到相同长度,设置为末尾填充和末尾截断方式
# x_train = keras.preprocessing.sequence.pad_sequences(x_train, maxlen=max_review_len,
#                                                      truncating='post', padding='post')
# x_test = keras.preprocessing.sequence.pad_sequences(x_test, maxlen=max_review_len, truncating='post', padding='post')
# print(x_train.shape, x_test.shape)  # 打印等长的
'''
以28 × 28大小的图片数据为例,如果网络层所接受的数据高宽为32 × 32,则必须将28 × 28
大小填充到32 × 32,可以选择在图片矩阵的上、下、左、右方向各填充 2 个单元
'''
# x = tf.random.normal([4, 28, 28, 1])
# x = tf.pad(x, [[0, 0], [2, 2], [2, 2], [0, 0]])
# print(x.shape)
'''5.4.2复制'''
'''
通过 tf.tile 函数可以在任意维度将数据重复复制多份,如 shape 为[4,32,32,3]的数据,
复制方案为 multiples=[2,3,3,1],即通道数据不复制,高和宽方向分别复制 2 份,图片数再
复制 1 份,实现如下:'''
# x = tf.random.normal([4, 32, 32, 3])
# x = tf.tile(x, multiples=[2, 3, 3, 1])
# print(x)
'''5.5数据限幅'''
'''通过简单的数据限幅运算实现,限制元素的范围𝑥 ∈ [0, +∞)。
在 TensorFlow 中,可以通过 tf.maximum(x, a)实现数据的下限幅,即𝑥 ∈ [𝑎, +∞);可
以通过 tf.minimum(x, a)实现数据的上限幅,即𝑥 ∈ (−∞,𝑎],
基于 tf.maximum 函数,我们可以实现 ReLU 函数
'''
# x = tf.range(9)
# # x = tf.maximum(x, 0)    # 下限幅到2
# # x = tf.minimum(x, 7)    # 上限到7
# x = tf.minimum(tf.maximum(x, 0), 7)    # 上限到7
# print(x)
# x = tf.range(9)
# x = tf.clip_by_value(x, 2, 7)   # 实现上下限幅
# print(x)
'''5.6高级操作'''
# tf.gather 可以实现根据索引号收集数据的目的。
# x = tf.random.uniform([4, 35, 8], maxval=100, dtype=tf.int32)
# # y = tf.gather(x, [0, 1], axis=0)
# # y = tf.split([:2])
# # y = tf.gather(x, [1, 4, 9, 12, 13, 27], axis=1)     # 收集第 1,4,9,12,13,27 号同学成绩
# # y = tf.gather(x, [2,4], axis=2)     # 第 3,5 科目的成绩
# # y = tf.gather(tf.gather(x, [2, 3], axis=0), [3, 4, 6, 27], axis=1)      # 第[2,3]班级的第[3,4,6,27]号同学的科目成绩
# # y = tf.gather(tf.gather(x, [1, 2, 3], axis=0), [])
# # y = tf.gather_nd(x, [[1, 1],  [2, 2], [3, 3]])  # 根据多维坐标收集数据
# # y = tf.gather_nd(x, [[1, 1, 2], [2, 2, 3]])
# y = tf.boolean_mask(x, mask=[True, False, False, True], axis=0)     # 根据掩码方式采样班级,给出掩码和维度索引
# print(y)
# x = tf.random.uniform([2, 3, 8], maxval=100, dtype=tf.int32)
# # y = tf.gather_nd(x, [[0, 0], [0, 1], [1, 1], [1, 2]])
# y = tf.boolean_mask(x, mask=[[True, True, False], [False, True, True]])
# print(y)

# tf.where(cond, a, b)操作可以根据 cond 条件的真假从参数𝑨或𝑩中读取数据
# a = tf.ones([3, 3])
# b = tf.zeros([3, 3])
# print(a, b)
# conda = tf.constant([[True, False, False], [False, True, False], [True, True, False]])
# y = tf.where(condition=conda, x=a, y=b)
# print(tf.where(conda))

# x = tf.random.normal([3, 3])  # 构造 a
# mask = x > 0  # 比较操作,等同于 tf.math.gre
# y = tf.boolean_mask(x, mask)  # 通过掩码提取正数的元素值
# print(y)

# 通过 tf.scatter_nd(indices, updates, shape)函数可以高效地刷新张量的部分数
# 构建需要刷新的数据索引号
# indices = tf.constant([[4],[3],[1],[7]])
# updates = tf.constant([4.4,3.3,1.1,7.7])
# y = tf.scatter_nd(indices=indices, updates=updates, shape=[8])
# print(y)

# indices = tf.constant([[1], [3]])    # 构造写入数据,即 2 个矩阵
# updates = tf.constant([[[5,5,5,5],[6,6,6,6],[7,7,7,7],[8,8,8,8]], [[1,1,1,1],[2,2,2,2],[3,3,3,3],[4,4,4,4]]])
# y = tf.scatter_nd(indices,updates,[4,4,4])
# print(y)

# 通过 tf.meshgrid 函数可以方便地生成二维网格的采样点坐标
# x = tf.linspace(-8, 8, 100)
# y = tf.linspace(-8, 8, 100)
# x, y = tf.meshgrid(x, y)    # 生成网格点,并内部拆分后返回
# print(x.shape, y.shape)
# # print(x)
# z = tf.sqrt(x**2+y**2)
# z = tf.sin(z)/z
#
# fig = plt.figure()
# ax = Axes3D(fig)
# ax.contour3D(x.numpy(), y.numpy(), z.numpy(), 50)
# plt.show()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值