parameters.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2016 Google Inc. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ==============================================================================
  15. """Author: aneelakantan (Arvind Neelakantan)
  16. """
  17. import numpy as np
  18. import tensorflow as tf
  19. class Parameters:
  20. def __init__(self, u):
  21. self.utility = u
  22. self.init_seed_counter = 0
  23. self.word_init = {}
  24. def parameters(self, utility):
  25. params = {}
  26. inits = []
  27. embedding_dims = self.utility.FLAGS.embedding_dims
  28. params["unit"] = tf.Variable(
  29. self.RandomUniformInit([len(utility.operations_set), embedding_dims]))
  30. params["word"] = tf.Variable(
  31. self.RandomUniformInit([utility.FLAGS.vocab_size, embedding_dims]))
  32. params["word_match_feature_column_name"] = tf.Variable(
  33. self.RandomUniformInit([1]))
  34. params["controller"] = tf.Variable(
  35. self.RandomUniformInit([2 * embedding_dims, embedding_dims]))
  36. params["column_controller"] = tf.Variable(
  37. self.RandomUniformInit([2 * embedding_dims, embedding_dims]))
  38. params["column_controller_prev"] = tf.Variable(
  39. self.RandomUniformInit([embedding_dims, embedding_dims]))
  40. params["controller_prev"] = tf.Variable(
  41. self.RandomUniformInit([embedding_dims, embedding_dims]))
  42. global_step = tf.Variable(1, name="global_step")
  43. #weigths of question and history RNN (or LSTM)
  44. key_list = ["question_lstm"]
  45. for key in key_list:
  46. # Weights going from inputs to nodes.
  47. for wgts in ["ix", "fx", "cx", "ox"]:
  48. params[key + "_" + wgts] = tf.Variable(
  49. self.RandomUniformInit([embedding_dims, embedding_dims]))
  50. # Weights going from nodes to nodes.
  51. for wgts in ["im", "fm", "cm", "om"]:
  52. params[key + "_" + wgts] = tf.Variable(
  53. self.RandomUniformInit([embedding_dims, embedding_dims]))
  54. #Biases for the gates and cell
  55. for bias in ["i", "f", "c", "o"]:
  56. if (bias == "f"):
  57. print "forget gate bias"
  58. params[key + "_" + bias] = tf.Variable(
  59. tf.random_uniform([embedding_dims], 1.0, 1.1, self.utility.
  60. tf_data_type[self.utility.FLAGS.data_type]))
  61. else:
  62. params[key + "_" + bias] = tf.Variable(
  63. self.RandomUniformInit([embedding_dims]))
  64. params["history_recurrent"] = tf.Variable(
  65. self.RandomUniformInit([3 * embedding_dims, embedding_dims]))
  66. params["history_recurrent_bias"] = tf.Variable(
  67. self.RandomUniformInit([1, embedding_dims]))
  68. params["break_conditional"] = tf.Variable(
  69. self.RandomUniformInit([2 * embedding_dims, embedding_dims]))
  70. init = tf.global_variables_initializer()
  71. return params, global_step, init
  72. def RandomUniformInit(self, shape):
  73. """Returns a RandomUniform Tensor between -param_init and param_init."""
  74. param_seed = self.utility.FLAGS.param_seed
  75. self.init_seed_counter += 1
  76. return tf.random_uniform(
  77. shape, -1.0 *
  78. (np.float32(self.utility.FLAGS.param_init)
  79. ).astype(self.utility.np_data_type[self.utility.FLAGS.data_type]),
  80. (np.float32(self.utility.FLAGS.param_init)
  81. ).astype(self.utility.np_data_type[self.utility.FLAGS.data_type]),
  82. self.utility.tf_data_type[self.utility.FLAGS.data_type],
  83. param_seed + self.init_seed_counter)