|
|
|
|
|
|
|
|
|
|
|
"""Initialization functions for RNN sequence-to-sequence models.""" |
|
|
|
import math |
|
|
|
|
|
def lecun_normal_init_parameters(module): |
|
"""Initialize parameters in the LeCun's manner.""" |
|
for p in module.parameters(): |
|
data = p.data |
|
if data.dim() == 1: |
|
|
|
data.zero_() |
|
elif data.dim() == 2: |
|
|
|
n = data.size(1) |
|
stdv = 1.0 / math.sqrt(n) |
|
data.normal_(0, stdv) |
|
elif data.dim() in (3, 4): |
|
|
|
n = data.size(1) |
|
for k in data.size()[2:]: |
|
n *= k |
|
stdv = 1.0 / math.sqrt(n) |
|
data.normal_(0, stdv) |
|
else: |
|
raise NotImplementedError |
|
|
|
|
|
def uniform_init_parameters(module): |
|
"""Initialize parameters with an uniform distribution.""" |
|
for p in module.parameters(): |
|
data = p.data |
|
if data.dim() == 1: |
|
|
|
data.uniform_(-0.1, 0.1) |
|
elif data.dim() == 2: |
|
|
|
data.uniform_(-0.1, 0.1) |
|
elif data.dim() in (3, 4): |
|
|
|
pass |
|
else: |
|
raise NotImplementedError |
|
|
|
|
|
def set_forget_bias_to_one(bias): |
|
"""Initialize a bias vector in the forget gate with one.""" |
|
n = bias.size(0) |
|
start, end = n // 4, n // 2 |
|
bias.data[start:end].fill_(1.0) |
|
|