TensorFlow特殊数据结构与图的深入解析
1. 特殊数据结构
TensorFlow除了常规的浮点或整数张量外,还支持多种特殊数据结构,下面为你详细介绍。
1.1 字符串张量
- 基本创建 :张量可以存储字节字符串,在自然语言处理中很有用。当创建包含Unicode字符串的张量时,TensorFlow会自动将其编码为UTF - 8。示例如下:
import tensorflow as tf
# 创建字节字符串张量
print(tf.constant(b"hello world"))
# 输出:<tf.Tensor: id=149, shape=(), dtype=string, numpy=b'hello world'>
# 创建Unicode字符串张量,自动编码为UTF - 8
print(tf.constant("café"))
# 输出:<tf.Tensor: id=138, shape=(), dtype=string, numpy=b'caf\xc3\xa9'>
# 创建表示Unicode字符串的张量
print(tf.constant([ord(c) for c in "café"]))
# 输出:<tf.Tensor: id=211, shape=(4,), dtype=int32, numpy=array([ 99, 97, 102, 233], dtype=int32)>
- 字符串长度与操作
超级会员免费看
订阅专栏 解锁全文
43

被折叠的 条评论
为什么被折叠?



