Skip to content

Commit

Permalink
Match clamping between Pytorch and c2 preprocessing
Browse files Browse the repository at this point in the history
Summary: title

Reviewed By: kittipatv

Differential Revision: D13006446

fbshipit-source-id: ad79caab4bfd05475d56d9565f4f891ececd503d
  • Loading branch information
econti authored and facebook-github-bot committed Nov 9, 2018
1 parent 377786d commit 0e7fe9d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
3 changes: 3 additions & 0 deletions ml/rl/preprocessing/normalization.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
MINIMUM_SAMPLES_TO_IDENTIFY = 20
DEFAULT_MAX_QUANTILE_SIZE = 20
DEFAULT_NUM_SAMPLES = 100000
MAX_FEATURE_VALUE = 6.0
MIN_FEATURE_VALUE = MAX_FEATURE_VALUE * -1
EPS = 1e-6


class NumpyEncoder(json.JSONEncoder):
Expand Down
14 changes: 8 additions & 6 deletions ml/rl/preprocessing/preprocessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import torch.nn as nn
from ml.rl import types as rlt
from ml.rl.preprocessing.identify_types import ENUM, FEATURE_TYPES
from ml.rl.preprocessing.normalization import MISSING_VALUE, NormalizationParameters
from ml.rl.preprocessing.normalization import (
EPS,
MAX_FEATURE_VALUE,
MIN_FEATURE_VALUE,
MISSING_VALUE,
NormalizationParameters,
)
from torch.nn import Module, Parameter


MAX_FEATURE_VALUE = 6
MIN_FEATURE_VALUE = MAX_FEATURE_VALUE * -1
EPS = 1e-6

logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -287,7 +289,7 @@ def _preprocess_CONTINUOUS(
if not self.clamp:
return continuous_output
else:
return torch.clamp(continuous_output, -3.0, 3.0)
return torch.clamp(continuous_output, MIN_FEATURE_VALUE, MAX_FEATURE_VALUE)

def _create_parameters_BOXCOX(
self, begin_index: int, norm_params: List[NormalizationParameters]
Expand Down
16 changes: 11 additions & 5 deletions ml/rl/preprocessing/preprocessor_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@
from ml.rl.caffe_utils import C2
from ml.rl.preprocessing import identify_types
from ml.rl.preprocessing.identify_types import FEATURE_TYPES
from ml.rl.preprocessing.normalization import MISSING_VALUE, NormalizationParameters
from ml.rl.preprocessing.normalization import (
EPS,
MAX_FEATURE_VALUE,
MIN_FEATURE_VALUE,
MISSING_VALUE,
NormalizationParameters,
)


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -242,7 +248,7 @@ def preprocess_blob(self, blob, normalization_parameters):
blob = C2.Sub(blob, means_blob, broadcast=1, axis=0)
blob = C2.Div(blob, stddevs_blob, broadcast=1, axis=0)
if self.clip_anomalies:
blob = C2.Clip(blob, min=-6.0, max=6.0)
blob = C2.Clip(blob, min=MIN_FEATURE_VALUE, max=MAX_FEATURE_VALUE)
elif feature_type == identify_types.CONTINUOUS_ACTION:
serving_min_value = np.array(
[norm.min_value for norm in normalization_parameters], dtype=np.float32
Expand All @@ -252,11 +258,11 @@ def preprocess_blob(self, blob, normalization_parameters):
)

training_min_value = (
np.ones(len(normalization_parameters), dtype=np.float32) * -1 + 1e-6
np.ones(len(normalization_parameters), dtype=np.float32) * -1 + EPS
)

scaling_factor = (
(np.ones(len(normalization_parameters), dtype=np.float32) - 1e-6)
(np.ones(len(normalization_parameters), dtype=np.float32) - EPS)
* 2
/ (serving_max_value - serving_min_value)
)
Expand All @@ -275,7 +281,7 @@ def preprocess_blob(self, blob, normalization_parameters):
blob = C2.Mul(blob, scaling_factor_blob, broadcast=1, axis=1)
blob = C2.Add(blob, training_min_blob, broadcast=1, axis=1)
if self.clip_anomalies:
blob = C2.Clip(blob, min=-1 + 1e-6, max=1 - 1e-6)
blob = C2.Clip(blob, min=-1 + EPS, max=1 - EPS)
else:
raise NotImplementedError("Invalid feature type: {}".format(feature_type))

Expand Down

0 comments on commit 0e7fe9d

Please sign in to comment.