Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Unicode strings

Unicode strings

作者头像
狼啸风云
发布于 2022-09-30 03:13:53
发布于 2022-09-30 03:13:53
2.5K00
代码可运行
举报
运行总次数:0
代码可运行

目录

Introduction

The tf.string data type

Representing Unicode

Converting between representations

Batch dimensions

Unicode operations

Character length

Character substrings

Split Unicode strings

Byte offsets for characters

Unicode scripts

Example: Simple segmentation


Introduction

处理自然语言的模型通常使用不同的字符集处理不同的语言。Unicode是一种标准编码系统,用于表示几乎所有语言的字符。每个字符都使用0到0x10FFFF之间的唯一整数编码点进行编码。Unicode字符串是由零个或多个代码点组成的序列。本教程展示了如何在TensorFlow中表示Unicode字符串,并使用标准字符串操作的Unicode等效项来操作它们。它基于脚本检测将Unicode字符串分隔为令牌。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
from __future__ import absolute_import, division, print_function, unicode_literals

try:
  # %tensorflow_version only exists in Colab.
  %tensorflow_version 2.x
except Exception:
  pass
import tensorflow as tf

The tf.string data type

基本的TensorFlow tf.string.dtype允许您构建字节字符串的张量。Unicode字符串默认编码为utf-8。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.constant(u"Thanks ?")
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=0, shape=(), dtype=string, numpy=b'Thanks \xf0\x9f\x98\x8a'>

一个特遣部队。字符串张量可以包含不同长度的字节字符串,因为字节字符串被视为原子单位。弦的长度不包括在张量维数中。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.constant([u"You're", u"welcome!"]).shape
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
TensorShape([2])

注意:当使用python构造字符串时,unicode的处理方式不同于betweeen v2和v3。在v2中,unicode字符串由“u”前缀表示,如上所示。在v3中,默认情况下字符串是unicode编码的。

Representing Unicode

在TensorFlow中有两种表示Unicode字符串的标准方法:

  • string scalar——使用已知的字符编码对代码点序列进行编码。
  • int32 vector ——每个位置包含一个代码点。

例如,下面的三个值都代表了Unicode字符串“语言处理”(意思是“语言处理”):

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a UTF-8 encoded string scalar.
text_utf8 = tf.constant(u"语言处理")
text_utf8
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=3, shape=(), dtype=string, numpy=b'\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86'>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a UTF-16-BE encoded string scalar.
text_utf16be = tf.constant(u"语言处理".encode("UTF-16-BE"))
text_utf16be
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=5, shape=(), dtype=string, numpy=b'\x8b\xed\x8a\x00Y\x04t\x06'>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Unicode string, represented as a vector of Unicode code points.
text_chars = tf.constant([ord(char) for char in u"语言处理"])
text_chars
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=7, shape=(4,), dtype=int32, numpy=array([35821, 35328, 22788, 29702], dtype=int32)>

Converting between representations

TensorFlow提供了在这些不同表示之间进行转换的操作:

  • tf.strings.unicode_decode:将编码的字符串标量转换为代码点向量。
  • tf.strings.unicode_encode:将代码点向量转换为编码的字符串标量。
  • tf.strings.unicode_transcode:将已编码的字符串标量转换为不同的编码。
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_decode(text_utf8,
                          input_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=12, shape=(4,), dtype=int32, numpy=array([35821, 35328, 22788, 29702], dtype=int32)>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(text_chars,
                          output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=23, shape=(), dtype=string, numpy=b'\xe8\xaf\xad\xe8\xa8\x80\xe5\xa4\x84\xe7\x90\x86'>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_transcode(text_utf8,
                             input_encoding='UTF8',
                             output_encoding='UTF-16-BE')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=25, shape=(), dtype=string, numpy=b'\x8b\xed\x8a\x00Y\x04t\x06'>

Batch dimensions

当解码多个字符串时,每个字符串中的字符数可能不相等。返回的结果是tf.ragged张量,其中最内层维度的长度随每个字符串中的字符数而变化:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# A batch of Unicode strings, each represented as a UTF8-encoded string.
batch_utf8 = [s.encode('UTF-8') for s in
              [u'hÃllo',  u'What is the weather tomorrow',  u'Göödnight', u'?']]
batch_chars_ragged = tf.strings.unicode_decode(batch_utf8,
                                               input_encoding='UTF-8')
for sentence_chars in batch_chars_ragged.to_list():
  print(sentence_chars)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[104, 195, 108, 108, 111]
[87, 104, 97, 116, 32, 105, 115, 32, 116, 104, 101, 32, 119, 101, 97, 116, 104, 101, 114, 32, 116, 111, 109, 111, 114, 114, 111, 119]
[71, 246, 246, 100, 110, 105, 103, 104, 116]
[128522]
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
batch_chars_padded = batch_chars_ragged.to_tensor(default_value=-1)
print(batch_chars_padded.numpy())
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
WARNING: Logging before flag parsing goes to stderr.
W0813 08:53:58.604015 139985772394240 deprecation.py:323] From /tmpfs/src/tf_docs_env/lib/python3.5/site-packages/tensorflow/python/ops/ragged/ragged_tensor.py:1553: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where

[[   104    195    108    108    111     -1     -1     -1     -1     -1
      -1     -1     -1     -1     -1     -1     -1     -1     -1     -1
      -1     -1     -1     -1     -1     -1     -1     -1]
 [    87    104     97    116     32    105    115     32    116    104
     101     32    119    101     97    116    104    101    114     32
     116    111    109    111    114    114    111    119]
 [    71    246    246    100    110    105    103    104    116     -1
      -1     -1     -1     -1     -1     -1     -1     -1     -1     -1
      -1     -1     -1     -1     -1     -1     -1     -1]
 [128522     -1     -1     -1     -1     -1     -1     -1     -1     -1
      -1     -1     -1     -1     -1     -1     -1     -1     -1     -1
      -1     -1     -1     -1     -1     -1     -1     -1]]
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
batch_chars_sparse = batch_chars_ragged.to_sparse()

当编码多个长度相同的字符串时,使用tf.Tensor可用作输入:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode([[99, 97, 116], [100, 111, 103], [ 99, 111, 119]],
                          output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=129, shape=(3,), dtype=string, numpy=array([b'cat', b'dog', b'cow'], dtype=object)>

当编码多个长度可变的字符串时,使用tf.RaggedTensor作为输入:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(batch_chars_ragged, output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=131, shape=(4,), dtype=string, numpy=
array([b'h\xc3\x83llo', b'What is the weather tomorrow',
       b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>

如果你有一个带多个填充或稀疏格式字符串的张量,那么把它转换成tf。调用unicode_encode之前的ragged张量:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(
    tf.RaggedTensor.from_sparse(batch_chars_sparse),
    output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=214, shape=(4,), dtype=string, numpy=
array([b'h\xc3\x83llo', b'What is the weather tomorrow',
       b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(
    tf.RaggedTensor.from_tensor(batch_chars_padded, padding=-1),
    output_encoding='UTF-8')
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.Tensor: id=289, shape=(4,), dtype=string, numpy=
array([b'h\xc3\x83llo', b'What is the weather tomorrow',
       b'G\xc3\xb6\xc3\xb6dnight', b'\xf0\x9f\x98\x8a'], dtype=object)>

Unicode operations

Character length

tf.strings.length操作有一个参数单元,它指示应该如何计算长度。unit默认值为“BYTE”,但可以将其设置为其他值,如“UTF8_CHAR”或“UTF16_CHAR”,以确定每个编码字符串中的Unicode码点数量。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Note that the final character takes up 4 bytes in UTF8.
thanks = u'Thanks ?'.encode('UTF-8')
num_bytes = tf.strings.length(thanks).numpy()
num_chars = tf.strings.length(thanks, unit='UTF8_CHAR').numpy()
print('{} bytes; {} UTF-8 characters'.format(num_bytes, num_chars))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
11 bytes; 8 UTF-8 characters

Character substrings

同样,tf.strings.substr操作接受“unit”参数,并使用它来确定“pos”和“len”参数包含哪些偏移量。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# default: unit='BYTE'. With len=1, we return a single byte.
tf.strings.substr(thanks, pos=7, len=1).numpy()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
b'\xf0'
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# Specifying unit='UTF8_CHAR', we return a single character, which in this case
# is 4 bytes.
print(tf.strings.substr(thanks, pos=7, len=1, unit='UTF8_CHAR').numpy())
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
b'\xf0\x9f\x98\x8a'

Split Unicode strings

tf.strings。unicode_split操作将unicode字符串拆分为各个字符的子字符串:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_split(thanks, 'UTF-8').numpy()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
array([b'T', b'h', b'a', b'n', b'k', b's', b' ', b'\xf0\x9f\x98\x8a'],
      dtype=object)

Byte offsets for characters

将tf.strings生成的字符张量对齐。使用原始字符串unicode_decode,了解每个字符开始的偏移量是很有用的。tf.strings方法。unicode_decode_with_offsets类似于unicode_decode,只是它返回第二个张量,其中包含每个字符的起始偏移量。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
codepoints, offsets = tf.strings.unicode_decode_with_offsets(u"???", 'UTF-8')

for (codepoint, offset) in zip(codepoints.numpy(), offsets.numpy()):
  print("At byte offset {}: codepoint {}".format(offset, codepoint))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
At byte offset 0: codepoint 127880
At byte offset 4: codepoint 127881
At byte offset 8: codepoint 127882

Unicode scripts

每个Unicode代码点都属于一个称为脚本的代码点集合。角色的脚本有助于确定角色可能使用的语言。例如,知道“Б”是在斯拉夫字母表明现代文本包含字符可能来自俄罗斯或乌克兰等斯拉夫语言。TensorFlow提供tf.string。确定给定代码点使用哪个脚本的unicode_script操作。脚本代码是与Unicode (ICU) UScriptCode值的国际组件对应的int32值。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
uscript = tf.strings.unicode_script([33464, 1041])  # ['芸', 'Б']

print(uscript.numpy())  # [17, 8] == [USCRIPT_HAN, USCRIPT_CYRILLIC]
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[17  8]

tf.strings.unicode_script操作也可以应用于多维tf.Tensor.RaggedTensors codepoints:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
print(tf.strings.unicode_script(batch_chars_ragged))
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.RaggedTensor [[25, 25, 25, 25, 25], [25, 25, 25, 25, 0, 25, 25, 0, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 0, 25, 25, 25, 25, 25, 25, 25, 25], [25, 25, 25, 25, 25, 25, 25, 25, 25], [0]]>

Example: Simple segmentation

分割是将文本分割成类词单元的任务。当空格用于分隔单词时,这通常很简单,但是一些语言(如汉语和日语)不使用空格,而一些语言(如德语)包含长复合词,必须将其拆分才能分析其含义。在web文本,不同的语言和脚本经常混在一起,如“纽约株価”(纽约证券交易所)。我们可以通过改变脚本来近似单词边界来执行非常粗略的分割(不需要实现任何ML模型)。这将为字符串工作像“纽约株価”上面的例子。它也适用于大多数使用空格的语言,因为各种脚本的空格字符都被分类为USCRIPT_COMMON,这是一种与任何实际文本不同的特殊脚本代码。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# dtype: string; shape: [num_sentences]
#
# The sentences to process.  Edit this line to try out different inputs!
sentence_texts = [u'Hello, world.', u'世界こんにちは']

首先,我们将句子解码为字符码点,并找到每个字符的脚本标识符。

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# dtype: int32; shape: [num_sentences, (num_chars_per_sentence)]
#
# sentence_char_codepoint[i, j] is the codepoint for the j'th character in
# the i'th sentence.
sentence_char_codepoint = tf.strings.unicode_decode(sentence_texts, 'UTF-8')
print(sentence_char_codepoint)

# dtype: int32; shape: [num_sentences, (num_chars_per_sentence)]
#
# sentence_char_scripts[i, j] is the unicode script of the j'th character in
# the i'th sentence.
sentence_char_script = tf.strings.unicode_script(sentence_char_codepoint)
print(sentence_char_script)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.RaggedTensor [[72, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 46], [19990, 30028, 12371, 12435, 12395, 12385, 12399]]>
<tf.RaggedTensor [[25, 25, 25, 25, 25, 0, 0, 25, 25, 25, 25, 25, 0], [17, 17, 20, 20, 20, 20, 20]]>

接下来,我们使用这些脚本标识符来确定应该在何处添加单词边界。我们在每个句子的开头加上一个单词边界,对于每个字符,其脚本与前一个字符不同:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# dtype: bool; shape: [num_sentences, (num_chars_per_sentence)]
#
# sentence_char_starts_word[i, j] is True if the j'th character in the i'th
# sentence is the start of a word.
sentence_char_starts_word = tf.concat(
    [tf.fill([sentence_char_script.nrows(), 1], True),
     tf.not_equal(sentence_char_script[:, 1:], sentence_char_script[:, :-1])],
    axis=1)

# dtype: int64; shape: [num_words]
#
# word_starts[i] is the index of the character that starts the i'th word (in
# the flattened list of characters from all sentences).
word_starts = tf.squeeze(tf.where(sentence_char_starts_word.values), axis=1)
print(word_starts)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.Tensor([ 0  5  7 12 13 15], shape=(6,), dtype=int64)

然后我们可以使用这些开始偏移量来构建一个包含所有批次单词列表的ragged张量:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# dtype: int32; shape: [num_words, (num_chars_per_word)]
#
# word_char_codepoint[i, j] is the codepoint for the j'th character in the
# i'th word.
word_char_codepoint = tf.RaggedTensor.from_row_starts(
    values=sentence_char_codepoint.values,
    row_starts=word_starts)
print(word_char_codepoint)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.RaggedTensor [[72, 101, 108, 108, 111], [44, 32], [119, 111, 114, 108, 100], [46], [19990, 30028], [12371, 12435, 12395, 12385, 12399]]>

最后,我们可以把单词codepoints ragged张量分割成句子:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
# dtype: int64; shape: [num_sentences]
#
# sentence_num_words[i] is the number of words in the i'th sentence.
sentence_num_words = tf.reduce_sum(
    tf.cast(sentence_char_starts_word, tf.int64),
    axis=1)

# dtype: int32; shape: [num_sentences, (num_words_per_sentence), (num_chars_per_word)]
#
# sentence_word_char_codepoint[i, j, k] is the codepoint for the k'th character
# in the j'th word in the i'th sentence.
sentence_word_char_codepoint = tf.RaggedTensor.from_row_lengths(
    values=word_char_codepoint,
    row_lengths=sentence_num_words)
print(sentence_word_char_codepoint)
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
<tf.RaggedTensor [[[72, 101, 108, 108, 111], [44, 32], [119, 111, 114, 108, 100], [46]], [[19990, 30028], [12371, 12435, 12395, 12385, 12399]]]>

为了让最终的结果更容易阅读,我们可以把它编码回UTF-8字符串:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
tf.strings.unicode_encode(sentence_word_char_codepoint, 'UTF-8').to_list()
代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
[[b'Hello', b', ', b'world', b'.'],
 [b'\xe4\xb8\x96\xe7\x95\x8c',
  b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf']]
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-09-06,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
TensorFlow支持Unicode,中文NLP终于省心了
什么是 Unicode?Unicode 是计算机科学领域里的一项业界标准,包括字符集、编码方案等。Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言、跨平台进行文本转换、处理的要求。
AI科技大本营
2018/12/28
4.2K0
TensorFlow支持Unicode,中文NLP终于省心了
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(九)
¹ Jasper Snoek 等人,“机器学习算法的实用贝叶斯优化”,《第 25 届国际神经信息处理系统会议论文集》2(2012):2951–2959。
ApacheCN_飞龙
2024/05/24
1890
Sklearn、TensorFlow 与 Keras 机器学习实用指南第三版(九)
让Tensorflow直接输入字符串,无需额外词表的3种方法
tf.strings是很早就加入到tensorflow的内容,不过一直都很边缘,而且支持也不好,直到2.1/2.2版本才开始有越来越好的支持。
段清华DEAN
2020/04/10
1.4K0
让Tensorflow直接输入字符串,无需额外词表的3种方法
让Tensorflow直接输入字符串,无需额外词表的3种方法
tf.strings是很早就加入到tensorflow的内容,不过一直都很边缘,而且支持也不好,直到2.1/2.2版本才开始有越来越好的支持。
段清华DEAN
2020/06/10
1.3K0
tf_text
在文字的建模实践中,一般需要把原始文字拆解成单字、单词或者词组,然后将这些拆分的要素进行索引,标记化供机器学习算法使用。这种预处理叫做标注(Tokenize)。虽然这些功能都可以用python实现,但是Keras提供了现成的方法。
润森
2019/11/24
9530
Tensorflow2.0常用基础API
tensorflow2.0改进之后已经非常像numpy形式了,不用像之前的session那样操作,一些基本的操作如下。需要注意的店以及部分数据均写在代码注释中。
Mirza Zhao
2023/06/26
7880
TensorFlow 2.0 快速入门指南:第一部分
在本部分中,我们将介绍 TensorFlow 2.00 alpha。 我们将首先概述该机器学习生态系统的主要功能,并查看其使用示例。 然后我们将介绍 TensorFlow 的高级 Keras API。 我们将在本节结尾处研究人工神经网络技术。
ApacheCN_飞龙
2023/04/23
4.4K0
【tensorflow2.0】数据管道dataset
如果需要训练的数据大小不大,例如不到1G,那么可以直接全部读入内存中进行训练,这样一般效率最高。
西西嘛呦
2020/08/26
1.8K0
【tensorflow2.0】数据管道dataset
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第12章 使用TensorFlow自定义模型并训练
目前为止,我们只是使用了TensorFlow的高级API —— tf.keras,它的功能很强大:搭建了各种神经网络架构,包括回归、分类网络、Wide & Deep 网络、自归一化网络,使用了各种方法,包括批归一化、dropout和学习率调度。事实上,你在实际案例中95%碰到的情况只需要tf.keras就足够了(和tf.data,见第13章)。现在来深入学习TensorFlow的低级Python API。当你需要实现自定义损失函数、自定义标准、层、模型、初始化器、正则器、权重约束时,就需要低级API了。甚至有时需要全面控制训练过程,例如使用特殊变换或对约束梯度时。这一章就会讨论这些问题,还会学习如何使用TensorFlow的自动图生成特征提升自定义模型和训练算法。首先,先来快速学习下TensorFlow。
SeanCheney
2019/12/13
5.4K0
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第12章 使用TensorFlow自定义模型并训练
TensorFlow2.X学习笔记(5)--TensorFlow中阶API之数据管道
Dataset数据结构应用非常灵活,因为它本质上是一个Sequece序列,其每个元素可以是各种类型,例如可以是张量,列表,字典,也可以是Dataset。
MiChong
2020/09/24
1.5K0
Transformers 4.37 中文文档(二十二)
BARThez 模型是由 Moussa Kamal Eddine、Antoine J.-P. Tixier 和 Michalis Vazirgiannis 于 2020 年 10 月 23 日提出的BARThez: a Skilled Pretrained French Sequence-to-Sequence Model。
ApacheCN_飞龙
2024/06/26
2070
【tensorflow2.0】张量数据结构
Tensorflow的基本数据结构是张量Tensor。张量即多维数组。Tensorflow的张量和numpy中的array很类似。
西西嘛呦
2020/08/26
5050
TensorFlow2.0(6):利用data模块进行数据预处理
在整个机器学习过程中,除了训练模型外,应该就属数据预处理过程消耗的精力最多,数据预处理过程需要完成的任务包括数据读取、过滤、转换等等。为了将用户从繁杂的预处理操作中解放处理,更多地将精力放在算法建模上,TensorFlow中提供了data模块,这一模块以多种方式提供了数据读取、数据处理、数据保存等功能。本文重点是data模块中的Dataset对象。
统计学家
2019/12/23
1.9K0
Tensorflow使用TFRecords和tf.Example
参考 tf.python_io.TFRecordWriter() - 云+社区 - 腾讯云
狼啸风云
2022/09/28
8760
Tensorflow使用TFRecords和tf.Example
【tensorflow2.0】张量的结构操作
张量数学运算主要有:标量运算,向量运算,矩阵运算。另外我们会介绍张量运算的广播机制。
西西嘛呦
2020/08/26
2.2K0
Tensorflow技术点整理(二)
这里跟PyTorch不同的是序号定义的不同,PyTorch是上下定义位置,而Tensorflow是左右定义位置。
算法之名
2022/03/24
4120
Tensorflow技术点整理(二)
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第13章 使用TensorFlow加载和预处理数据
Data API还可以从现成的文件(比如CSV文件)、固定大小的二进制文件、使用TensorFlow的TFRecord格式的文件(支持大小可变的记录)读取数据。TFRecord是一个灵活高效的二进制格式,基于Protocol Buffers(一个开源二进制格式)。Data API还支持从SQL数据库读取数据。另外,许多开源插件也可以用来从各种数据源读取数据,包括谷歌的BigQuery。
SeanCheney
2019/12/16
3.4K0
《机器学习实战:基于Scikit-Learn、Keras和TensorFlow》第13章 使用TensorFlow加载和预处理数据
Transformers 4.37 中文文档(八十八)
LayoutLM 模型是由 Yiheng Xu,Minghao Li,Lei Cui,Shaohan Huang,Furu Wei 和 Ming Zhou 在论文LayoutLM: Pre-training of Text and Layout for Document Image Understanding中提出的。这是一种简单但有效的文本和布局预训练方法,用于文档图像理解和信息提取任务,如表单理解和收据理解。它在几个下游任务上取得了最先进的结果:
ApacheCN_飞龙
2024/06/26
3530
Tensorflow2.0
tf.test.is_gpu_available() # 判断gpu可用与否 ``` 2. 从镜像配置 ```shell # 云端的系统镜像直接有开发环境 # 升级tensorflow 版本 pip install --upgrade tensorflow-gpu==2.0.0 pip3 install --upgrade tensorflow-gpu==2.0.0 ```
Dean0731
2020/05/25
1.6K0
Tensorflow2.0
Transformers 4.37 中文文档(五十七)
RoCBert 模型是由 HuiSu、WeiweiShi、XiaoyuShen、XiaoZhou、TuoJi、JiaruiFang、JieZhou 在 RoCBert: Robust Chinese Bert with Multimodal Contrastive Pretraining 中提出的。它是一个经过预训练的中文语言模型,在各种形式的对抗攻击下具有鲁棒性。
ApacheCN_飞龙
2024/06/26
2450
相关推荐
TensorFlow支持Unicode,中文NLP终于省心了
更多 >
LV.1
这个人很懒,什么都没有留下~
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文