def init_h(h_init, X, batch_size, models, **h_args):
'''Initializes the RNN hidden state.
Args:
h_init (str): type of initialization.
X (T.tensor): input tensor for initialization through MLP.
batch_size (int)
models (list): list of Layer, pulls 'h_net' for initialization
(TODO change this).
**h_args: kwargs for different initializations.
Returns:
T.tensor: Full 3D tensor returned to train `h_net`.
'''
if h_init is None:
h0 = None
elif h_init == 'last':
print 'Initializing h0 from chain'
h0 = theano.shared(np.zeros((batch_size, rnn.dim_h)).astype(floatX))
h0s = h0[None, :, :]
elif h_init == 'noise':
noise_amount = h_args['noise_amount']
print 'Initializing h0 from noise'
h0 = trng.normal(avg=0, std=0.1, size=(batch_size, rnn.dim_h)).astype(floatX)
h0s = h0[None, :, :]
elif h_init == 'average':
print 'Initializing h0 from running average'
averager = models['averager']
h0 = (T.alloc(0., batch_size, rnn.dim_h) + averager.m[None, :]).astype(floatX)
h0s = h0[None, :, :]
elif h_init == 'mlp':
print 'Initializing h0 from MLP'
mlp = models['h_net']
h0s = mlp(X)
return h0s
评论列表
文章目录