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())
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())
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)
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))
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)
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)
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")
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")
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))
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")
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")
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")
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")
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")
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")
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")
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")
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")
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")
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)
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)
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))