TensorFlow2.x——strings & ragged_tensor 讲解

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)
<tf.RaggedTensor [[99, 97, 102, 101], [99, 111, 102, 102, 101, 101], [21654, 21857]]>

#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.RaggedTensor [[11, 12], [34, 54, 65], [], [57]]>
tf.Tensor([34 54 65], shape=(3,), dtype=int32)
<tf.RaggedTensor [[34, 54, 65], []]>

# 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))

<tf.RaggedTensor [[11, 12], [34, 54, 65], [], [57], [12, 34], [], [94]]>
<tf.RaggedTensor [[11, 12, 21, 23], [34, 54, 65, 61, 67, 89], [], [57, 91]]>

# 将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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值