Layer normalizes the input, cf. https://arxiv.org/pdf/1607.06450.pdf. Args: blob_in: The input blob to layer normalize. blob_out: The layer normalized output blob. dim_in: The dimension of the scale and bias. For example, if blob_in is a 2D design matrix
(
model,
blob_in,
blob_out,
dim_in,
axis=1,
epsilon=1e-4,
initial_scale=1.0,
initial_bias=0.0,
)
| 209 | |
| 210 | |
| 211 | def layer_norm( |
| 212 | model, |
| 213 | blob_in, |
| 214 | blob_out, |
| 215 | dim_in, |
| 216 | axis=1, |
| 217 | epsilon=1e-4, |
| 218 | initial_scale=1.0, |
| 219 | initial_bias=0.0, |
| 220 | ): |
| 221 | ''' |
| 222 | Layer normalizes the input, cf. https://arxiv.org/pdf/1607.06450.pdf. |
| 223 | |
| 224 | Args: |
| 225 | blob_in: The input blob to layer normalize. |
| 226 | blob_out: The layer normalized output blob. |
| 227 | dim_in: The dimension of the scale and bias. For example, if blob_in is |
| 228 | a 2D design matrix and axis is 1, this would be the number of |
| 229 | columns. |
| 230 | axis: (optional) The axis to normalize. Typically the feature axis. |
| 231 | Defaults to 1. |
| 232 | epsilon: (optional) A small value used for numerical stability in |
| 233 | calculation. Defaults to 1e-4. |
| 234 | initial_scale: (optional) The initial value for the learned scale |
| 235 | parameter. Defaults to 1.0 |
| 236 | initial_bias: (optional) The initial value for the learned bias |
| 237 | parameter of the layerwise standard deviation. Defaults to 0.0. |
| 238 | |
| 239 | Returns: |
| 240 | A 3-tuple consisting of: |
| 241 | - The layer normalized input blob. |
| 242 | - The mean of the input blob across the given axis. |
| 243 | - The standard deviation of the input blob acress the given axis. |
| 244 | ''' |
| 245 | |
| 246 | # The learned multiplicative scale or "gain". |
| 247 | scale = model.create_param( |
| 248 | param_name='{}_scale'.format(blob_out), |
| 249 | shape=[dim_in] if isinstance(dim_in, int) else dim_in, |
| 250 | initializer=initializers.Initializer( |
| 251 | 'ConstantFill', |
| 252 | value=initial_scale, |
| 253 | ), |
| 254 | tags=ParameterTags.WEIGHT, |
| 255 | ) |
| 256 | |
| 257 | # The learned additive bias or "shift". |
| 258 | bias = model.create_param( |
| 259 | param_name='{}_bias'.format(blob_out), |
| 260 | shape=[dim_in] if isinstance(dim_in, int) else dim_in, |
| 261 | initializer=initializers.Initializer( |
| 262 | 'ConstantFill', |
| 263 | value=initial_bias, |
| 264 | ), |
| 265 | tags=ParameterTags.BIAS, |
| 266 | ) |
| 267 | |
| 268 | normalized, mean, std = model.net.LayerNorm( |
nothing calls this directly
no test coverage detected
searching dependent graphs…