Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save huynhnguyen/7d824ebb3834346c4a882f194f9a909b to your computer and use it in GitHub Desktop.
Save huynhnguyen/7d824ebb3834346c4a882f194f9a909b to your computer and use it in GitHub Desktop.
open journal (24/5/217): orthogonal initializer for lstm with tensorflow 1.10.
import numpy as np
def orthogonal_initializer(scale=1.0, seed=None, dtype=tf.float32):
def _initializer(shape, dtype=dtype, partition_info=None):
flat = (shape[0], np.prod(shape[1:]))
a = np.random.normal(0.0, 1.0, flat)
u, _, v = np.linalg.svd(a, full_matrices=False)
q = (u if u.shape == flat else v).reshape(shape)
return tf.constant(scale * q[:shape[0], :shape[1]], dtype=dtype)
return _initializer
tf.reset_default_graph()
ortho_initializer = orthogonal_initializer()
with tf.variable_scope('e',reuse=None):
encode_cell = tf.contrib.rnn.LSTMCell(num_units= 10, initializer=ortho_initializer)
inpt = tf.constant([[1,2,3,4,5,6,7,8,9,10]],dtype=tf.float32)
zero = encode_cell.zero_state(1,tf.float32)
outp = encode_cell(inpt,zero)
with tf.Session() as sess:
tf.global_variables_initializer().run()
print sess.run(outp)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment