.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size=2, num_steps=8)
for X, X_valid_len, Y, Y_valid_len in train_iter:
print('X:', X.astype(np.int32))
print('X的有效长度:', X_valid_len)
print('Y:', Y.astype(np.int32))
print('Y的有效长度:', Y_valid_len)
break
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
[07:16:20] ../src/storage/storage.cc:196: Using Pooled (Naive) StorageManager for CPU
X: [[ 0 4 3 1 1 1 1 1]
[ 6 18 59 4 3 1 1 1]]
X的有效长度: [3 5]
Y: [[ 0 4 3 1 1 1 1 1]
[ 6 7 168 4 3 1 1 1]]
Y的有效长度: [3 5]
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size=2, num_steps=8)
for X, X_valid_len, Y, Y_valid_len in train_iter:
print('X:', X.type(torch.int32))
print('X的有效长度:', X_valid_len)
print('Y:', Y.type(torch.int32))
print('Y的有效长度:', Y_valid_len)
break
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
X: tensor([[ 7, 43, 4, 3, 1, 1, 1, 1],
[44, 23, 4, 3, 1, 1, 1, 1]], dtype=torch.int32)
X的有效长度: tensor([4, 4])
Y: tensor([[ 6, 7, 40, 4, 3, 1, 1, 1],
[ 0, 5, 3, 1, 1, 1, 1, 1]], dtype=torch.int32)
Y的有效长度: tensor([5, 3])
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size=2, num_steps=8)
for X, X_valid_len, Y, Y_valid_len in train_iter:
print('X:', tf.cast(X, tf.int32))
print('X的有效长度:', X_valid_len)
print('Y:', tf.cast(Y, tf.int32))
print('Y的有效长度:', Y_valid_len)
break
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
X: tf.Tensor(
[[ 6 18 35 4 3 1 1 1]
[ 0 29 4 3 1 1 1 1]], shape=(2, 8), dtype=int32)
X的有效长度: tf.Tensor([5 4], shape=(2,), dtype=int32)
Y: tf.Tensor(
[[ 6 7 165 4 3 1 1 1]
[ 15 104 0 5 3 1 1 1]], shape=(2, 8), dtype=int32)
Y的有效长度: tf.Tensor([5 5], shape=(2,), dtype=int32)
.. raw:: html
.. raw:: html
.. raw:: latex
\diilbookstyleinputcell
.. code:: python
train_iter, src_vocab, tgt_vocab = load_data_nmt(batch_size=2, num_steps=8)
for X, X_valid_len, Y, Y_valid_len in train_iter:
print('X:', X.astype(paddle.int32))
print('X的有效长度:', X_valid_len)
print('Y:', Y.astype(paddle.int32))
print('Y的有效长度:', Y_valid_len)
break
.. raw:: latex
\diilbookstyleoutputcell
.. parsed-literal::
:class: output
X: Tensor(shape=[2, 8], dtype=int32, place=Place(cpu), stop_gradient=True,
[[16 , 51 , 5 , 3 , 1 , 1 , 1 , 1 ],
[183, 10 , 11 , 3 , 1 , 1 , 1 , 1 ]])
X的有效长度: Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
[[4],
[4]])
Y: Tensor(shape=[2, 8], dtype=int32, place=Place(cpu), stop_gradient=True,
[[35 , 5 , 3 , 1 , 1 , 1 , 1 , 1 ],
[197, 16 , 8 , 9 , 3 , 1 , 1 , 1 ]])
Y的有效长度: Tensor(shape=[2, 1], dtype=int64, place=Place(cpu), stop_gradient=True,
[[3],
[5]])
.. raw:: html
.. raw:: html
小结
----
- 机器翻译指的是将文本序列从一种语言自动翻译成另一种语言。
- 使用单词级词元化时的词表大小,将明显大于使用字符级词元化时的词表大小。为了缓解这一问题,我们可以将低频词元视为相同的未知词元。
- 通过截断和填充文本序列,可以保证所有的文本序列都具有相同的长度,以便以小批量的方式加载。
练习
----
1. 在\ ``load_data_nmt``\ 函数中尝试不同的\ ``num_examples``\ 参数值。这对源语言和目标语言的词表大小有何影响?
2. 某些语言(例如中文和日语)的文本没有单词边界指示符(例如空格)。对于这种情况,单词级词元化仍然是个好主意吗?为什么?
.. raw:: html