未验证 提交 b0ed082e 编写于 作者: 张春乔 提交者: GitHub

[xdoctest] reformat example code with google style in 78 (#55966)

* input.py

* Update python/paddle/nn/functional/input.py

* Update input.py
上级 723c6f77
......@@ -72,16 +72,20 @@ def one_hot(x, num_classes, name=None):
Examples:
.. code-block:: python
import paddle
# Correspond to the first example above, where label.shape is 4 and one_hot_label.shape is [4, 4].
label = paddle.to_tensor([1, 1, 3, 0], dtype='int64')
# label.shape = [4]
one_hot_label = paddle.nn.functional.one_hot(label, num_classes=4)
# one_hot_label.shape = [4, 4]
# one_hot_label = [[0., 1., 0., 0.],
# [0., 1., 0., 0.],
# [0., 0., 0., 1.],
# [1., 0., 0., 0.]]
>>> import paddle
>>> # Correspond to the first example above, where label.shape is 4 and one_hot_label.shape is [4, 4].
>>> label = paddle.to_tensor([1, 1, 3, 0], dtype='int64')
>>> print(label.shape)
[4]
>>> one_hot_label = paddle.nn.functional.one_hot(label, num_classes=4)
>>> print(one_hot_label.shape)
[4, 4]
>>> print(one_hot_label)
Tensor(shape=[4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
[[0., 1., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 0., 1.],
[1., 0., 0., 0.]])
"""
......@@ -166,24 +170,43 @@ def embedding(x, weight, padding_idx=None, sparse=False, name=None):
.. code-block:: python
import paddle
import paddle.nn as nn
x0 = paddle.arange(3, 6).reshape((3, 1)).astype(paddle.int64)
w0 = paddle.full(shape=(10, 3), fill_value=2).astype(paddle.float32)
# x.data = [[3], [4], [5]]
# x.shape = [3, 1]
x = paddle.to_tensor(x0, stop_gradient=False)
# w.data = [[2. 2. 2.] ... [2. 2. 2.]]
# w.shape = [10, 3]
w = paddle.to_tensor(w0, stop_gradient=False)
# emb.data = [[[2., 2., 2.]], [[2., 2., 2.]], [[2., 2., 2.]]]
# emb.shape = [3, 1, 3]
emb = nn.functional.embedding(
x=x, weight=w, sparse=True, name="embedding")
>>> import paddle
>>> import paddle.nn as nn
>>> x0 = paddle.arange(3, 6).reshape((3, 1)).astype(paddle.int64)
>>> w0 = paddle.full(shape=(10, 3), fill_value=2).astype(paddle.float32)
>>> x = paddle.to_tensor(x0, stop_gradient=False)
>>> print(x.numpy())
[[3]
[4]
[5]]
>>> print(x.shape)
[3, 1]
>>> w = paddle.to_tensor(w0, stop_gradient=False)
>>> print(w.numpy())
[[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]
[2. 2. 2.]]
>>> print(w.shape)
[10, 3]
>>> emb = nn.functional.embedding(
... x=x, weight=w, sparse=True, name="embedding")
>>> print(emb.numpy())
[[[2. 2. 2.]]
[[2. 2. 2.]]
[[2. 2. 2.]]]
>>> print(emb.shape)
[3, 1, 3]
"""
padding_idx = (
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册