コード例 #1
0
 def test_tf(self):
     tf_true = tf.constant([[0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]])
     tf_pred = tf.constant([[0.1, 0.9, 0.05, 0.05], [0.1, 0.2, 0.0, 0.7], [0.0, 0.15, 0.8, 0.05],
                            [1.0, 0.0, 0.0, 0.0]])
     mse = MeanSquaredError(inputs='x', outputs='x')
     output = mse.forward(data=[tf_pred, tf_true], state={})
     self.assertTrue(np.allclose(output.numpy(), 0.014375001))
コード例 #2
0
 def test_torch(self):
     torch_true = torch.tensor([[0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0]])
     torch_pred = torch.tensor([[0.1, 0.9, 0.05, 0.05], [0.1, 0.2, 0.0, 0.7], [0.0, 0.15, 0.8, 0.05],
                                [1.0, 0.0, 0.0, 0.0]])
     mse = MeanSquaredError(inputs='x', outputs='x')
     output = mse.forward(data=[torch_pred, torch_true], state={})
     self.assertTrue(np.allclose(output.detach().numpy(), 0.014375001))
コード例 #3
0
    def test_network_transform_one_layer_model_torch(self):
        model = fe.build(model_fn=OneLayerTorchModel, optimizer_fn="adam")
        weight = get_torch_one_layer_model_weight(model)
        network = fe.Network(ops=[
            ModelOp(model=model, inputs="x", outputs="y_pred"),
            MeanSquaredError(inputs=("y_pred", "y"), outputs="ce"),
            UpdateOp(model=model, loss_name="ce")
        ],
                             pops=PlusOneNumpyOp(inputs="y_pred",
                                                 outputs="y_pred_processed"))
        batch = {
            "x": np.array([[
                1,
                1,
                1,
            ]], dtype=np.float32),
            "y": np.array([[1]], dtype=np.float32)
        }
        batch = network.transform(data=batch, mode="train")

        with self.subTest("output y_pred check"):
            ans = np.array([[6]], dtype=np.float32)  # 1*1 + 1*2 + 1*3
            self.assertTrue(np.array_equal(batch["y_pred"].numpy(), ans))

        with self.subTest("postprocessing y_pred check"):
            ans = np.array([[7]], dtype=np.float32)  # 1*1 + 1*2 + 1*3 + 1
            self.assertTrue(np.array_equal(batch["y_pred_processed"], ans))

        with self.subTest("output ce check"):
            self.assertEqual(batch["ce"].numpy(), 25)  # (6-1)^2

        with self.subTest("check whether model weight changed"):
            weight2 = get_torch_one_layer_model_weight(model)
            self.assertFalse(is_equal(weight, weight2))