strings & ragged_tensor 讲解
本文只是简略介绍其中的一些方法,更为详细的API应用请参考官文文档,
链接:https://tensorflow.google.cn/versions/r2.0/api_docs/python
代码示例:
引用函数库
import tensorflow as tf
import numpy as np
#strings
t = tf.constant("cafe")
print(t)
print(tf.strings.length(t))
print(tf.strings.length(t, unit = "UTF8_CHAR")) #uft-8编码长度
print(tf.strings.unicode_decode(t,"UTF8")) #utf-8解码
tf.Tensor(b’cafe’, shape=(), dtype=string)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor(4, shape=(), dtype=int32)
tf.Tensor([ 99 97 102 101], shape=(4,), dtype=int32)
#string array
t = tf.constant(["cafe", "coffee", "咖啡"])
print(tf.strings.length(t, "UTF8_CHAR"))
r = tf.strings.unicode_decode(t, "UTF8")
print(r)
tf.Tensor([4 6 2], shape=(3,), dtype=int32)
#ragged tensor 不规则张量
"""
[2, 3, 5]
[]
[4,6]
"""
r = tf.ragged.constant([[11,12], [34,54,65], [], [57]])
#index operation
print(r)
print(r[1])
print(r[1:3]) #左闭右开,取二三行
tf.Tensor([34 54 65], shape=(3,), dtype=int32)
# ops on ragged tensor
r2 = tf.ragged.constant([[12, 34], [], [94]])
print(tf.concat(([r, r2]), axis=0)) #拼接操作
r3 = tf.ragged.constant([[21, 23], [61, 67, 89], [], [91]])
print(tf.concat([r, r3], axis=1))
# 将ragged_tenor 转化为普通的 Tensor ,缺的数据用0补齐
print(r.to_tensor(0))
tf.Tensor(
[[11 12 0]
[34 54 65]
[ 0 0 0]
[57 0 0]], shape=(4, 3), dtype=int32)