TensorFlow 之中的 Unicode 數(shù)據(jù)格式的處理
在我們之前的數(shù)據(jù)處理過程之中,我們都是采用的 ASCII 碼或者其他編碼處理數(shù)據(jù)格式的,但是這些編碼并不能夠完全表示當(dāng)前所有語言的所有字符,比如我們就無法使用 ASCII 碼來表示漢語。因此這個時候我們就需要用到一種新的編碼方式來進(jìn)行字符的處理,于是這節(jié)課我們來學(xué)習(xí)如何在 TensorFlow 之中處理 Unicode 格式的數(shù)據(jù)。
1. 在 TensorFlow之中創(chuàng)建 Unicode 字符串以及張量
在 TensorFlow 之中,Unicode是存儲在 tf.string 數(shù)據(jù)類型之中的,而在默認(rèn)的情況之下,Unicode在 TensorFlow 之中的默認(rèn)的編碼格式是 UTF-8 編碼的,我們可以通過以下示例查看具體的細(xì)節(jié):
ch_string = u"你好呀!"
en_string = u"Hello"
ch_string_utf_8 = tf.constant(ch_string)
en_string_utf_8 = tf.constant(en_string)
print(ch_string_utf_8, en_string_utf_8, sep='\n')
在這段代碼之中,我們著重進(jìn)行了以下的操作:
- 在字符串前加上了 u,從而指示該字符串為 Unicode 格式;
- 我們使用 tf.constant 函數(shù)來將字符串轉(zhuǎn)化為 Tensor 張量。
我們可以得到如下輸出:
tf.Tensor(b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80\xef\xbc\x81', shape=(), dtype=string)
tf.Tensor(b'Hello', shape=(), dtype=string)
我們可以發(fā)現(xiàn)以下幾點:
- 這兩個 Tensor 的數(shù)據(jù)類型都為 string ,這其實是 TensorFlow 內(nèi)部的 tf.string 數(shù)據(jù)類型;
- 這兩 個Tensor 的 Shape 都為空,因為在 TensoFlow 之中不會為 Unicode 字符串賦予形狀,這是因為字符串的長度不盡相同;
- 第一個中文的字符串被按照 UTF-8 規(guī)則進(jìn)行了編碼,而英文并沒有進(jìn)行編碼(嚴(yán)格來說,英文也進(jìn)行了編碼,只是編碼前后相同,這一點可以由字符串前面的b就可以看出)。
2. TensorFlow 之中 Unicode 字符串的存在形式
在TensorFlow之 中, Unicode 字符串有兩種表現(xiàn)形式,它們分別是:
- 編碼格式:使用編碼規(guī)則進(jìn)行編碼后的字符串,比如 UTF-8、UTF-16 等編碼方式;
- 解碼格式:對于每一個字符按照唯一的整數(shù)進(jìn)行編碼之后的格式,這些整數(shù)被稱作“代碼點”。
在第一小節(jié)之中我們看到的形式就是編碼格式,而且編碼方式為 UTF-8,對于兩種格式,我們可以通過 tf.strings.unicode_decode 以及 tf.strings.unicode_encode 進(jìn)行相應(yīng)的轉(zhuǎn)化,比如以下示例:
ch_string_utf_8_decode = tf.strings.unicode_decode(ch_string_utf_8, input_encoding='UTF-8')
ch_string_utf_8_encode = tf.strings.unicode_encode(ch_string_utf_8_decode, output_encoding='UTF-8')
print(ch_string_utf_8_decode)
print(ch_string_utf_8_encode)
在這 tf.strings.unicode_decode 函數(shù)之中,包含兩個參數(shù):
- 第一個參數(shù)就是我們要進(jìn)行解碼的字符串,比如我們的 ch_string_utf_8 ;
- 第二個參數(shù)是輸入字符串的編碼格式,因為我們的字符串編碼格式為 UTF-8 ,因此在這里我們的參數(shù)為input_encoding=‘UTF-8’。
tf.strings.unicode_encode 函數(shù)與 tf.strings.unicode_decode 函數(shù)相似,只是第二個參數(shù)是輸出字符串的編碼方式,因為我們需要 UTF-8 編碼的格式,因此這里我們選擇 output_encoding=‘UTF-8’。
我們可以得到輸出:
tf.Tensor([20320 22909 21568 65281], shape=(4,), dtype=int32)
tf.Tensor(b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80\xef\xbc\x81', shape=(), dtype=string)
我們發(fā)現(xiàn)解碼后的字符串就是一串整數(shù)數(shù)組,其中的每個整數(shù)代表著一個中文字符;于此同時,更重要的是解碼產(chǎn)生的數(shù)組是擁有形狀的,而正因如此,解碼后的表示更加適合我們用作數(shù)據(jù)集。
同時我們也可以發(fā)現(xiàn) ch_string_utf_8_encode 與 ch_string_utf_8 兩個完全一樣,因為 ch_string_utf_8 本來就是編碼的字符串嘛。
3. 單個 Unicode 字符串的處理
無論 Unicode 格式怎么編碼,Unicode 字符串終歸是字符串,因此在實際應(yīng)用之中就會進(jìn)行各種的字符串操作,因此我們有必要來學(xué)習(xí)一下在 TensorFlow 之中的 Unicode 字符串的基本處理操作。
3.1 如何獲取 Unicode 字符串的長度
我們可以使用 tf.strings.length 函數(shù)來獲取 Unicode 字符串的長度,該函數(shù)含有兩個重要的參數(shù):
- str,要獲取長度的字符串;
- unit,長度的單位,目前包含兩個選項,一個是“BYTE”,另一個是“UTF8_CHAR”:
- BYTE,按照字節(jié)進(jìn)行計數(shù),從而獲取字符串的長度;
- UTF8_CHAR,按照單個 Unicode 字符的單位進(jìn)行計數(shù),獲取我們通常認(rèn)知的長度。
同時該 API 返回的是一個 Tensor ,我們可以通過 numpy() 函數(shù)來將其轉(zhuǎn)化為我們可以直接使用的數(shù)字長度。
比如以下代碼:
len_bytes = tf.strings.length(ch_string_utf_8, unit='BYTE')
len_chars = tf.strings.length(ch_string_utf_8, unit='UTF8_CHAR')
print(len_bytes, len_chars)
print(len_bytes.numpy(), len_chars.numpy())
我們可以得到如下輸出:
tf.Tensor(12, shape=(), dtype=int32) tf.Tensor(4, shape=(), dtype=int32)
12 4
可以看到,“你好呀!”字符串含有 12 個字節(jié)長度,而且正如我們看到的那樣,包含 4 個漢字字符。
3.2 子字符串的操作
對于 Unicode 子字符串的操作,我們可以通過 tf.strings.substr 函數(shù)來實現(xiàn),該 API 接收 4 個參數(shù),它們分別是:
- str,要進(jìn)行子字符串操作的 Unicode 字符串;
- unit,與前面的 unit 一樣,表示截取的單位,包含“BYTE”以及“UTF8_CHAR”兩個選項;
- pos,開始截取的位置;
- len,截取的長度。
我們可以通過以下示例進(jìn)行查看:
print(tf.strings.substr(ch_string_utf_8, pos=3, len=1, unit='BYTE'))
print(tf.strings.substr(ch_string_utf_8, pos=3, len=1, unit='UTF8_CHAR'))
我們可以得到如下輸出:
tf.Tensor(b'\xe5', shape=(), dtype=string)
tf.Tensor(b'\xef\xbc\x81', shape=(), dtype=string)
我們可以發(fā)現(xiàn),b’\xe5 剛剛好是 3 位置的字符串,而 b’\xef\xbc\x81’ 剛剛好是最后一個“!”的 Unicode 表示。
3.3 字符串的拆分
通過拆分操作,我們可以將每個Unicode字符進(jìn)行拆分,從而形成一個數(shù)組,每個數(shù)組包含一個 Unicode 字符的編碼。
對于該操作,我們可以通過 tf.strings.unicode_split 函數(shù)實現(xiàn),該函數(shù)的具體使用如下:
print(tf.strings.unicode_split(ch_string_utf_8, 'UTF-8'))
其中的第二個參數(shù)表示的是字符串的編碼方式,我們可以得到如下輸出:
tf.Tensor([b'\xe4\xbd\xa0' b'\xe5\xa5\xbd' b'\xe5\x91\x80' b'\xef\xbc\x81'], shape=(4,), dtype=string)
我們看到,我們的字符串已經(jīng)成功進(jìn)行了拆分的基本操作。
4. 使用 Unicode 數(shù)據(jù)構(gòu)造數(shù)據(jù)集的示例
在實際的使用之中,我們大致分為以下幾步來構(gòu)造 Unicode 字符串的數(shù)據(jù)集:
- 首先將 Unicode 字符串?dāng)?shù)據(jù)進(jìn)行解碼,因為這樣就可以計算長度;
- 將其統(tǒng)一為定長的形式;
- 構(gòu)造數(shù)據(jù)集
對于解碼,我們可以通過之前的 tf.strings.unicode_decode 函數(shù)進(jìn)行解碼,我們可以通過下面的示例查看解碼的結(jié)果:
data_string = [u"你好呀", u"很高興認(rèn)識你", u"Hello", u"Nice to meet you"]
decode_data = tf.strings.unicode_decode(data_string, input_encoding='UTF-8')
print(decode_data, decode_data.shape, sep='\n')
我們可以得到的輸出為:
<tf.RaggedTensor [[20320, 22909, 21568], [24456, 39640, 20852, 35748, 35782, 20320], [72, 101, 108, 108, 111], [78, 105, 99, 101, 32, 116, 111, 32, 109, 101, 101, 116, 32, 121, 111, 117]]>
(4, None)
可以發(fā)現(xiàn),我們得到的數(shù)據(jù)為 tf.RaggedTensor 格式,而這種格式的每個元素都不是定長的,而這就到十六我們的數(shù)據(jù)的 shape 只能為(4, None),因此我們可以通過to_tensor()函數(shù)來將其轉(zhuǎn)化為定長的張量。
decode_data_pad = decode_data.to_tensor()
print(decode_data_pad, decode_data_pad.shape, sep='\n')
我們可以得到如下結(jié)果:
tf.Tensor(
[[20320 22909 21568 0 0 0 0 0 0 0 0 0
0 0 0 0]
[24456 39640 20852 35748 35782 20320 0 0 0 0 0 0
0 0 0 0]
[ 72 101 108 108 111 0 0 0 0 0 0 0
0 0 0 0]
[ 78 105 99 101 32 116 111 32 109 101 101 116
32 121 111 117]], shape=(4, 16), dtype=int32)
(4, 16)
由此我們可以發(fā)現(xiàn),我們的數(shù)據(jù)已經(jīng) Padding 到了統(tǒng)一的長度,而這個長度是根據(jù)最長的字符串的長度來決定的。這樣之后,我們便可以進(jìn)一步構(gòu)造數(shù)據(jù)集,我們將會采用定長與不定長的數(shù)據(jù)分別構(gòu)造數(shù)據(jù)集,來查看兩者的區(qū)別。
在這里我們可以使用虛擬的標(biāo)簽進(jìn)行操作, 我們依然使用傳統(tǒng)的 tf.data.Dataset.from_tensor_slices 函數(shù)來進(jìn)行數(shù)據(jù)集的構(gòu)建:
labels = [0, 0, 0, 0]
dataset = tf.data.Dataset.from_tensor_slices((decode_data, labels))
dataset_pad = tf.data.Dataset.from_tensor_slices((decode_data_pad, labels))
print(dataset)
print(dataset_pad)
我們可以得到結(jié)果:
<TensorSliceDataset shapes: ((None,), ()), types: (tf.int32, tf.int32)>
<TensorSliceDataset shapes: ((16,), ()), types: (tf.int32, tf.int32)>
我們可以看到,沒有采用 Padding 的數(shù)據(jù)集的形狀為 ((None,), ()) ,而采用了 Padding 數(shù)據(jù)集的形狀為((16,), ()),而后者是會對我們的使用有利的,因此我們推薦使用后者進(jìn)行操作。
5. 小結(jié)
在這節(jié)課之中,我們學(xué)習(xí)了如何在 TensorFlow 之中使用 Unicode 字符串,我們同時學(xué)習(xí)了 Unicode 字符串的兩種存在形式,又了解了 Unicode 字符串的基本操作,最后我們通過一個簡單的示例了解了如何使用 Unicode 字符串構(gòu)造數(shù)據(jù)集。