• 【TensorFlow】带你搞懂 TensorFlow 中的张量数据结构


    系列文章目录

    第二章 TensorFlow 深度学习入门之 TensorFlow的核心概念


    目录

    系列文章目录

    前言

    一、常量的张量

    1 张量的数据类型与numpy.array的关系

    2 如何表示张量 Tensor

    3  如何改变张量的数据类型

    二、变量的张量



    前言

    程序 = 数据结构+算法

    TensorFlow程序 = 张量数据结构 + 计算图算法语言

    Tensorflow的基本数据结构是张量Tensor。张量即多维数组。Tensorflow的张量和numpy中的array很类似。

    从行为特性来看,有两种类型的张量,常量Constant和变量Variable.

    常量的值在计算图中不可以被重新赋值,变量可以在计算图中用assign或assign_add 重新赋值。

    话不多说,直接上代码,带你搞懂 TensorFlow 中的张量数据结构。


    一、常量的张量

    1 张量的数据类型与numpy.array的关系

    1. import numpy as np
    2. import tensorflow as tf
    3. # tf.int32 类型的常量
    4. a = tf.constant(1)
    5. print('tf.int32 类型的常量:',a)
    6. # tf.int64 类型的常量
    7. b = tf.constant(1, dtype=tf.int64)
    8. print('tf.int64 类型的常量:', b)
    9. # tf.float32 类型常量
    10. c = tf.constant(3.14)
    11. print('tf.float32 类型常量:', c)
    12. # tf.double 类型的常量
    13. d = tf.constant(3.14, dtype=tf.double)
    14. print('tf.double 类型的常量:', d)
    15. # tf.string 类型的常量
    16. e = tf.constant('hello world!')
    17. print('tf.string 类型的常量:', e)
    18. # tf.bool 类型的常量
    19. f = tf.constant(True)
    20. print('tf.bool 类型的常量:', f)

    输出结果:
    tf.int32 类型的常量: tf.Tensor(1, shape=(), dtype=int32)
    
    tf.int64 类型的常量: tf.Tensor(1, shape=(), dtype=int64)
    
    tf.float32 类型常量: tf.Tensor(3.14, shape=(), dtype=float32)
    
    tf.double 类型的常量: tf.Tensor(3.14, shape=(), dtype=float64)
    
    tf.string 类型的常量: tf.Tensor(b'hello world!', shape=(), dtype=string)
    
    tf.bool 类型的常量: tf.Tensor(True, shape=(), dtype=bool)
    1. # 查看张量的数据类型和numpy.array 是否一样
    2. print(tf.int64 == np.int64)
    3. print(tf.double == np.double)
    4. print(tf.double == np.float64)
    5. print(tf.bool == np.bool)
    6. print(tf.string == np.unicode)
    输出结果:
    True
    True
    True
    True
    False

    可以看见:

    module 'numpy' has no attribute 'string'
    tf.string 类型与 np.unicode 类型不一致,其他的类型还是很相似的

    2 如何表示张量 Tensor

    不同类型的数据可以用不同维度(rank)的张量来表示。

    标量为0维张量,向量为1维张量,矩阵为2维张量。

    彩色图像有rgb三个通道,可以表示为3维张量。

    视频还有时间维,可以表示为4维张量。

    可以简单地总结为:有几层中括号,就是多少维的张量。

    1. # 0 维张量
    2. scalar = tf.constant(2.1)
    3. print(tf.rank(scalar))
    4. # tf.rank() 的作用和 numpy的 ndim方法相同
    5. # print(scalar.numpy().ndim)
    6. print(scalar)
    7. print(np.ndim(scalar))
    8. # 1 维张量
    9. vector = tf.constant([1, 2, 3])
    10. print(tf.rank(vector))
    11. print(vector)
    12. print(np.ndim(vector))
    13. # 2 维张量
    14. matrix = tf.constant([[1, 2, 3], [4, 5, 6]])
    15. print(tf.rank(matrix).numpy())
    16. print(matrix)
    17. print(np.ndim(matrix))
    18. # 3维张量
    19. tensor3 = tf.constant([[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]])
    20. print(tf.rank(tensor3))
    21. print(tensor3)
    22. print(np.ndim(tensor3))
    23. # 4 维张量
    24. tensor4 = tf.constant([[[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]],
    25. [[[1, 2, 3], [2, 3, 4]],[[3, 4, 5], [4, 5, 6]]]])
    26. print(tensor4)
    27. print(tf.rank(tensor4))
    28. print(np.ndim(tensor4))

    输出结果:

    # 0 维张量

    tf.Tensor(0, shape=(), dtype=int32)
    tf.Tensor(2.1, shape=(), dtype=float32)
    0

    # 1 维张量

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

    # 2 维张量

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

    # 3维张量

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

    #4 维张量

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

    3  如何改变张量的数据类型

    • 可以用tf.cast() 改变张量的数据类型
    • 可以用numpy方法将tensorflow中的张量转化成numpy中的张量
    • 可以用shape方法查看张量的尺寸
    • 可以用 decode 将编码转为中文
    1. # 用tf.cast改变张量的数据类型
    2. tensor = tf.constant([1.1, 2.2], dtype=tf.float64)
    3. tensor_cast = tf.cast(tensor, tf.float32)
    4. print(tensor.dtype)
    5. print(tensor_cast.dtype)
    6. print(tensor.shape)

    输出结果:

    
    
    (2,)
    1. # 用numpy方法将tensorflow中的张量转化成numpy中的张量
    2. a = tf.constant([[1, 2], [3, 4]])
    3. print('tensorflow中的张量:', a.shape)
    4. print('numpy中的张量:',a.numpy())
    输出结果:
    tensorflow中的张量: (2, 2)
    numpy中的张量: [[1 2]
     [3 4]]
    
    1. # decode 可以将编码转为中文
    2. b = tf.constant(u"我喜欢中文!")
    3. print(b.numpy())
    4. print(b.numpy().decode('utf-8'))

    输出结果:

    b'\xe6\x88\x91\xe5\x96\x9c\xe6\xac\xa2\xe4\xb8\xad\xe6\x96\x87\xef\xbc\x81'
    我喜欢中文!

    二、变量的张量

    模型中需要被训练的参数一般被设置成变量。

    1. # 要注意 :常量值不可以改变,常量的重新赋值相当于创造新的内存空间
    2. c = tf.constant([1.1, 2.2])
    3. print(c)
    4. print(id(c))
    5. # 改变常量值
    6. c = c + tf.constant([0.9, 0.8])
    7. print(c)
    8. print(id(c))
    输出结果:
    tf.Tensor([1.1 2.2], shape=(2,), dtype=float32)
    2373561408680
    tf.Tensor([2. 3.], shape=(2,), dtype=float32)
    2373562947016
    
    1. # 变量的值可以改变,可以通过assignassign_add 等方法给变量重新赋值
    2. d = tf.Variable([1.1, 2.2])
    3. print(d)
    4. print(id(d))
    5. # 改变变量值,添加
    6. d.assign_add([0.9, 0.8])
    7. print(d)
    8. print(id(d))
    9. # 改变变量值,直接改变
    10. d.assign([1.14, 2.25])
    11. print(d)
    12. print(id(d))

    输出结果:

    
    2373563227776
    
    2373563227776
    
    2373563227776
  • 相关阅读:
    arcgis--二维建筑面的三维显示设置
    图像预处理工具_CogPolarUnwrapTool
    springboot+vue全栈开发【4.前端篇之Vue组件化开发】
    MySQL定时整库备份&滚动删除指定日期前的备份数据
    【Jetson Nano】jetson nano一些基本功能命令
    Conda Channel 介绍与配置
    程序员如何转型做产品经理-我的转型之路
    【Vue】vue-router页面跳转及传参:声明式导航/传参+编程式导航/传参
    深度优先与宽度优先搜索(python)
    [激光原理与应用-16]:《激光原理与技术》-2- 光的本质(粒子、波动说、电磁波、量子)
  • 原文地址:https://blog.csdn.net/m0_51816252/article/details/126789324