MachineLearning入门—第6章—TensorFlow的低阶API简介

TensorFlow的低阶API主要包括张量操作,计算图和⾃动微分。
如果把模型⽐作⼀个房⼦,那么低阶API就是【模型之砖】。
在低阶API层次上,可以把TensorFlow当做⼀个增强版的numpy来使⽤。
TensorFlow提供的⽅法⽐numpy更全⾯,运算速度更快,如果需要的话,还可以使⽤GPU进⾏加速。

张量的结构操作:
张量的操作主要包括张量的结构操作 和 张量的数学运算
张量结构操作诸如:张量创建,索引切⽚,维度变换,合并分割。
张量数学运算主要有:标量运算,向量运算,矩阵运算。另外我们会介绍张量运算的⼴播机制。
Autograph计算图我们将介绍使⽤Autograph的规范建议,Autograph的机制原理,
Autograph和tf.Module

张量创建:


    a = tf.constant([1, 2, 3], dtype=tf.float32)
    tf.print(a)
    tf.print(a.shape)
    print(str(a.ndim) + "阶张量")

    b = tf.constant([[1, 2, 3], [1, 2, 3]], dtype=tf.float32)
    tf.print(b)
    tf.print(b.shape)
    print(str(b.ndim) + "阶张量")

    c = tf.constant([[[1, 2, 3], [1, 2, 3]], [[1, 2, 3], [1, 2, 3]]], dtype=tf.float32)
    tf.print(c)
    tf.print(c.shape)
    print(str(c.ndim) + "阶张量")

索引切片:
张量的索引切⽚⽅式和numpy⼏乎是⼀样的。切⽚时⽀持缺省参数和省略号。

    tf.random.set_seed(3)
    t = tf.random.uniform([5, 5], minval=0, maxval=10, dtype=tf.int32)
    tf.print(t)

    tf.print(t[0])

    tf.print(t[-1])

    tf.print(t[1, 3])
    tf.print(t[1][3])

    print("第1⾏⾄第3⾏")
    tf.print(t[1:4, :])
    tf.print(tf.slice(t, [1, 0], [3, 5]))

    print("前四行,前两列")
    tf.print(t[0:4, 0:2])

维度变换:
维度变换相关函数主要有 tf.reshape, tf.squeeze, tf.expand_dims, tf.transpose.

tf.reshape 可以改变张量的形状。
tf.squeeze 可以减少维度。
tf.expand_dims 可以增加维度。
tf.transpose 可以交换维度。

    a = tf.random.uniform(shape=[1, 3, 3, 2], minval=0, maxval=255, dtype=tf.int32)
    tf.print(a.shape)
    tf.print(a)

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

    c = tf.reshape(b, [1, 3, 3, 2])
    tf.print(c)

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

    a1 = tf.random.uniform(shape=[100, 600, 600, 4], minval=0, maxval=255, dtype=tf.int32)
    tf.print(a1.shape)

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

合并切割:
和numpy类似,可以⽤tf.concat和tf.stack⽅法对多个张量进⾏合并,可以⽤tf.split⽅法把⼀个张量分割成多个张量。
tf.concat和tf.stack有略微的区别,tf.concat是连接,不会增加维度,⽽tf.stack是堆叠,会增加维度。

 def tensor_merge():
        a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
        b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
        c = tf.constant([[9.0, 10.0], [11.0, 12.0]])

        concat_1 = tf.concat([a, b, c], axis=0)
        tf.print(concat_1)

        concat_2 = tf.concat([a, b, c], axis=1)
        tf.print(concat_2)

        stack_0 = tf.stack([a, b, c])
        tf.print(stack_0)

        stack_1 = tf.stack([a, b, c], axis=1)
        tf.print(stack_1)
def tensor_marge():
    a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
    b = tf.constant([[5.0, 6.0], [7.0, 8.0]])
    c = tf.constant([[9.0, 10.0], [11.0, 12.0]])
    c = tf.concat([a, b, c], axis=0)
    tf.print(c)

    split_0 = tf.split(c, 3, axis=0)
    print(type(split_0))
    for team in split_0:
        tf.print(team)

    split_1 = tf.split(c, [4, 2], axis=0)
    for team in split_1:
        tf.print(team)

标量运算
标量运算符的特点是对张量实施逐元素运算。
有些标量运算符对常⽤的数学运算符进⾏了重载。并且⽀持类似numpy的⼴播特性
许多标量的运算符都在tf.math模块下

a = tf.constant([[1.0, 2], [-3, 4.0]])
b = tf.constant([[5.0, 6], [7.0, 8.0]])
print(a + b)
print(tf.math.add(a, b))

向量运算 :
向量运算符只在⼀个特定轴上运算,将⼀个向量映射到⼀个标量或者另外⼀个向量。
许多向量运算符都以reduce开头

 a = tf.range(1, 10)
    tf.print(a)
    tf.print(tf.reduce_sum(a))
    tf.print(tf.reduce_mean(a))
    tf.print(tf.reduce_max(a))
    tf.print(tf.reduce_min(a))
    tf.print(tf.reduce_prod(a))

    b = tf.reshape(a, (3, 3))
    tf.print(tf.reduce_sum(b, axis=1, keepdims=True))
    tf.print(tf.reduce_sum(b, axis=0, keepdims=True))

    p = tf.constant([True, False, False])
    q = tf.constant([False, False, True])
    tf.print(tf.reduce_all(p))
    tf.print(tf.reduce_any(q))

    tf.print(tf.math.cumsum(a))
    tf.print(tf.math.cumprod(a))

    tf.print(tf.argmax(a))
    tf.print(tf.argmin(a))

矩阵运算:
矩阵运算包括:矩阵乘法,矩阵转置,矩阵逆,矩阵求迹,矩阵范数,矩阵⾏列式,矩阵求特征值,矩阵分解等运算。除了⼀些常⽤的运算外,⼤部分和矩阵有关的运算都在tf.linalg⼦包中。

a = tf.constant([[1, 2], [3, 4]])
b = tf.constant([[2, 0], [0, 2]])
tf.print(a @ b)

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

a1 = tf.constant([[1.0, 2], [3.0, 4]], dtype=tf.float32)
inv = tf.linalg.inv(a1)
tf.print(inv)

norm = tf.linalg.norm(a1)
tf.print(norm)

det = tf.linalg.det(a1)
tf.print(det)

eigvalsh = tf.linalg.eigvalsh(a1)
tf.print(eigvalsh)

a2 = tf.constant([[1.0, 2], [3, 4]])
tf.linalg.trace(a2)

a3 = tf.constant([[1.0, 2.0], [3.0, 4.0]], dtype=tf.float32)
q, r = tf.linalg.qr(a3)
tf.print(q)
tf.print(r)
tf.print(q @ r)

v, s, d = tf.linalg.svd(a3)
matmul = tf.matmul(tf.matmul(s, tf.linalg.diag(v)), d)
tf.print(matmul)

⼴播机制:
TensorFlow的⼴播规则和numpy是⼀样的:
1、如果张量的维度不同,将维度较⼩的张量进⾏扩展,直到两个张量的维度都⼀样。
2、如果两个张量在某个维度上的⻓度是相同的,或者其中⼀个张量在该维度上的⻓度为1,那么
我们就说这两个张量在该维度上是相容的。
3、如果两个张量在所有维度上都是相容的,它们就能使⽤⼴播。
4、⼴播之后,每个维度的⻓度将取两个张量在该维度⻓度的较⼤值。
5、在任何⼀个维度上,如果⼀个张量的⻓度为1,另⼀个张量⻓度⼤于1,那么在该维度上,就好
像是对第⼀个张量进⾏了复制

a = tf.constant([1, 2, 3])
b = tf.constant([[0, 0, 0], [1, 1, 1], [2, 2, 2]])

tf.print(tf.broadcast_to(a, b.shape))
tf.print(b + a)

shape = tf.broadcast_static_shape(a.shape, b.shape)
tf.print(shape)

c = tf.constant([1,2,3])
d = tf.constant([[1],[2],[3]])
dynamic_shape = tf.broadcast_dynamic_shape(tf.shape(c), tf.shape(d))
tf.print(dynamic_shape)

AutoGraph的使⽤规范:
有三种计算图的构建⽅式:静态计算图,动态计算图,以及Autograph。
TensorFlow 2.0主要使⽤的是动态计算图和Autograph。
动态计算图易于调试,编码效率较⾼,但执⾏效率偏低。
静态计算图执⾏效率很⾼,但较难调试。
⽽Autograph机制可以将动态图转换成静态计算图,兼收执⾏效率和编码效率之利。
当然Autograph机制能够转换的代码并不是没有任何约束的,有⼀些编码规范需要遵循,否则可能会转
换失败或者不符合预期。

Autograph编码规范总结:
1,被@tf.function修饰的函数应尽可能使⽤TensorFlow中的函数⽽不是Python中的其他函数。例
如使⽤tf.print⽽不是print,使⽤tf.range⽽不是range,使⽤tf.constant(True)⽽不是True.

2,避免在@tf.function修饰的函数内部定义tf.Variable.

3,被@tf.function修饰的函数不可修改该函数外部的Python列表或字典等数据结构变量

Original: https://blog.csdn.net/xy3233/article/details/121754662
Author: Fuly1024
Title: MachineLearning入门—第6章—TensorFlow的低阶API简介

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/761142/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球