-
Notifications
You must be signed in to change notification settings - Fork 5.9k
add log2 operator #28319
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
add log2 operator #28319
Changes from 9 commits
f6d9404
f1b4f73
cca2f9a
3049700
665f827
eebde3d
933dd5d
02fcf16
cd94a3a
d1838bf
b72f42c
e5a5c26
8ae9d2c
bfc4d79
e08c326
9eadc71
faedba2
f0151d0
bb143ad
6ca56f2
c8b5fcf
23e94c0
4b35414
d508c4c
9a6d1bb
c84a99f
c7023f9
816a086
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 |
|---|---|---|
|
|
@@ -111,6 +111,7 @@ | |
| 'relu', | ||
| 'selu', | ||
| 'log', | ||
| 'log2', | ||
| 'crop', | ||
| 'crop_tensor', | ||
| 'elu', | ||
|
|
@@ -8731,6 +8732,59 @@ def log(x, name=None): | |
| return out | ||
|
|
||
|
|
||
| def log2(x, name=None): | ||
|
||
| """ | ||
| :alias_main: paddle.log2 | ||
| :alias: paddle.log2,paddle.tensor.log2,paddle.tensor.math.log2 | ||
| :old_api: paddle.fluid.layers.log2 | ||
|
||
|
|
||
| Calculates the log to the base 2 of the given input tensor, element-wise. | ||
|
|
||
| .. math:: | ||
|
|
||
| Out = \\ln(x)/ln2 | ||
|
|
||
| Args: | ||
| x (Variable): Input LoDTensor or Tensor. Must be one of the following types: float32, float64. | ||
|
||
| name (str|None): The default value is None. Normally there is no need for user to set this property. For more information, please refer to :ref:`api_guide_Name` | ||
|
|
||
|
|
||
| Returns: | ||
| Variable: The log to the base 2 of the input LoDTensor or Tensor computed element-wise. | ||
|
||
|
|
||
| Examples: | ||
|
|
||
| .. code-block:: python | ||
| import numpy as np | ||
| import paddle | ||
| import paddle.fluid as fluid | ||
|
||
|
|
||
| paddle.enable_static() | ||
|
|
||
| # Graph Organizing | ||
| x = fluid.layers.data(name="x", shape=[1], dtype="float32") | ||
| res = fluid.layers.log2(x) | ||
|
|
||
| # Create an executor using CPU as an example | ||
| exe = fluid.Executor(fluid.CPUPlace()) | ||
|
|
||
| # Execute | ||
| x_i = np.array([[1], [2]]).astype(np.float32) | ||
|
||
| res_val, = exe.run(fluid.default_main_program(), feed={'x':x_i}, fetch_list=[res]) | ||
|
||
| print(res_val) # [[0.], [0.6931472]] | ||
| """ | ||
| if in_dygraph_mode(): | ||
| return core.ops.log2(x) | ||
|
|
||
| check_variable_and_dtype(x, 'x', ['float32', 'float64'], "log") | ||
| inputs = {'X': [x]} | ||
| helper = LayerHelper('log2', **locals()) | ||
| dtype = helper.input_dtype(input_param_name='x') | ||
| out = helper.create_variable_for_type_inference(dtype) | ||
| helper.append_op(type="log2", inputs={"X": x}, outputs={"Out": out}) | ||
| return out | ||
|
|
||
|
|
||
| @deprecated(since="2.0.0", update_to="paddle.nn.functional.relu") | ||
| def relu(x, name=None): | ||
| """ | ||
|
|
||
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.
尽管数学上等价,但是计算机应该算以log2为底会更简单快速。比算log(x)/log(2)快。如果有空可以自己写写看这里有没有更快的实现。
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.
这里看看有空调查和实现一下,没空时现在这样也能勉强接受。。
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.
之后实现,谢谢
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.
这个问题,除了性能,还有计算误差的问题,建议再调研下,看是否能优化。
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.
换成tensor原生实现,thx
done;