-
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
Add elementwise math operations #343
Merged
luotao1
merged 4 commits into
PaddlePaddle:develop
from
emailweixu:elementwise_operation
Nov 4, 2016
Merged
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* Copyright (c) 2016 Baidu, Inc. All Rights Reserve. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. */ | ||
|
||
#include <gtest/gtest.h> | ||
#include <vector> | ||
#include <string> | ||
#include "paddle/gserver/layers/DataLayer.h" | ||
#include "ModelConfig.pb.h" | ||
#include "paddle/trainer/Trainer.h" | ||
|
||
#include "TestUtil.h" | ||
#include "LayerGradUtil.h" | ||
|
||
using namespace paddle; // NOLINT | ||
using namespace std; // NOLINT | ||
|
||
P_DECLARE_bool(use_gpu); | ||
P_DECLARE_bool(thread_local_rand_use_global_seed); | ||
|
||
void testActivation(const string& act) { | ||
LOG(INFO) << "test activation: " << act; | ||
size_t size = 10; | ||
TestConfig config; | ||
config.biasSize = 0; | ||
config.layerConfig.set_type("addto"); | ||
config.layerConfig.set_size(size); | ||
config.layerConfig.set_active_type(act); | ||
config.inputDefs.push_back({INPUT_DATA, "layer_0", size, 0}); | ||
config.layerConfig.add_inputs(); | ||
for (auto useGpu : {false, true}) { | ||
testLayerGrad(config, | ||
act + "_activation", | ||
100, | ||
/* trans= */false, | ||
useGpu, | ||
/* useWeight */true); | ||
} | ||
} | ||
|
||
TEST(Activation, activation) { | ||
auto types = ActivationFunction::getAllRegisteredTypes(); | ||
std::set<string> excluded{"sequence_softmax"}; | ||
for (auto type : types) { | ||
if (excluded.count(type)) continue; | ||
testActivation(type); | ||
} | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
testing::InitGoogleTest(&argc, argv); | ||
initMain(argc, argv); | ||
FLAGS_thread_local_rand_use_global_seed = true; | ||
srand(1); | ||
return RUN_ALL_TESTS(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# Copyright (c) 2016 Baidu, Inc. All Rights Reserved | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .layers import LayerOutput, mixed_layer, identity_projection, \ | ||
slope_intercept_layer | ||
from .attrs import is_compatible_with | ||
from .default_decorators import * | ||
import activations as act | ||
|
||
__all__ = [] | ||
|
||
def register_unary_math_op(op_name, act): | ||
def op(input, name=None): | ||
return mixed_layer(input=[identity_projection(input=input)], | ||
name=name, | ||
act=act) | ||
op = wrap_name_default(op_name)(op) | ||
op.__doc__ = type(act).__doc__ | ||
globals()[op_name]= op | ||
__all__.append(op_name) | ||
|
||
# register_unary_math_op('log', act.LogActivation()) | ||
register_unary_math_op('exp', act.ExpActivation()) | ||
register_unary_math_op('log', act.ExpActivation()) | ||
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. line 35 is |
||
register_unary_math_op('abs', act.AbsActivation()) | ||
register_unary_math_op('sigmoid', act.SigmoidActivation()) | ||
register_unary_math_op('tanh', act.TanhActivation()) | ||
register_unary_math_op('square', act.SquareActivation()) | ||
|
||
def add(layeroutput, other): | ||
if is_compatible_with(other, float): | ||
return slope_intercept_layer(input=layeroutput, intercept=other) | ||
assert isinstance(other, LayerOutput) | ||
return mixed_layer(input=[identity_projection(input=layeroutput), | ||
identity_projection(input=other)]) | ||
|
||
LayerOutput.__radd__ = add | ||
LayerOutput.__add__ = add | ||
|
||
def sub(layeroutput, other): | ||
if is_compatible_with(other, float): | ||
return slope_intercept_layer(input=layeroutput, intercept=other) | ||
assert isinstance(other, LayerOutput) | ||
neg = slope_intercept_layer(input=other, slope=-1.0) | ||
return mixed_layer(input=[identity_projection(input=layeroutput), | ||
identity_projection(input=neg)]) | ||
|
||
LayerOutput.__sub__ = sub | ||
|
||
def rsub(layeroutput, other): | ||
neg = slope_intercept_layer(input=layeroutput, slope=-1.0) | ||
return add(neg, other) | ||
|
||
LayerOutput.__rsub__ = rsub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
python/paddle/trainer_config_helpers/tests/configs/math_ops.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from paddle.trainer_config_helpers import * | ||
from paddle.trainer_config_helpers import math | ||
|
||
settings( | ||
batch_size=1000, | ||
learning_rate=1e-5 | ||
) | ||
|
||
x = data_layer(name='data', size=100) | ||
x = math.exp(x) | ||
x = math.log(x) | ||
x = math.abs(x) | ||
x = math.sigmoid(x) | ||
x = math.square(x) | ||
x = math.square(x) | ||
y = 1 + x | ||
y = y + 1 | ||
y = x + y | ||
y = y - x | ||
y = y - 2 | ||
y = 2 - y | ||
|
||
outputs(y) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
line 205 is Log Activation