MCPcopy Create free account
hub / github.com/apache/singa / _random_fill

Function _random_fill

python/singa/initializer.py:285–336  ·  view source on GitHub ↗

Fill the tensor with values sampled from a distribution. With `distribution="normal"`, samples are drawn from a normal distribution centered on zero, with `stddev = sqrt(scale / n)` where n is: - number of input units in the weight tensor, if mode = "fan_in" - number of outp

(t, scale, mode, distribution)

Source from the content-addressed store, hash-verified

283
284
285def _random_fill(t, scale, mode, distribution):
286 """Fill the tensor with values sampled from a distribution.
287
288 With `distribution="normal"`, samples are drawn from a normal
289 distribution centered on zero, with `stddev = sqrt(scale / n)` where n is:
290 - number of input units in the weight tensor, if mode = "fan_in"
291 - number of output units, if mode = "fan_out"
292 - average of the numbers of input and output units, if mode = "fan_avg"
293
294 With `distribution="uniform"`,
295 samples are drawn from a uniform distribution
296 within [-limit, limit], with `limit = sqrt(3 * scale / n)`.
297
298
299 Args:
300 t (Tensor): Tensor to be filled
301 scale (float): scale factor
302 mode (str): "fan_in" or "fan_out" or "fan_avg"
303 distribution (str): "normal" or "uniform"
304
305 Raises:
306 ValueError: In case of an invalid value for scale, mode or distribution
307 """
308 if scale <= 0.:
309 raise ValueError('`scale` must be a positive float. Got:', scale)
310 mode = mode.lower()
311 if mode not in {'fan_in', 'fan_out', 'fan_avg'}:
312 raise ValueError(
313 'Invalid `mode` argument: '
314 'expected on of {"fan_in", "fan_out", "fan_avg"} '
315 'but got', mode)
316 distribution = distribution.lower()
317 if distribution not in {'normal', 'uniform'}:
318 raise ValueError(
319 'Invalid `distribution` argument: '
320 'expected one of {"normal", "uniform"} '
321 'but got', distribution)
322
323 fan_in, fan_out = _compute_fans(t.shape)
324 if mode == 'fan_in':
325 scale /= max(1., fan_in)
326 elif mode == 'fan_out':
327 scale /= max(1., fan_out)
328 else:
329 scale /= max(1., float(fan_in + fan_out) / 2)
330 if distribution == 'normal':
331 # 0.879... = scipy.stats.truncnorm.std(a=-2, b=2, loc=0., scale=1.)
332 # stddev = np.sqrt(scale) / .87962566103423978
333 t.gaussian(0., np.sqrt(scale))
334 else:
335 limit = np.sqrt(3. * scale)
336 t.uniform(-limit, limit)

Callers 6

lecun_uniformFunction · 0.85
glorot_normalFunction · 0.85
glorot_uniformFunction · 0.85
he_normalFunction · 0.85
lecun_normalFunction · 0.85
he_uniformFunction · 0.85

Calls 4

_compute_fansFunction · 0.85
maxFunction · 0.85
gaussianMethod · 0.45
uniformMethod · 0.45

Tested by

no test coverage detected