예제 #1
0
def tf_spec_structure(spec,
                      inputs=None,
                      input_shape=None,
                      input_type=dtypes.float32):
  """Return a postfix representation of the specification.

  This is intended to be used as part of test cases to
  check for gross differences in the structure of the graph.
  The resulting string is not invertible or unabiguous
  and cannot be used to reconstruct the graph accurately.

  Args:
      spec: specification
      inputs: input to the spec construction (usually a Tensor)
      input_shape: tensor shape (in lieu of inputs)
      input_type: type of the input tensor

  Returns:
      A string with a postfix representation of the
      specification.
  """

  if inputs is None:
    inputs = array_ops.placeholder(input_type, input_shape)
  outputs = specs.create_net(spec, inputs)
  return str(tf_structure(outputs).strip())
예제 #2
0
def tf_spec_structure(spec,
                      inputs=None,
                      input_shape=None,
                      input_type=dtypes.float32):
    """Return a postfix representation of the specification.

  This is intended to be used as part of code cases to
  check for gross differences in the structure of the graph.
  The resulting string is not invertible or unabiguous
  and cannot be used to reconstruct the graph accurately.

  Args:
      spec: specification
      inputs: input to the spec construction (usually a Tensor)
      input_shape: tensor shape (in lieu of inputs)
      input_type: type of the input tensor

  Returns:
      A string with a postfix representation of the
      specification.
  """

    if inputs is None:
        inputs = array_ops.placeholder(input_type, input_shape)
    outputs = specs.create_net(spec, inputs)
    return str(tf_structure(outputs).strip())
예제 #3
0
 def testPrint(self):
     with self.test_session():
         inputs = constant_op.constant(_rand(1, 18, 19, 5))
         spec = "net = Cr(64, [5, 5])"
         outputs = specs.create_net(spec, inputs)
         variables.global_variables_initializer().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
         summaries.tf_spec_print(spec, inputs)
예제 #4
0
 def testLstm2to0(self):
     with self.test_session():
         inputs = tf.constant(_rand(1, 64, 64, 5))
         spec = "net = Lstm2to0(15)"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [1, 15])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 15))
예제 #5
0
 def testLstm2to0(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 64, 64, 5))
     spec = "net = Lstm2to0(15)"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [1, 15])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 15))
예제 #6
0
 def testSummary(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 18, 19, 5))
     spec = "net = Cr(64, [5, 5])"
     outputs = specs.create_net(spec, inputs)
     tf.global_variables_initializer().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
     summaries.tf_spec_summary(spec, inputs)
예제 #7
0
 def testSummary(self):
     with self.test_session():
         inputs = tf.constant(_rand(1, 18, 19, 5))
         spec = "net = Cr(64, [5, 5])"
         outputs = specs.create_net(spec, inputs)
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
         summaries.tf_spec_summary(spec, inputs)
예제 #8
0
 def testStructureFromTensor(self):
     with self.test_session():
         inputs = tf.constant(_rand(1, 18, 19, 5))
         spec = "net = Cr(64, [5, 5])"
         outputs = specs.create_net(spec, inputs)
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
         self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                          "_ var conv var biasadd relu")
예제 #9
0
 def testStructureFromTensor(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 18, 19, 5))
     spec = "net = Cr(64, [5, 5])"
     outputs = specs.create_net(spec, inputs)
     tf.global_variables_initializer().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ variablev2 conv variablev2 biasadd relu")
예제 #10
0
 def testUnary(self):
     # This is just a quick and dirty check that these ops exist
     # and work as unary ops.
     with self.test_session():
         inputs = tf.constant(_rand(17, 55))
         spec = "net = Do(0.5) | Bn | Unit(1) | Relu | Sig | Tanh | Smax"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [17, 55])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (17, 55))
예제 #11
0
 def testMpPower(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 64, 64, 5))
     spec = "M2 = Mp([2, 2]); net = M2**3"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [1, 8, 8, 5])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 8, 8, 5))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ maxpool maxpool maxpool")
예제 #12
0
 def testUnary(self):
   # This is just a quick and dirty check that these ops exist
   # and work as unary ops.
   with self.test_session():
     inputs = tf.constant(_rand(17, 55))
     spec = "net = Do(0.5) | Bn | Unit(1) | Relu | Sig | Tanh | Smax"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [17, 55])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (17, 55))
예제 #13
0
 def testSimpleConv(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 18, 19, 5))
     spec = "net = Cr(64, [5, 5])"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [1, 18, 19, 64])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ var conv var biasadd relu")
예제 #14
0
 def testMpPower(self):
     with self.test_session():
         inputs = tf.constant(_rand(1, 64, 64, 5))
         spec = "M2 = Mp([2, 2]); net = M2**3"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [1, 8, 8, 5])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 8, 8, 5))
         self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                          "_ maxpool maxpool maxpool")
예제 #15
0
 def testImport(self):
   with self.test_session():
     inputs = tf.constant(_rand(10, 20))
     spec = "S = Import('import tensorflow as tf; f = tf.nn.sigmoid')"
     spec += "; net = S | S"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [10, 20])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (10, 20))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ sig sig")
예제 #16
0
 def testAdd(self):
   with self.test_session():
     inputs = tf.constant(_rand(17, 55))
     spec = "net = Fs(10) + Fr(10)"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [17, 10])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (17, 10))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ var dot var biasadd sig "
                      "<> var dot var biasadd relu add")
예제 #17
0
 def testStructure(self):
     with self.test_session():
         inputs_shape = (1, 18, 19, 5)
         inputs = constant_op.constant(_rand(*inputs_shape))
         spec = "net = Cr(64, [5, 5])"
         outputs = specs.create_net(spec, inputs)
         variables.global_variables_initializer().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 18, 19, 64))
         self.assertEqual(
             summaries.tf_spec_structure(spec, input_shape=inputs_shape),
             "_ variablev2 conv variablev2 biasadd relu")
예제 #18
0
 def testConc(self):
   with self.test_session():
     inputs = tf.constant(_rand(10, 20))
     spec = "net = Conc(1, Fs(20), Fs(10))"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [10, 30])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (10, 30))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ _ var dot var biasadd sig "
                      "<> var dot var biasadd sig concat")
예제 #19
0
 def testImport(self):
     with self.test_session():
         inputs = tf.constant(_rand(10, 20))
         spec = "S = Import('import tensorflow as tf; f = tf.nn.sigmoid')"
         spec += "; net = S | S"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [10, 20])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (10, 20))
         self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                          "_ sig sig")
예제 #20
0
 def testAdd(self):
     with self.test_session():
         inputs = tf.constant(_rand(17, 55))
         spec = "net = Fs(10) + Fr(10)"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [17, 10])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (17, 10))
         self.assertEqual(
             summaries.tf_spec_structure(spec, inputs),
             "_ var dot var biasadd sig "
             "<> var dot var biasadd relu add")
예제 #21
0
 def testAbbrevPower(self):
   with self.test_session():
     inputs = tf.constant(_rand(1, 64, 64, 5))
     spec = "C3 = Cr([3, 3]); M2 = Mp([2, 2]); net = (C3(5) | M2)**3"
     outputs = specs.create_net(spec, inputs)
     self.assertEqual(outputs.get_shape().as_list(), [1, 8, 8, 5])
     tf.initialize_all_variables().run()
     result = outputs.eval()
     self.assertEqual(tuple(result.shape), (1, 8, 8, 5))
     self.assertEqual(summaries.tf_spec_structure(spec, inputs),
                      "_ var conv var biasadd relu maxpool var conv var"
                      " biasadd relu maxpool var conv var"
                      " biasadd relu maxpool")
예제 #22
0
 def testConc(self):
     with self.test_session():
         inputs = tf.constant(_rand(10, 20))
         spec = "net = Conc(1, Fs(20), Fs(10))"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [10, 30])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (10, 30))
         self.assertEqual(
             summaries.tf_spec_structure(spec, inputs),
             "_ _ var dot var biasadd sig "
             "<> var dot var biasadd sig concat")
예제 #23
0
 def testAbbrevPower(self):
     with self.test_session():
         inputs = tf.constant(_rand(1, 64, 64, 5))
         spec = "C3 = Cr([3, 3]); M2 = Mp([2, 2]); net = (C3(5) | M2)**3"
         outputs = specs.create_net(spec, inputs)
         self.assertEqual(outputs.get_shape().as_list(), [1, 8, 8, 5])
         tf.initialize_all_variables().run()
         result = outputs.eval()
         self.assertEqual(tuple(result.shape), (1, 8, 8, 5))
         self.assertEqual(
             summaries.tf_spec_structure(spec, inputs),
             "_ var conv var biasadd relu maxpool var conv var"
             " biasadd relu maxpool var conv var"
             " biasadd relu maxpool")
예제 #24
0
def tf_spec_print(spec, inputs=None, input_shape=None, input_type=tf.float32):
  """Print a tree representing the spec.

  Args:
      spec: specification
      inputs: input to the spec construction (usually a Tensor)
      input_shape: optional shape of input
      input_type: type of the input tensor
  """

  if inputs is None:
    inputs = tf.placeholder(input_type, input_shape)
  outputs = specs.create_net(spec, inputs)
  tf_print(outputs)
예제 #25
0
def tf_spec_summary(spec, inputs=None, input_shape=None, input_type=tf.float32):
  """Output a summary of the specification.

  This prints a list of left-most tensor operations and summarized the
  variables found in the right branches. This kind of representation
  is particularly useful for networks that are generally structured
  like pipelines.

  Args:
      spec: specification
      inputs: input to the spec construction (usually a Tensor)
      input_shape: optional shape of input
      input_type: type of the input tensor
  """

  if inputs is None:
    inputs = tf.placeholder(input_type, input_shape)
  outputs = specs.create_net(spec, inputs)
  tf_parameter_summary(outputs)
예제 #26
0
 def testKeywordRestriction(self):
   with self.test_session():
     inputs = tf.constant(_rand(10, 20))
     spec = "import re; net = Conc(1, Fs(20), Fs(10))"
     self.assertRaises(ValueError, lambda: specs.create_net(spec, inputs))
예제 #27
0
 def testKeywordRestriction(self):
     with self.test_session():
         inputs = tf.constant(_rand(10, 20))
         spec = "import re; net = Conc(1, Fs(20), Fs(10))"
         self.assertRaises(ValueError,
                           lambda: specs.create_net(spec, inputs))