def test_kernel_ker2_def_ort(self):
     ker = Sum(
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                    length_scale_bounds=(1e-3, 1e3)),
         CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                    length_scale_bounds=(1e-3, 1e3)))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                          op_version=get_opset_number_from_onnx())
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))],
         target_opset=get_opset_number_from_onnx())
     model_onnx.ir_version = get_ir_version_from_onnx()
     sess = _capture_output(
         lambda: OnnxInference(model_onnx.SerializeToString(),
                               runtime="onnxruntime2"), 'c')[0]
     try:
         res = sess.run({'X': Xtest_.astype(numpy.float32)})
     except RuntimeError as e:
         if "Got invalid dimensions for input" in str(e):
             # probable bug somewhere
             return
         raise e
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2, decimal=5)
 def test_kernel_ker12_def(self):
     ker = (Sum(CK(0.1, (1e-3, 1e3)), CK(0.1, (1e-3, 1e3)) *
                RBF(length_scale=1, length_scale_bounds=(1e-3, 1e3))))
     onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
     model_onnx = onx.to_onnx(
         inputs=[('X', FloatTensorType([None, None]))],
         outputs=[('Y', FloatTensorType([None, None]))])
     sess = OnnxInference(model_onnx.SerializeToString())
     res = sess.run({'X': Xtest_.astype(numpy.float32)})
     m1 = res['Y']
     m2 = ker(Xtest_)
     self.assertEqualArray(m1, m2)
    def test_kernel_ker2_def_ort1(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32)
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))])
        sess = OnnxInference(model_onnx.SerializeToString(),
                             runtime="onnxruntime1")

        rows = []

        def myprint(*args, **kwargs):
            rows.append(" ".join(map(str, args)))

        res = sess.run({'X': Xtest_.astype(numpy.float32)},
                       intermediate=True, verbose=1, fLOG=myprint)
        self.assertGreater(len(rows), 2)
        m1 = res['Y']
        self.assertNotEmpty(m1)
        self.assertGreater(len(res), 2)
        # m2 = ker(Xtest_)
        # self.assertEqualArray(m1, m2, decimal=5)

        cpu = OnnxInference(model_onnx.SerializeToString())
        sbs = side_by_side_by_values(
            [cpu, sess], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertGreater(len(sbs), 2)
        self.assertIsInstance(sbs, list)
        self.assertIsInstance(sbs[0], dict)
        self.assertIn('step', sbs[0])
        self.assertIn('step', sbs[1])
        self.assertIn('metric', sbs[0])
        self.assertIn('metric', sbs[1])
        self.assertIn('cmp', sbs[0])
        self.assertIn('cmp', sbs[1])

        sess3 = OnnxInference(model_onnx.SerializeToString(),
                              runtime="onnxruntime2")
        sbs = side_by_side_by_values(
            [cpu, sess, sess3], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertNotEmpty(sbs)

        inputs = {'X': Xtest_.astype(numpy.float32)}
        sbs = side_by_side_by_values(
            [(cpu, inputs), (sess, inputs), (sess3, inputs)])
        self.assertNotEmpty(sbs)
    def test_kernel_ker2_def(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                             op_version=get_opset_number_from_onnx())
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))],
            target_opset=get_opset_number_from_onnx())
        sess = OnnxInference(model_onnx.SerializeToString())

        res = sess.run({'X': Xtest_.astype(numpy.float32)})
        m1 = res['Y']
        m2 = ker(Xtest_)
        self.assertEqualArray(m1, m2)

        res = sess.run({'X': Xtest_.astype(numpy.float32)}, intermediate=True)
        self.assertGreater(len(res), 30)
        self.assertIsInstance(res, dict)
示例#5
0
# - アルファ
#   --- ノイズパラメータ
#   --- 全ての観測値にノイズ値を割り当てるか、Numpy配列形式でn個の値を割り当てる
#   --- nは訓練データの目的変数の値の長さ
# - kernel
#   --- 関数を近似するカーネル
#   --- ここでは定数カーネルとRBFカーネルから柔軟なカーネルを構築
# - normalize_y
#   --- ターゲットとなるデータセットの平均が0出ない場合はTrueに設定することができる
#   --- Falseのままにしておいても十分にうまくいく
# - n_restarts_optimizer
#   --- カーネルを最適化するためのイテレーション回数
#   --- 実際に使用する場合は10-20に設定

# カーネル関数の設定
mixed_kernel = kernel = CK(1, (1e-4, 1e4)) * RBF(10, (1e-4, 1e4))

# インスタンスの生成
gpr = GaussianProcessRegressor(alpha=5,
                               n_restarts_optimizer=20,
                               kernel=mixed_kernel)
gpr

# 学習
gpr.fit(boston_X[train_set], boston_y[train_set])

# 予測
test_preds = gpr.predict(boston_X[~train_set])

# 2 結果のプロット ---------------------------------------------------------------------------------
    def test_kernel_ker2_def_ort1(self):
        ker = Sum(
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=10,
                                       length_scale_bounds=(1e-3, 1e3)),
            CK(0.1, (1e-3, 1e3)) * RBF(length_scale=1,
                                       length_scale_bounds=(1e-3, 1e3))
        )
        onx = convert_kernel(ker, 'X', output_names=['Y'], dtype=numpy.float32,
                             op_version=get_opset_number_from_onnx())
        model_onnx = onx.to_onnx(
            inputs=[('X', FloatTensorType([None, None]))],
            outputs=[('Y', FloatTensorType([None, None]))],
            target_opset=get_opset_number_from_onnx())
        model_onnx.ir_version = get_ir_version_from_onnx()
        sess = OnnxInference(model_onnx.SerializeToString(),
                             runtime="onnxruntime1")

        rows = []

        def myprint(*args, **kwargs):
            rows.append(" ".join(map(str, args)))

        res = _capture_output(
            lambda: sess.run({'X': Xtest_.astype(numpy.float32)},
                             intermediate=True, verbose=1, fLOG=myprint),
            'c')[0]
        self.assertGreater(len(rows), 2)
        m1 = res['Y']
        self.assertNotEmpty(m1)
        self.assertGreater(len(res), 2)
        # m2 = ker(Xtest_)
        # self.assertEqualArray(m1, m2, decimal=5)

        cpu = OnnxInference(model_onnx.SerializeToString())
        sbs = side_by_side_by_values(
            [cpu, sess], inputs={'X': Xtest_.astype(numpy.float32)})
        self.assertGreater(len(sbs), 2)
        self.assertIsInstance(sbs, list)
        self.assertIsInstance(sbs[0], dict)
        self.assertIn('step', sbs[0])
        self.assertIn('step', sbs[1])
        self.assertIn('metric', sbs[0])
        self.assertIn('metric', sbs[1])
        self.assertIn('cmp', sbs[0])
        self.assertIn('cmp', sbs[1])

        sess3 = _capture_output(
            lambda: OnnxInference(model_onnx.SerializeToString(),
                                  runtime="onnxruntime2"), 'c')[0]
        try:
            sbs = side_by_side_by_values(
                [cpu, sess, sess3], inputs={'X': Xtest_.astype(numpy.float32)})
        except RuntimeError as e:
            if "Got invalid dimensions for input" in str(e):
                # probable bug somewhere
                return
            raise e
        self.assertNotEmpty(sbs)

        inputs = {'X': Xtest_.astype(numpy.float32)}
        sbs = side_by_side_by_values(
            [(cpu, inputs), (sess, inputs), (sess3, inputs)])
        self.assertNotEmpty(sbs)
示例#7
0
#print "Average number of users: " + str(int(mean(nou)))
#print "Standard deviation: " + str(int(np.sqrt(sample_var(nou, mean(nou)))))

training_data = [nou, bits, pktNum, sigS, dataRate, phyB, phyG, phyN]
test_data = [nou_tst, bits_tst, pkt_tst, sigS_tst, dR_tst, b_tst, g_tst, n_tst]
labels = ["Number of users", "Bits", "Number of Packets", "Signal Strength",\
          "Data Rate(MB)", "802.11b bits", "802.11g bits", "802.11n bits"]
#
#Number of test samples
p = 0
r = 100
#Window size
D = 15

kernel1 = LK(sigma_0=1, sigma_0_bounds=(1e-1, 1e1))
kernel2 = CK(constant_value=1)
kernel3 = WK(0.1)
kernel = Sum(kernel1, kernel2)
kernel = Sum(kernel, kernel3)
#1e-1 for linear + constant, 1e-3 for RBF
gp = GaussianProcessRegressor(kernel=kernel, n_restarts_optimizer=10,\
                              normalize_y=False, alpha=1e-1)
#print gp.get_params()['kernel']

for z in range(len(labels)):  #len(labels)
    total_samp, Xtr, Ytr, Xtst, Ycomp, Ytst = GP_prep(training_data[z],
                                                      test_data[z], 60, p, r,
                                                      D)

    print(Xtr.shape, Ytr.shape, Xtst.shape, Ytst.shape)
    #Testing if it overfits