數(shù)值類型
標(biāo)量(Scalar):?jiǎn)蝹€(gè)的實(shí)數(shù),如1.2,1.3等,維度數(shù)dim為0,形狀shape為[]。
向量(Vector):n個(gè)實(shí)數(shù)的有序集合,通過(guò)中括號(hào)包裹,如[1,2],[1,2,3.4]等,維度數(shù)dim為1,長(zhǎng)度不定,形狀shape為[n]。
矩陣(Matrix):n行m列實(shí)數(shù)的有序集合,如[[1,2],[3,4]],維度數(shù)dim為2,每個(gè)維度上的長(zhǎng)度不定,形狀shape為[n,m]。
張量(Tensor):所有維度數(shù)dim>2的數(shù)組統(tǒng)稱為張量。張量的每個(gè)維度也叫作軸(Axis)。一般維度代表了具體的物理含義,比如shape為[2,32,32,3]的張量共有4維,如果表示圖像數(shù)據(jù)的話,每個(gè)維度代表的含義分別是圖像的數(shù)量、圖像高度、圖像寬度、圖像通道數(shù)。張量的維度數(shù)以及每個(gè)維度所代表的具體物理含義需要由用戶自行定義。
import tensorflow as tf
a = tf.constant(1.2)
b = tf.constant([1.2])
c = tf.constant([[1, 2], [3, 4]])
d = tf.constant([[[1, 2], [3, 4]], [[5, 6],[7, 8]]])
print("標(biāo)量:", a)
print("向量:", b)
print("矩陣:", c)
print("張量:", d)
np = c.numpy()
print("Numpy:", np)
輸出情況如下:
標(biāo)量: tf.Tensor(1.2, shape=(), dtype=float32)
向量: tf.Tensor([1.2], shape=(1,), dtype=float32)
矩陣: tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)
張量: tf.Tensor(
[[[1 2]
[3 4]]
[[5 6]
[7 8]]], shape=(2, 2, 2), dtype=int32)
Numpy: [[1 2]
[3 4]]
shape表示張量的形狀,dtype表示張量的數(shù)據(jù)精度
通過(guò)張量的numpy()方法可以返回Numpy.array類型的數(shù)據(jù)。
字符串類型
Tensorflow還支持字符串(String)類型的數(shù)據(jù),例如在表示圖像數(shù)據(jù)時(shí),可以先記錄圖像的路徑字符串,再通過(guò)預(yù)處理函數(shù)根據(jù)路徑讀取圖像張量。通過(guò)傳入字符串對(duì)象即可創(chuàng)建字符串類型的張量。
a = tf.constant("Hello, Deep Learning")
print("字符串:", a)
# 輸出結(jié)果
字符串: tf.Tensor(b'Hello, Deep Learning', shape=(), dtype=string)
在tf.strings模塊中,提供常見的字符串類型的工具函數(shù),如小寫lower()、大寫upper()、拼接join()、長(zhǎng)度length()、切分split()等。
tf.strings.lower(a)
布爾類型
布爾類型的張量只需要傳入Python語(yǔ)言的布爾類型數(shù)據(jù),轉(zhuǎn)換成TensorFlow內(nèi)部布爾型即可。
import tensorflow as tf
a = tf.constant(True)
print("布爾類型:", a)
# 輸出結(jié)果
布爾類型: tf.Tensor(True, shape=(), dtype=bool)
TensorFlow的布爾類型和Python語(yǔ)言的布爾類型并不等價(jià),不能通用。只可數(shù)字等價(jià),對(duì)象不等價(jià)。
數(shù)字精度
常用的精度類型有tf.int16, tf.int32, tf.int64, tf.float16, tf.float32, tf.float64等,其中tf.float64即為tf.double。
在創(chuàng)建張量時(shí),可以指定張量的保存精度
tf.constant(123456, dtype=tf.int16) # tf.Tensor(-7616, shape=(), dtype=int16)數(shù)據(jù)發(fā)生了溢出
tf.constant(123456, dtype=tf.int32) # tf.Tensor(123456, shape=(), dtype=int32)
對(duì)于大部分深度學(xué)習(xí)算法,一般使用tf.int32和tf.float32可滿足大部分場(chǎng)合的精度需求。
可以使用tf.cast()進(jìn)行類型的轉(zhuǎn)換 注意:高精度轉(zhuǎn)向低精度,可能發(fā)生溢出隱患
tf.cast(a, tf.double)
待優(yōu)化張量
為了區(qū)分需要計(jì)算梯度信息的張量與不需要計(jì)算梯度信息的張量,TensorFlow增加了一種專門的數(shù)據(jù)類型來(lái)支持梯度信息的記錄:tf.Variable()。tf.Variable類型在普通張量類型的基礎(chǔ)上添加了name、trainable等屬性來(lái)支持計(jì)算圖的構(gòu)建。通過(guò)tf.Variable()函數(shù)可以將普通張量轉(zhuǎn)換為待優(yōu)化張量。
import tensorflow as tf
a = tf.constant(1.23)
b = tf.Variable(a) # 通過(guò)普通張量賦值
print("普通張量:", a)
print("待優(yōu)化張量:", b)
c = tf.Variable([1,2,3]) # 直接賦值
print("待優(yōu)化張量:", c)
# 通過(guò)GradientTape.watch()方法臨時(shí)加入跟蹤梯度信息的列表,從而支持自動(dòng)求導(dǎo)功能
with tf.GradientTape() as tape:
tape.watch([a])
輸出結(jié)果:
普通張量: tf.Tensor(1.23, shape=(), dtype=float32)
待優(yōu)化張量: <tf.Variable 'Variable:0' shape=() dtype=float32, numpy=1.23>
待優(yōu)化張量: <tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([1, 2, 3], dtype=int32)>