2019-06-06 14:21:15 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2015-2019 Skymind, Inc.
|
|
|
|
*
|
|
|
|
* This program and the accompanying materials are made available under the
|
|
|
|
* terms of the Apache License, Version 2.0 which is available at
|
|
|
|
* https://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.
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
//
|
|
|
|
// @author Paul Dubs
|
|
|
|
//
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
#include <system/op_boilerplate.h>
|
2019-06-06 14:21:15 +02:00
|
|
|
#if NOT_EXCLUDED(OP_standardize)
|
|
|
|
|
|
|
|
#include <ops/declarable/CustomOperations.h>
|
|
|
|
#include <ops/declarable/helpers/reverse.h>
|
|
|
|
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
namespace sd {
|
2019-06-06 14:21:15 +02:00
|
|
|
namespace ops {
|
|
|
|
|
|
|
|
CONFIGURABLE_OP_IMPL(standardize, 1, 1, true, 0, -2) {
|
2019-12-20 20:35:39 +01:00
|
|
|
|
2019-06-06 14:21:15 +02:00
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
std::vector<int> axis;
|
|
|
|
|
|
|
|
if (block.width() > 1)
|
2019-06-06 14:21:15 +02:00
|
|
|
axis = INPUT_VARIABLE(1)->template asVectorT<int>();
|
2019-12-20 20:35:39 +01:00
|
|
|
else if (block.numI() > 0)
|
|
|
|
axis = *block.getIArguments();
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
REQUIRE_TRUE(!axis.empty(), 0, "STANDARDIZE OP: axis has to be non-empty")
|
|
|
|
|
|
|
|
shape::checkDimensions(input->rankOf(), axis);
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
auto means = input->reduceAlongDimension(reduce::Mean, axis, true);
|
|
|
|
auto stdev = input->varianceAlongDimension(variance::SummaryStatsStandardDeviation, false, axis);
|
2019-06-06 14:21:15 +02:00
|
|
|
stdev.reshapei(means.getShapeAsVector());
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
input->applyTrueBroadcast(sd::BroadcastOpsTuple::Subtract(), means, *output, false);
|
|
|
|
output->applyTrueBroadcast(sd::BroadcastOpsTuple::Divide(), stdev, *output, false);
|
|
|
|
output->applyScalar(sd::scalar::ReplaceNans, 0, *output);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
DECLARE_TYPES(standardize) {
|
|
|
|
getOpDescriptor()->setAllowedInputTypes(0, {ALL_FLOATS});
|
|
|
|
getOpDescriptor()->setAllowedInputTypes(1, {DataType::INT32, DataType::INT64});
|
|
|
|
getOpDescriptor()->setAllowedOutputTypes(0, DataType::INHERIT);
|
|
|
|
}
|
|
|
|
|
|
|
|
CUSTOM_OP_IMPL(standardize_bp, 2, 1, false, 0, -2) {
|
|
|
|
auto input = INPUT_VARIABLE(0);
|
|
|
|
auto eps = block.width() == 3 ? INPUT_VARIABLE(2) : INPUT_VARIABLE(1);
|
|
|
|
|
|
|
|
auto output = OUTPUT_VARIABLE(0);
|
|
|
|
std::vector<int> axis;
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
if (block.width() == 3)
|
2019-06-06 14:21:15 +02:00
|
|
|
axis = INPUT_VARIABLE(1)->template asVectorT<int>();
|
2019-12-20 20:35:39 +01:00
|
|
|
else if (block.numI() > 0)
|
2019-06-06 14:21:15 +02:00
|
|
|
axis = *block.getIArguments();
|
|
|
|
|
|
|
|
REQUIRE_TRUE(!axis.empty(), 0, "STANDARDIZE OP: axis has to be non-empty")
|
|
|
|
|
|
|
|
|
|
|
|
shape::checkDimensions(input->rankOf(), axis);
|
|
|
|
auto longAxis = ArrayUtils::toLongVector(axis);
|
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
auto means = input->reduceAlongDimension(reduce::Mean, axis, true);
|
|
|
|
auto stdev = input->varianceAlongDimension(variance::SummaryStatsStandardDeviation, false, axis);
|
2019-06-06 14:21:15 +02:00
|
|
|
stdev.reshapei(means.getShapeAsVector());
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
eps->applyTrueBroadcast(sd::BroadcastOpsTuple::Divide(), stdev, *output, false);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
NDArray dldu_sum = -output->reduceAlongDimension(reduce::Sum, axis, true);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
NDArray dldx_u(input->shapeInfo(), false, block.launchContext());
|
|
|
|
std::vector<NDArray*> meanBpArgs = {input, &dldu_sum};
|
|
|
|
std::vector<NDArray*> meanBpOutput = {&dldx_u};
|
|
|
|
std::vector<double> meanBpTArgs = {};
|
|
|
|
std::vector<bool> meanBpBArgs = {};
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::ops::reduce_mean_bp meanBp;
|
2019-06-06 14:21:15 +02:00
|
|
|
meanBp.execute(meanBpArgs, meanBpOutput, meanBpTArgs, longAxis, meanBpBArgs);
|
|
|
|
*output += dldx_u;
|
|
|
|
|
|
|
|
// (eps * (means - input) / (stdev * stdev))
|
|
|
|
NDArray tmp(eps->shapeInfo(), false, block.launchContext());
|
2020-03-02 10:49:41 +01:00
|
|
|
means.applyTrueBroadcast(sd::BroadcastOpsTuple::Subtract(), *input, tmp, false);
|
|
|
|
tmp.applyPairwiseTransform(sd::pairwise::Multiply, *eps, tmp);
|
|
|
|
stdev.applyPairwiseTransform(sd::pairwise::Multiply, stdev, stdev);
|
|
|
|
tmp.applyTrueBroadcast(sd::BroadcastOpsTuple::Divide(), stdev, tmp, false);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
2019-12-20 20:35:39 +01:00
|
|
|
auto dlds_sum = tmp.reduceAlongDimension(reduce::Sum, axis, true);
|
2019-06-06 14:21:15 +02:00
|
|
|
NDArray dldx_s(input->shapeInfo(), false, block.launchContext());
|
|
|
|
std::vector<NDArray*> stdevBpArgs = {input, &dlds_sum};
|
|
|
|
std::vector<NDArray*> stdevBpOutput = {&dldx_s};
|
|
|
|
std::vector<double> stdevBpTArgs = {};
|
|
|
|
std::vector<bool> stdevBpBArgs = {};
|
2020-03-02 10:49:41 +01:00
|
|
|
sd::ops::reduce_stdev_bp stdevBp;
|
2019-06-06 14:21:15 +02:00
|
|
|
stdevBp.execute(stdevBpArgs, stdevBpOutput, stdevBpTArgs, longAxis, stdevBpBArgs);
|
|
|
|
*output += dldx_s;
|
|
|
|
|
2020-03-02 10:49:41 +01:00
|
|
|
output->applyScalar(sd::scalar::ReplaceNans, 0, *output);
|
2019-06-06 14:21:15 +02:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_TYPES(standardize_bp) {
|
|
|
|
getOpDescriptor()
|
2020-03-02 10:49:41 +01:00
|
|
|
->setAllowedInputTypes(sd::DataType::ANY)
|
2019-06-06 14:21:15 +02:00
|
|
|
->setAllowedOutputTypes({ALL_FLOATS});
|
|
|
|
}
|
|
|
|
|
|
|
|
DECLARE_SHAPE_FN(standardize_bp) {
|
|
|
|
auto in = inputShape->at(0);
|
|
|
|
Nd4jLong *out;
|
|
|
|
COPY_SHAPE(in, out);
|
|
|
|
|
|
|
|
return SHAPELIST(CONSTANT(out));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|