Guest User

Untitled

a guest
Jun 22nd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.87 KB | None | 0 0
  1. class Model(object):
  2. def __init__(self, _input, is_training, hidden_size, vocab_size, num_layers,
  3. dropout=config.trainer.dropout, init_scale=config.trainer.init_scale):
  4. self.is_training = is_training
  5. self.input_obj = _input
  6. self.batch_size = _input.batch_size
  7. self.num_steps = _input.num_steps
  8. self.hidden_size = hidden_size
  9.  
  10. # create the word embeddings
  11. with tf.device("/cpu:0"):
  12. randomized = tf.random_uniform([vocab_size, hidden_size], -init_scale, init_scale)
  13. print("randomized: ", randomized)
  14. embedding = tf.Variable(randomized)
  15. inputs = tf.nn.embedding_lookup(embedding, self.input_obj.input_data)
  16.  
  17. if is_training and dropout < 1:
  18. inputs = tf.nn.dropout(inputs, dropout)
  19.  
  20.  
  21.  
  22. # set up the state storage / extraction
  23. self.init_state = tf.placeholder(tf.float32, [num_layers, 2, self.batch_size, hidden_size])
  24.  
  25. state_per_layer_list = tf.unstack(self.init_state, axis=0)
  26. rnn_tuple_state = tuple([tf.contrib.rnn.LSTMStateTuple(state_per_layer_list[idx][0], state_per_layer_list[idx][1])for idx in range(num_layers)])
  27.  
  28. # create an LSTM cell to be unrolled
  29. print("Hidden size: ", hidden_size)
  30. cell = tf.contrib.rnn.LSTMCell(hidden_size, forget_bias=config.trainer.forget_bias)
  31. # add a dropout wrapper if training
  32. if is_training and dropout < 1:
  33. cell = tf.contrib.rnn.DropoutWrapper(cell, output_keep_prob=dropout)
  34.  
  35. if num_layers > 1:
  36. cell = tf.contrib.rnn.MultiRNNCell([cell for _ in range(num_layers)], state_is_tuple=True)
  37.  
  38. print("input: ", inputs)
  39. output, self.state = tf.nn.dynamic_rnn(cell, inputs, dtype=tf.float32, initial_state=rnn_tuple_state)
  40.  
  41. # reshape to (batch_size * num_steps, hidden_size)
  42. output = tf.reshape(output, [-1, hidden_size])
  43.  
  44. softmax_w = tf.Variable(tf.random_uniform([hidden_size, vocab_size], -init_scale, init_scale))
  45. softmax_b = tf.Variable(tf.random_uniform([vocab_size], -init_scale, init_scale))
  46. logits = tf.nn.xw_plus_b(output, softmax_w, softmax_b)
  47.  
  48. # Reshape logits to be a 3-D tensor for sequence loss
  49. logits = tf.reshape(logits, [self.batch_size, self.num_steps, vocab_size])
  50.  
  51. # Use the contrib sequence loss and average over the batches
  52. loss = tf.contrib.seq2seq.sequence_loss(logits,
  53. self.input_obj.targets,
  54. tf.ones([self.batch_size, self.num_steps], dtype=tf.float32),
  55. average_across_timesteps=False,
  56. average_across_batch=True)
  57. # Update the cost
  58. self.cost = tf.reduce_sum(loss)
  59.  
  60. # get the prediction accuracy
  61. self.softmax_out = tf.nn.softmax(tf.reshape(logits, [-1, vocab_size]))
  62. self.predict = tf.cast(tf.argmax(self.softmax_out, axis=1), tf.int32)
  63. correct_prediction = tf.equal(self.predict, tf.reshape(self.input_obj.targets, [-1]))
  64. self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
  65.  
  66. if not is_training:
  67. return
  68. self.learning_rate = tf.Variable(0.01, trainable=False)
  69.  
  70. tvars = tf.trainable_variables()
  71. grads, _ = tf.clip_by_global_norm(tf.gradients(self.cost, tvars), 5)
  72. optimizer = tf.train.GradientDescentOptimizer(self.learning_rate)
  73. self.train_op = optimizer.apply_gradients(zip(grads, tvars),
  74. global_step=tf.contrib.framework.get_or_create_global_step())
  75.  
  76. self.new_lr = tf.placeholder(tf.float32, shape=[])
  77. self.lr_update = tf.assign(self.learning_rate, self.new_lr)
  78.  
  79. def assign_lr(self, session, lr_value):
  80. session.run(self.lr_update, feed_dict={self.new_lr: lr_value})
Add Comment
Please, Sign In to add comment