Skip to content

Commit

Permalink
fix fluid doc, test=document_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
seiriosPlus committed Oct 12, 2020
1 parent 3f58caa commit f142e5a
Showing 1 changed file with 44 additions and 5 deletions.
49 changes: 44 additions & 5 deletions python/paddle/fluid/input.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,17 +232,33 @@ def embedding(input,
weight_attr=paddle.nn.initializer.Constant(value=1.0))
adam = paddle.optimizer.SGD(parameters=[embedding.weight], learning_rate=0.01)
output = embedding(x)
output=paddle.mean(output)
m_output=paddle.mean(output)
adam.minimize(output)
adam.minimize(m_output)
place = paddle.CPUPlace()
exe = paddle.static.Executor(place)
exe.run(paddle.static.default_startup_program())
x = np.array([[7, 2, 4, 5],[4, 3, 2, 9]], dtype=np.int64)
out, weight = exe.run(paddle.static.default_main_program(), feed={'x':x}, fetch_list=[output, embedding.weight])
# x is a Numpy.
# x.data = [[7, 2, 4, 5], [4, 3, 2, 9]]
# x.shape = [2, 4]
out, = exe.run(paddle.static.default_main_program(), feed={'x':x}, fetch_list=[output])
# out is a Numpy.
# out.data = [[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.]],
#
# [[1., 1., 1.],
# [1., 1., 1.],
# [1., 1., 1.],
# [0., 0., 0.]]]
# out.shape = [2, 4, 3]
Dygraph Examples:
Expand All @@ -251,23 +267,46 @@ def embedding(input,
import paddle
import numpy as np
paddle.disable_static()
x_data = np.arange(3, 6).reshape((3, 1)).astype(np.int64)
y_data = np.arange(6, 12).reshape((3, 2)).astype(np.float32)
# x is a Tensor.
# x.data = [[3], [4], [5]]
# x.shape = [3, 1]
x = paddle.to_tensor(x_data, stop_gradient=False)
y = paddle.to_tensor(y_data, stop_gradient=False)
# embedding weight shape = [10, 3]
embedding = paddle.nn.Embedding(10, 3, sparse=True)
# embedding weight data = [10, 3]
w0 = np.full(shape=(10, 3), fill_value=2).astype(np.float32)
# embedding.weight.shape = [10, 3]
# embedding.weight.data =
# [[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.]]
embedding.weight.set_value(w0)
adam = paddle.optimizer.Adam(
parameters=[embedding.weight], learning_rate=0.01)
adam.clear_grad()
# out is Tensor
# out.shape: [3, 1, 3]
# out.layout: NCHW
# out.dtype: float
# out.data: [2 2 2 2 2 2 2 2 2]
out = embedding(x)
out.backward()
adam.step()
Expand Down

1 comment on commit f142e5a

@paddle-bot-old
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulation! Your pull request passed all required CI. You could ask reviewer(s) to approve and merge. 🎉

Please sign in to comment.