天天看点

TensorFlow 基础 (01)

tensorflow 基础数据类型, 数值, 字符串, 布尔等, 核心是认识 张量 tensor, 包括 标量, 向量, 矩阵 .. 和理解属性, 维度, dim, 形状 shap 的实际意义.

以前都自嘲什么码农, 搬砖啥的, 倒不如 "工具人" 这个词更加贴切. 我现在就是一个完完全全的工具人. 上班真的是没有太大乐趣, 如果不下班后培养自己的兴趣爱好, 或者技术精进的话, 那确实很快就变成社畜了, 虽然谈什么, 遭受社会的毒打啥的, 有些过分, 但确实, 这种日子也不算太友好. 要么就很容易消磨斗志, 甘于平庸, 要么就自怨自艾, 归根到底, 其实就是对于自己庸碌而不甘的无奈感...

不扯了...

数据类型

跟其他的编程一样, 也将 TensorFlow 看做一门 "编程语言" 来看待, 这样就能很好理解所谓的数据类型了. 即经常谈及的, 数值类型, 字符串, 布尔类型等.

数值类型

张量 (tensor) 是 tensorflow 的主要数据载体, 根据 维度 Dimension 可划分为:

  • 标量 (Scalar)单个的实数, 如 1, 2, 3, 4 等, 其 维度 Dimension 为 0, shape 为 [ ]
  • 向量 (Vector)n 个实数的有序集合, 通过中括号包裹, Dimension 为 1, shape 为 [ n ]
  • 矩阵 (Matrix)n 行 m 列 实数的集合, 如 [ [1,2], [3, 4] ], 维数为2, 长度不定, shape 为 [n, m ]
#  矩阵,有行, 列 
| 1, 2|
| 3, 4|      
  • 张量 (Tensor)即所有维度数 dim > 2 的数组都通常为 张量. 张量的每个维度, 也称作 **轴 (Axis) **.

一般维度代表了具体的物理意义. 比如 shape 为 [ 1, 2, 3, 4 ] 的张量共有 4 维, 如果表示图的话, 每个 维度 / 轴 分别表示图片的 数量, 高度, 宽度, 通道数(RGB) .

彩色图片的 shape 为 [ 2, 32, 32, 3] 表示 有 2张图片, 宽高都是32, 有3个通道(RGB)

一般将标量, 向量, 矩阵等也统称的 张量, 不做区分, 维度要自己来判断, 来看一波是如何创建的.

import tensorflow as tf 

#  创建标量

# Python 的方式直接整
num = 666

tf_num = tf.constant(6.66) 

print(type(num), type(tf_num), tf.is_tensor(tf_num))      
<class 'int'> <class 'tensorflow.python.framework.ops.EagerTensor'> True      

通过 print 的方式, 可以打印出 张量的 具体相关信息.

arr_01 = tf.constant([1, 2, 3, 6.66])
arr_02 = tf.constant([[1,2,3], [4,5,6.66]])

print(arr_01)
print(arr_02)      
tf.Tensor([1.   2.   3.   6.66], shape=(4,), dtype=float32)

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

非常直观哈, shape 表示 张量的形状, 还是要注意区分向量是 (n, ) 的 一维哦, dtype 表示数值精度, 张量对象的 numpy 方法可以返回Numpy.array 的类型数据, 就是为了, 方便导出和使用吧.

arr_02.numpy()      
array([[1.  , 2.  , 3.  ],
       [4.  , 5.  , 6.66]], dtype=float32)      

与标量不同在于, 向量的定义, 须通过 List 容器来传给 tf.constant( ).

tf_arr = tf.constant([1, 2, 3])

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

同样, 定义矩阵, 传入一个, 二维数组即可呀.

tf_matrix = tf.constant([[1,2,3], [4,5,6]])

print(tf_matrix)
print(tf_matrix.shape) # [row, col]      
tf.Tensor(
[[1 2 3]
 [4 5 6]], shape=(2, 3), dtype=int32)

 # 2 行 3列 的 shape
(2, 3)      

同样, 3维的张量, 也是同样的定义哦.

# 3 维张量
tf_3D = tf.constant(
    [
        [
            [1, 2],
            [3, 4],
            [5, 6],
            [7, 8]
        ]
    ]
)

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

字符串类型

我觉得字符串是编程中主要的处理对象吧, 但只有在数据科学这块, 数值才是成为了主要的处理对象,诸如业务数据, 图片数据, 文本向量化等, 但字符串, 始终还是占据非常, 非常重要的地位啊, 在编程上, 不过在 深度学习这块, 似乎就没那么重要了哦.

tf_str = tf.constant("Hello,world!")

print(tf_str)      
tf.Tensor(b'Hello,world!', shape=(), dtype=string)      

在 tf.strings 模块中, 提供了常见的字符串类型的工具函数, 如 大小写转换 lower() .., 拼接 join( ), 分割 split( ) 长度 length ( ) 等.

#  字符串 - 基本处理
print(tf.strings.lower(tf_str))
print(tf.strings.length(tf_str))
print(tf.strings.split(tf_str))      
tf.Tensor(b'hello,world!', shape=(), dtype=string)
tf.Tensor(12, shape=(), dtype=int32)
tf.Tensor([b'Hello,world!'], shape=(1,), dtype=string      

不整了, 就这样吧, 深度学习中, 还是张量的天下, 字符串这块, 使用上就比较低了呀, 不展开了.

布尔类型

在创建布尔类型标量时, 只需要将 Python 的 True 和 False 传递给 tf.constant( ) 即可. 但值得注意的是,tf 的布尔 和 Pyhton 的布尔是不等价(对象)的哦.

tf_true = tf.constant(True)
tf_bool_lst = tf.constant([True, False, False])

print(tf_true)
print(tf_bool_lst)
print(tf_true) is True      
tf.Tensor(True, shape=(), dtype=bool)
tf.Tensor([ True False False], shape=(3,), dtype=bool)

False      

小结

  • tensorflow 的基本数据类型, 数值, 字符串, 布尔值等, 数值这块是主要探讨的内容.
  • 重点是对 张量 tensor 有直观认识, 包括 标量, 向量, 矩阵, 张量.. 理解 维度 dim 和 形状 shape 的意义
  • 结合 numpy 数组来理解张量, 就会好很多, 这些知识果然都是相关联的, 在数据科学这块