/******************************************************************************* * Copyright (c) 2015-2018 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 ******************************************************************************/ // // Created by raver on 8/4/2018. // #include "testlayers.h" #include #include #include #include using namespace nd4j; class DeclarableOpsTests14 : public testing::Test { public: DeclarableOpsTests14() { printf("\n"); fflush(stdout); } }; TEST_F(DeclarableOpsTests14, Test_Validation_Edge_1) { auto x = NDArrayFactory::create('c', {2}, {2, 2}); auto exp = NDArrayFactory::create('c', {2, 2}, Environment::getInstance()->defaultFloatDataType()); exp.assign(4.0f); nd4j::ops::fill op; auto result = op.execute({&x}, {4.0f},{}, {}); ASSERT_EQ(Status::OK(), result->status()); auto z = result->at(0); ASSERT_EQ(exp, *z); delete result; } TEST_F(DeclarableOpsTests14, Test_Reshape_CF_1) { auto x = NDArrayFactory::create('f', {2, 3}, {1.0, 4.0, 2.0, 5.0, 3.0, 6.0}); auto e = NDArrayFactory::create('f', {3, 2}, {1.0, 3.0, 5.0, 2.0, 4.0, 6.0}); x.printShapeInfo("x shape"); x.printBuffer("x buffr"); x.printIndexedBuffer("x indxd"); auto r = x.reshape('c', {3, 2}); r->printIndexedBuffer("r pre-s"); r->streamline('f'); nd4j::ops::reshape op; auto result = op.execute({&x}, {}, {3, 2}, {}); ASSERT_EQ(Status::OK(), result->status()); auto z = result->at(0); delete r; delete result; } TEST_F(DeclarableOpsTests14, Test_Inf_Comparison_1) { auto x = NDArrayFactory::create('c', {5}, {1, 2, 3, std::numeric_limits::infinity(), 5}); auto y = NDArrayFactory::create('c', {5}, {1, 2, 3, std::numeric_limits::infinity(), 5}); ASSERT_EQ(x, y); } TEST_F(DeclarableOpsTests14, Test_Inf_Comparison_2) { auto x = NDArrayFactory::create('c', {5}, {1, 2, 3, std::numeric_limits::infinity(), 5}); auto y = NDArrayFactory::create('c', {5}, {1, 2, 3, -std::numeric_limits::infinity(), 5}); ASSERT_NE(x, y); } TEST_F(DeclarableOpsTests14, Multiply_test) { for(int k=2;k<10;k++){ nd4j_printf("k=%d\n", k); NDArray x = NDArrayFactory::create('c', {k, 1}); NDArray y = NDArrayFactory::create('c', {k}); NDArray e = NDArrayFactory::create('c', {k, k}); x.assign(1.0); y.assign(1.0); e.assign(1.0); nd4j::ops::multiply op; auto result = op.execute({&x, &y}, {}, {}); auto f = result->at(0); NDArray r = *f; ASSERT_EQ(e, r); ASSERT_EQ(e, *f); delete result; } } TEST_F(DeclarableOpsTests14, Test_EvalReductionShape_1) { auto x = NDArrayFactory::create('c', {3}, {5, 3, 4}); auto y = NDArrayFactory::create('c', {1}, {1}); auto e = NDArrayFactory::create('c', {2}, {5, 4}); nd4j::ops::evaluate_reduction_shape op; auto result = op.execute({&x, &y}, {}, {}, {false, false}); ASSERT_EQ(Status::OK(), result->status()); auto z = result->at(0); ASSERT_EQ(e, *z); delete result; } TEST_F(DeclarableOpsTests14, Test_EvalReductionShape_2) { auto x = NDArrayFactory::create('c', {3}, {5, 3, 4}); auto y = NDArrayFactory::create('c', {1}, {1}); auto e = NDArrayFactory::create('c', {3}, {5, 1, 4}); nd4j::ops::evaluate_reduction_shape op; auto result = op.execute({&x, &y}, {}, {}, {true, false}); ASSERT_EQ(Status::OK(), result->status()); auto z = result->at(0); ASSERT_EQ(e, *z); delete result; } TEST_F(DeclarableOpsTests14, Test_Reduce_Min_Small_0) { auto x = NDArrayFactory::create('c', {3, 4}, {-999.f, 0.2236f, 0.7973f, 0.0962f, 0.7231f, 0.3381f, -0.7301f, 0.9115f, -0.5094f, 0.9749f, -2.1340f, 0.6023f}); auto z = NDArrayFactory::create('c', {4}); auto e = NDArrayFactory::create('c', {4}, {-999.f, 0.2236f, -2.1340f, 0.0962f}); nd4j::ops::reduce_min op; op.execute({&x}, {&z}, {}, {0}, {}); //z.printIndexedBuffer("Z"); ASSERT_EQ(e, z); } TEST_F(DeclarableOpsTests14, Test_Reduce_Min_Small_1) { auto x = NDArrayFactory::create('c', {3, 4}, {-999.f, 0.2236f, 0.7973f, 0.0962f, 0.7231f, 0.3381f, -0.7301f, 0.9115f, -0.5094f, 0.9749f, -2.1340f, 0.6023f}); auto z = NDArrayFactory::create('c', {3}); auto e = NDArrayFactory::create('c', {3}, {-999.f, -0.7301f, -2.1340f}); nd4j::ops::reduce_min op; op.execute({&x}, {&z}, {}, {1}, {}); //z.printIndexedBuffer("Z"); ASSERT_EQ(e, z); } TEST_F(DeclarableOpsTests14, Test_Diag_Zeros_1) { auto x = NDArrayFactory::create('c', {2}, {1, 2}); auto z = NDArrayFactory::create('c', {2, 2}, {-119, -119, -119, -119}); auto exp = NDArrayFactory::create('c', {2, 2}, {1, 0, 0, 2}); nd4j::ops::diag op; auto status = op.execute({&x}, {&z}, {}, {}, {}); ASSERT_EQ(Status::OK(), status); ASSERT_EQ(exp, z); } TEST_F(DeclarableOpsTests14, Test_scalar_broadcast_1) { auto x = NDArrayFactory::create(1.0f); auto y = NDArrayFactory::create('c', {5, 10}); auto e = NDArrayFactory::create('c', {5, 10}); e.assign(1.0); nd4j::ops::add op; auto result = op.execute({&x, &y}, {}, {}); ASSERT_EQ(Status::OK(), result->status()); ASSERT_EQ(e, *result->at(0)); delete result; } TEST_F(DeclarableOpsTests14, Test_scalar_broadcast_2) { auto x = NDArrayFactory::create(1.0f); auto y = NDArrayFactory::create('c', {5, 10}); auto e = NDArrayFactory::create('c', {5, 10}); y.assign(2.0f); e.assign(-1.0f); nd4j::ops::subtract op; auto result = op.execute({&x, &y}, {}, {}); ASSERT_EQ(Status::OK(), result->status()); ASSERT_EQ(e, *result->at(0)); delete result; }