-
Notifications
You must be signed in to change notification settings - Fork 5.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
【Hackathon 5th No.14】Add combinations API to Paddle #57792
Changes from 1 commit
156a027
a02bc01
383f9bc
838630f
f530330
767cda5
ad486ff
7b45322
308a4f1
f85ed3f
798736c
d96cb13
ba2f97d
6976300
34c8f7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6953,30 +6953,21 @@ def combinations(x, r=2, with_replacement=False, name=None): | |
.. code-block:: python | ||
|
||
>>> import paddle | ||
|
||
>>> # example1 | ||
>>> x = paddle.to_tensor([1, 2, 3], dtype='float32') | ||
>>> y = paddle.to_tensor([2, 3, 4], dtype='int32') | ||
>>> res = paddle.ldexp(x, y) | ||
>>> x = paddle.to_tensor([1, 2, 3], dtype='int32') | ||
>>> res = paddle.combinations(x) | ||
>>> print(res) | ||
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
[4. , 16., 48.]) | ||
|
||
>>> # example2 | ||
>>> x = paddle.to_tensor([1, 2, 3], dtype='float32') | ||
>>> y = paddle.to_tensor([2], dtype='int32') | ||
>>> res = paddle.ldexp(x, y) | ||
>>> print(res) | ||
Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True, | ||
[4. , 8. , 12.]) | ||
Tensor(shape=[3, 2], dtype=int32, place=Place(gpu:0), stop_gradient=True, | ||
[[1, 2], | ||
[1, 3], | ||
[2, 3]]) | ||
|
||
""" | ||
if len(x.shape) != 1: | ||
raise TypeError(f"Expect a 1-D vector, but got x shape {x.shape}") | ||
if not isinstance(r, int) or r < 0: | ||
raise ValueError(f"Expect a non-negative int, but got r={r}") | ||
|
||
if r == 0: | ||
if r == 0 or r > x.shape[0]: | ||
return paddle.empty([0], dtype=x.dtype) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. empty的形状设计好像有些问题,pytorch在不同情况下的empty的size是不同的。相关设计后续补充到rfc文档中。 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我之前错误理解empty表示输入形状为空的tensor了,已添加empty相关测试 |
||
|
||
if r > 1: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ | |
paddle.enable_static() | ||
|
||
|
||
def convert_combinations_to_array(x, r, with_replacement): | ||
def convert_combinations_to_array(x, r=2, with_replacement=False): | ||
if r == 0: | ||
return np.array([]).astype(x.dtype) | ||
if with_replacement: | ||
|
@@ -114,3 +114,11 @@ def modify_setting(self): | |
self.x_shape = [10] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少了输入为empty情况下的单测 |
||
self.r = 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 缺少r>x_shape情况的单测 |
||
self.with_replacement = True | ||
|
||
|
||
class TestIndexFillAPI3(TestCombinationsAPIBase): | ||
def modify_setting(self): | ||
self.dtype_np = 'float32' | ||
self.x_shape = [0] | ||
self.r = 10 | ||
self.with_replacement = False |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
严格按照模板(包括空行)