Tensorflow:TensorFlow基础(一)

   日期:2020-10-15     浏览:169    评论:0    
核心提示:文章目录TensorFlow基础1.数据类型1.1 数值类型1.2 字符串类型1.3 布尔类型2.数值精度2.1 读取精度2.2 类型转换3.待优化张量4.创建张量4.1 从数组、列表对象创建4.2 创建全0或全1张量4.3 创建自定义数值张量4.4 创建已知分布的张量4.4 创建序列TensorFlow基础import numpy as npimport tensorflow as tfimport tensorflow.keras as kerasimport tensorflow.keras

文章目录

  • TensorFlow基础
    • 1.数据类型
      • 1.1 数值类型
      • 1.2 字符串类型
      • 1.3 布尔类型
    • 2.数值精度
      • 2.1 读取精度
      • 2.2 类型转换
    • 3.待优化张量
    • 4.创建张量
      • 4.1 从数组、列表对象创建
      • 4.2 创建全0或全1张量
      • 4.3 创建自定义数值张量
      • 4.4 创建已知分布的张量
      • 4.4 创建序列

TensorFlow基础

import numpy as np
import tensorflow as tf
import tensorflow.keras as keras
import tensorflow.keras.layers as layers
physical_devices = tf.config.experimental.list_physical_devices('GPU')
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
tf.config.experimental.set_memory_growth(physical_devices[0], True)

1.数据类型

Tensorflow主要有3种数据类型:数值型,字符串型,布尔型

1.1 数值类型

标量(Scalar) 单个的实数,如 1.2, 3.4 等

向量(Vector) n 个实数的有序集合,通过中括号包裹,如[1.2],[1.2,3.4]等

矩阵(Matrix) n 行 m 列实数的有序集合,如[[1,2],[3,4]]

标量在 TensorFlow 是如何创建的

# python 语言方式创建标量
a = 1.2 
# TF 方式创建标量
aa = tf.constant(1.2)
type(a), type(aa), tf.is_tensor(aa)
(float, tensorflow.python.framework.ops.EagerTensor, True)

如果要使用 TensorFlow 提供的功能函数, 须通过 TensorFlow 规定的方式去创建张量,而不能使用 Python 语言的标准变量创建方式。

x = tf.constant([1,2.,3.3])
# 打印 TF 张量的相关信息 
x
<tf.Tensor: shape=(3,), dtype=float32, numpy=array([1. , 2. , 3.3], dtype=float32)>
# 将 TF 张量的数据导出为 numpy 数组格式
x.numpy() 
array([1. , 2. , 3.3], dtype=float32)

与标量不同,向量的定义须通过 List 容器传给 tf.constant()函数。

创建一个元素的向量:

# 创建一个元素的向量
a = tf.constant([1.2]) 
a, a.shape
(<tf.Tensor: id=2, shape=(1,), dtype=float32, numpy=array([1.2], dtype=float32)>,
 TensorShape([1]))

创建 3 个元素的向量:

 # 创建 3 个元素的向量
a = tf.constant([1,2, 3.])
a, a.shape
(<tf.Tensor: id=3, shape=(3,), dtype=float32, numpy=array([1., 2., 3.], dtype=float32)>,
 TensorShape([3]))

定义矩阵

# 创建 2 行 2 列的矩阵
a = tf.constant([[1,2],[3,4]]) 
a, a.shape
(<tf.Tensor: id=4, shape=(2, 2), dtype=int32, numpy=
 array([[1, 2],
        [3, 4]])>, TensorShape([2, 2]))

三维张量可以定义为:

# 创建 3 维张量
tf.constant([[[1,2],[3,4]],[[5,6],[7,8]]]) 
<tf.Tensor: id=5, shape=(2, 2, 2), dtype=int32, numpy=
array([[[1, 2],
        [3, 4]],

       [[5, 6],
        [7, 8]]])>

通过传入字符串对象即可创建字符串类型的张量

# 创建字符串
a = tf.constant('Hello, Deep Learning.') 
a
<tf.Tensor: id=6, shape=(), dtype=string, numpy=b'Hello, Deep Learning.'>

1.2 字符串类型

通过传入字符串对象即可创建字符串类型的张量

# 创建字符串
a = tf.constant('Hello, Deep Learning.') 
a
<tf.Tensor: id=7, shape=(), dtype=string, numpy=b'Hello, Deep Learning.'>

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

# 小写化字符串
tf.strings.lower(a) 
<tf.Tensor: id=8, shape=(), dtype=string, numpy=b'hello, deep learning.'>

1.3 布尔类型

布尔类型的张量只需要传入 Python 语言的布尔类型数据,转换成 TensorFlow 内部布尔型即可。

# 创建布尔类型标量
tf.constant(True) 
<tf.Tensor: id=9, shape=(), dtype=bool, numpy=True>

创建布尔类型的向量

 # 创建布尔类型向量
tf.constant([True, False])
<tf.Tensor: id=10, shape=(2,), dtype=bool, numpy=array([ True, False])>

需要注意的是, TensorFlow 的布尔类型和 Python 语言的布尔类型并不等价,不能通用

# 创建 TF 布尔张量
a = tf.constant(True) 
# TF 布尔类型张量与 python 布尔类型比较
print(a is True) 
# 仅数值比较
print(a == True) 
False
tf.Tensor(True, shape=(), dtype=bool)

2.数值精度

对于数值类型的张量,可以保持为不同字节长度的精度,如浮点数 3.14 既可以保存为
16-bit 长度,也可以保存为 32-bit 甚至 64-bit 的精度。Bit 位越长,精度越高,同时占用的内存空间也就越大。常用的精度类型有 tf.int16, tf.int32, tf.int64, tf.float16, tf.float32, tf.float64,其中 tf.float64 即为 tf.double

在创建张量时,可以指定张量的保存精度

# 创建指定精度的张量
tf.constant(123456789, dtype=tf.int16)
<tf.Tensor: id=14, shape=(), dtype=int16, numpy=-13035>

对于浮点数, 高精度的张量可以表示更精准的数据,例如采用 tf.float32 精度保存π时,实际保存的数据为 3.1415927

import numpy as np
# 从 numpy 中导入 pi 常量
np.pi 
# 32 位
tf.constant(np.pi, dtype=tf.float32) 
<tf.Tensor: id=16, shape=(), dtype=float32, numpy=3.1415927>

如果采用 tf.float64 精度保存π,则能获得更高的精度

tf.constant(np.pi, dtype=tf.float64) # 64 位
<tf.Tensor: id=17, shape=(), dtype=float64, numpy=3.141592653589793>

2.1 读取精度

通过访问张量的 dtype 成员属性可以判断张量的保存精度

a = tf.constant(np.pi, dtype=tf.float16)

# 读取原有张量的数值精度
print('before:',a.dtype) 
# 如果精度不符合要求,则进行转换
if a.dtype != tf.float32: 
    # tf.cast 函数可以完成精度转换
    a = tf.cast(a,tf.float32) 
# 打印转换后的精度
print('after :',a.dtype) 
before: <dtype: 'float16'>
after : <dtype: 'float32'>

2.2 类型转换

系统的每个模块使用的数据类型、 数值精度可能各不相同, 对于不符合要求的张量的类型及精度, 需要通过 tf.cast 函数进行转换

# 创建 tf.float16 低精度张量
a = tf.constant(np.pi, dtype=tf.float16) 
# 转换为高精度张量
tf.cast(a, tf.double) 
<tf.Tensor: id=21, shape=(), dtype=float64, numpy=3.140625>

进行类型转换时,需要保证转换操作的合法性, 例如将高精度的张量转换为低精度的张量时,可能发生数据溢出隐患:

a = tf.constant(123456789, dtype=tf.int32)
# 转换为低精度整型
tf.cast(a, tf.int16) 
<tf.Tensor: id=23, shape=(), dtype=int16, numpy=-13035>

布尔类型与整型之间相互转换也是合法的, 是比较常见的操作

a = tf.constant([True, False])
# 布尔类型转整型
tf.cast(a, tf.int32) 
<tf.Tensor: id=25, shape=(2,), dtype=int32, numpy=array([1, 0])>

一般默认 0 表示 False, 1 表示 True,在 TensorFlow 中,将非 0 数字都视为 True,

a = tf.constant([-1, 0, 1, 2])
# 整型转布尔类型
tf.cast(a, tf.bool) 
<tf.Tensor: id=27, shape=(4,), dtype=bool, numpy=array([ True, False,  True,  True])>

3.待优化张量

为了区分需要计算梯度信息的张量与不需要计算梯度信息的张量,TensorFlow 增加了一种专门的数据类型来支持梯度信息的记录:tf.Variabletf.Variable 类型在普通的张量类型基础上添加了 nametrainable 等属性来支持计算图的构建。由于梯度运算会消耗大量的计算资源,而且会自动更新相关参数,对于不需要的优化的张量,如神经网络的输入 X,不需要通过 tf.Variable 封装;相反,对于需要计算梯度并优化的张量,如神经网络层的W 和

 
打赏
 本文转载自:网络 
所有权利归属于原作者,如文章来源标示错误或侵犯了您的权利请联系微信13520258486
更多>最近资讯中心
更多>最新资讯中心
0相关评论

推荐图文
推荐资讯中心
点击排行
最新信息
新手指南
采购商服务
供应商服务
交易安全
关注我们
手机网站:
新浪微博:
微信关注:

13520258486

周一至周五 9:00-18:00
(其他时间联系在线客服)

24小时在线客服