network_units_test.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Tests for network_units."""
  2. import tensorflow as tf
  3. from tensorflow.python.framework import test_util
  4. from tensorflow.python.platform import googletest
  5. from dragnn.protos import spec_pb2
  6. from dragnn.python import network_units
  7. import dragnn.python.load_dragnn_cc_impl
  8. import syntaxnet.load_parser_ops
  9. FLAGS = tf.app.flags.FLAGS
  10. class NetworkUnitsConverterTest(test_util.TensorFlowTestCase):
  11. def testConvertNetworkStateTensorarray(self):
  12. with self.test_session() as session:
  13. ta = tf.TensorArray(
  14. dtype=tf.float32,
  15. size=0,
  16. dynamic_size=True,
  17. clear_after_read=False,
  18. infer_shape=False)
  19. # Create a 3-step x 2-stride x 2-feature-dim source array.
  20. ta = ta.write(0, [[0., 0.]] * 2) # The zeroth step will be removed.
  21. ta = ta.write(1, [[1., 10.]] * 2)
  22. ta = ta.write(2, [[2., 20.]] * 2)
  23. ta = ta.write(3, [[3., 30.]] * 2)
  24. tensor = network_units.convert_network_state_tensorarray(ta)
  25. actual = session.run(tensor)
  26. self.assertEqual(actual.shape, (6, 2))
  27. # The arrangement of the values is expected to be stride * steps.
  28. expected = [[1., 10.], [2., 20.], [3., 30.], [1., 10.], [2., 20.],
  29. [3., 30.]]
  30. self.assertAllEqual(actual, expected)
  31. class MockComponent(object):
  32. def __init__(self, master, component_spec):
  33. self.master = master
  34. self.spec = component_spec
  35. self.name = component_spec.name
  36. self.beam_size = 1
  37. self._attrs = {}
  38. def attr(self, name):
  39. return self._attrs[name]
  40. class MockMaster(object):
  41. def __init__(self):
  42. self.spec = spec_pb2.MasterSpec()
  43. self.hyperparams = spec_pb2.GridPoint()
  44. self.lookup_component = {
  45. 'previous': MockComponent(self, spec_pb2.ComponentSpec())
  46. }
  47. class NetworkUnitsLookupTest(test_util.TensorFlowTestCase):
  48. def setUp(self):
  49. # Clear the graph and all existing variables. Otherwise, variables created
  50. # in different tests may collide with each other.
  51. tf.reset_default_graph()
  52. self._master = MockMaster()
  53. self._master.spec = spec_pb2.MasterSpec()
  54. # Add a component with a linked feature.
  55. component_spec = self._master.spec.component.add()
  56. component_spec.name = 'fake_linked'
  57. component_spec.backend.registered_name = 'FakeComponent'
  58. linked_feature = component_spec.linked_feature.add()
  59. linked_feature.source_component = 'fake_linked'
  60. linked_feature.source_translator = 'identity'
  61. linked_feature.embedding_dim = -1
  62. linked_feature.size = 2
  63. self._linked_component = MockComponent(self._master, component_spec)
  64. # Add a feature with a fixed feature.
  65. component_spec = self._master.spec.component.add()
  66. component_spec.name = 'fake_fixed'
  67. component_spec.backend.registered_name = 'FakeComponent'
  68. fixed_feature = component_spec.fixed_feature.add()
  69. fixed_feature.fml = 'input.word'
  70. fixed_feature.embedding_dim = 1
  71. fixed_feature.size = 1
  72. self._fixed_component = MockComponent(self._master, component_spec)
  73. def testExportFixedFeaturesNetworkWithEnabledEmbeddingMatrix(self):
  74. network = network_units.ExportFixedFeaturesNetwork(self._fixed_component)
  75. self.assertEqual(1, len(network.params))
  76. def testExportFixedFeaturesNetworkWithDisabledEmbeddingMatrix(self):
  77. self._fixed_component.spec.fixed_feature[0].embedding_dim = -1
  78. network = network_units.ExportFixedFeaturesNetwork(self._fixed_component)
  79. self.assertEqual(0, len(network.params))
  80. class GetAttrsWithDefaultsTest(test_util.TensorFlowTestCase):
  81. def MakeAttrs(self, defaults, key=None, value=None):
  82. """Returns attrs based on the |defaults| and one |key|,|value| override."""
  83. spec = spec_pb2.RegisteredModuleSpec()
  84. if key and value:
  85. spec.parameters[key] = value
  86. return network_units.get_attrs_with_defaults(spec.parameters, defaults)
  87. def testFalseValues(self):
  88. def _assert_attr_is_false(value=None):
  89. key = 'foo'
  90. attrs = self.MakeAttrs({key: False}, key, value)
  91. self.assertFalse(attrs[key])
  92. _assert_attr_is_false()
  93. _assert_attr_is_false('false')
  94. _assert_attr_is_false('False')
  95. _assert_attr_is_false('FALSE')
  96. _assert_attr_is_false('no')
  97. _assert_attr_is_false('whatever')
  98. _assert_attr_is_false(' ')
  99. _assert_attr_is_false('')
  100. def testTrueValues(self):
  101. def _assert_attr_is_true(value=None):
  102. key = 'foo'
  103. attrs = self.MakeAttrs({key: False}, key, value)
  104. self.assertTrue(attrs[key])
  105. _assert_attr_is_true('true')
  106. _assert_attr_is_true('True')
  107. _assert_attr_is_true('TRUE')
  108. if __name__ == '__main__':
  109. googletest.main()