Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#include "gtest/gtest.h"
#include "core/framework/tensor.h"
#include "core/providers/cpu/ml/ml_common.h"
#include "test/unittest_util/dummy_allocator.h"
#include <random>
 
using namespace onnxruntime;
using namespace onnxruntime::ml;
class WriteScores : public ::testing::Test {
 protected:
  std::random_device r;
  std::default_random_engine rd{r()};
};
 
TEST_F(WriteScores, multiple_scores_transform_none) {
  POST_EVAL_TRANSFORM trans[] = {POST_EVAL_TRANSFORM::NONE,
                                 POST_EVAL_TRANSFORM::LOGISTIC,
                                 POST_EVAL_TRANSFORM::SOFTMAX,
                                 POST_EVAL_TRANSFORM::SOFTMAX_ZERO,
                                 POST_EVAL_TRANSFORM::PROBIT};
  for (POST_EVAL_TRANSFORM tran : trans) {
    InlinedVector<float> v1(100);
    std::uniform_real_distribution<float> uniform_dist(-1, 1);
    std::generate_n(v1.data(), v1.size(), [&]() -> float { return uniform_dist(rd); });
    InlinedVector<float> v2 = v1;
    auto alloc = std::make_shared<test::DummyAllocator>();
    Tensor t(DataTypeImpl::GetType<float>(), {static_cast<int64_t>(v1.size())}, alloc);
    if (tran == POST_EVAL_TRANSFORM::SOFTMAX_ZERO) {
      // random set one number as zero
      v1[3] = 0;
    }
    write_scores<float>(v1, tran, 0, &t, -1);
    const float* output_data = t.Data<float>();
    // verify the result
    if (tran == POST_EVAL_TRANSFORM::NONE) {
      for (size_t i = 0; i != v2.size(); ++i) {
        EXPECT_FLOAT_EQ(v1[i], output_data[i]);
      }
    } else if (tran == POST_EVAL_TRANSFORM::SOFTMAX || tran == POST_EVAL_TRANSFORM::SOFTMAX_ZERO) {
      // sum of the output values should be near to 1
      double sum = 0;
      for (size_t i = 0; i != v1.size(); ++i) {
        sum += output_data[i];
      }
      EXPECT_NEAR(sum, 1, 1e-6);
 
      if (tran == POST_EVAL_TRANSFORM::SOFTMAX_ZERO) {
        EXPECT_FLOAT_EQ(output_data[3], 0);
      }
    }
  }
}
 
TEST_F(WriteScores, single_score_transform_none) {
  InlinedVector<float> v1;
  std::uniform_real_distribution<float> uniform_dist(-100, 100);
  v1.push_back(uniform_dist(rd));
  InlinedVector<float> v2 = v1;
  auto alloc = std::make_shared<test::DummyAllocator>();
  Tensor t(DataTypeImpl::GetType<float>(), {static_cast<int64_t>(v1.size())}, alloc);
  write_scores<float>(v1, POST_EVAL_TRANSFORM::NONE, 0, &t, -1);
  const float* output_data = t.Data<float>();
  for (size_t i = 0; i != v2.size(); ++i) {
    EXPECT_FLOAT_EQ(v2[i], output_data[i]);
  }
}
 
TEST_F(WriteScores, single_score_transform_none_add_second_class) {
  for (int i = 0; i != 4; ++i) {
    InlinedVector<float> v1;
    std::uniform_real_distribution<float> uniform_dist(-100, 100);
    v1.push_back(uniform_dist(rd));
    InlinedVector<float> v2 = v1;
    auto alloc = std::make_shared<test::DummyAllocator>();
    Tensor t(DataTypeImpl::GetType<float>(), {2}, alloc);
    write_scores<float>(v1, POST_EVAL_TRANSFORM::NONE, 0, &t, i);
    const float* output_data = t.Data<float>();
    EXPECT_NEAR(output_data[1] + output_data[0], i == 0 || i == 1 ? 1 : 0, 1e-5);
    if (i == 0 || i == 1) {
      EXPECT_FLOAT_EQ(v2[0], output_data[1]);
      EXPECT_FLOAT_EQ(1 - v2[0], output_data[0]);
    }
  }
}