Files
ReAgent/reagent/test/preprocessing/test_postprocessing.py
Pradeep Kumar Srinivasan 15ec3d92f1 Enable Pyre's source-db buck builder and auto-suppress errors - batch 8.
Reviewed By: grievejia

Differential Revision: D24655770

fbshipit-source-id: 1fec713e37a8edc4ef0a9855f55c52bcf28eb275
2020-11-09 14:56:16 -08:00

47 lines
1.7 KiB
Python

#!/usr/bin/env python3
# Copyright (c) Facebook, Inc. and its affiliates. All rights reserved.
import unittest
import numpy.testing as npt
import torch
from reagent.preprocessing.identify_types import CONTINUOUS_ACTION, DO_NOT_PREPROCESS
from reagent.preprocessing.normalization import (
NormalizationData,
NormalizationParameters,
)
from reagent.preprocessing.postprocessor import Postprocessor
from reagent.preprocessing.preprocessor import Preprocessor
class TestPostprocessing(unittest.TestCase):
def test_continuous_action(self):
normalization_params = {
i: NormalizationParameters(
feature_type=CONTINUOUS_ACTION, min_value=-5.0 * i, max_value=10.0 * i
)
for i in range(1, 5)
}
preprocessor = Preprocessor(normalization_params, use_gpu=False)
postprocessor = Postprocessor(normalization_params, use_gpu=False)
x = torch.rand(3, 4) * torch.tensor([15, 30, 45, 60]) + torch.tensor(
[-5, -10, -15, -20]
)
presence = torch.ones_like(x, dtype=torch.uint8)
y = postprocessor(preprocessor(x, presence))
npt.assert_allclose(x, y, rtol=1e-4)
def test_do_not_preprocess(self):
normalization_params = {
i: NormalizationParameters(feature_type=DO_NOT_PREPROCESS)
for i in range(1, 5)
}
preprocessor = Preprocessor(normalization_params, use_gpu=False)
postprocessor = Postprocessor(normalization_params, use_gpu=False)
x = torch.randn(3, 4)
presence = torch.ones_like(x, dtype=torch.uint8)
y = postprocessor(preprocessor(x, presence))
npt.assert_allclose(x, y)