Python tensorflow.python.framework.tensor_shape.Dimension() Examples
The following are 30
code examples of tensorflow.python.framework.tensor_shape.Dimension().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
tensorflow.python.framework.tensor_shape
, or try the search function
.
Example #1
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testEquality(self): self.assertTrue(tensor_shape.Dimension(12) == tensor_shape.Dimension(12)) self.assertFalse(tensor_shape.Dimension(12) == tensor_shape.Dimension(13)) self.assertIs(None, tensor_shape.Dimension(12) == tensor_shape.Dimension(None)) self.assertIs(None, tensor_shape.Dimension(None) == tensor_shape.Dimension(12)) self.assertIs(None, tensor_shape.Dimension(None) == tensor_shape.Dimension(None)) self.assertTrue(tensor_shape.Dimension(12) == "12") self.assertTrue(tensor_shape.Dimension(12) == 24.0 / 2) # None indicates ambiguous comparison, but comparison vs the wrong type # is unambigously False. self.assertIsNotNone(tensor_shape.Dimension(12) == "_") self.assertIsNotNone(tensor_shape.Dimension(None) == 12.99) self.assertFalse(tensor_shape.Dimension(12) == "_") self.assertFalse(tensor_shape.Dimension(None) == 12.99) self.assertIs(None, tensor_shape.Dimension(None) == "13") self.assertIs(None, tensor_shape.Dimension(None) == None) # pylint: disable=g-equals-none self.assertFalse(tensor_shape.Dimension(12) == 12.99)
Example #2
Source File: constant_op.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def _dimension_tensor_conversion_function(d, dtype=None, name=None, as_ref=False): """Function to convert Dimension to Tensor.""" _ = as_ref if d.value is None: raise ValueError("Cannot convert an unknown Dimension to a Tensor: %s" % d) if dtype is not None: if dtype not in (dtypes.int32, dtypes.int64): raise TypeError("Cannot convert a TensorShape to dtype: %s" % dtype) else: dtype = dtypes.int32 if name is None: name = "shape_as_tensor" return constant(d.value, dtype=dtype, name=name)
Example #3
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testConcatenate(self): tensor_shape.TensorShape([1, 2, 3, 4]).assert_is_compatible_with( tensor_shape.TensorShape([1, 2]).concatenate( tensor_shape.TensorShape([3, 4]))) tensor_shape.TensorShape([1, 2, 3, 4]).assert_is_compatible_with( tensor_shape.TensorShape([1, 2]).concatenate( tensor_shape.TensorShape(None))) tensor_shape.TensorShape([1, 2, 3, 4]).assert_is_compatible_with( tensor_shape.TensorShape(None).concatenate( tensor_shape.TensorShape([3, 4]))) tensor_shape.TensorShape([1, 2, 3, 4]).assert_is_compatible_with( tensor_shape.TensorShape(None).concatenate( tensor_shape.TensorShape(None))) tensor_shape.TensorShape([1, 2, 3]).assert_is_compatible_with( tensor_shape.TensorShape([1, 2]).concatenate( tensor_shape.Dimension(3)))
Example #4
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testFullyDefinedShape(self): s = tensor_shape.TensorShape([tensor_shape.Dimension( 3), tensor_shape.Dimension(4), tensor_shape.Dimension(7)]) s.assert_is_fully_defined() self.assertEqual(3, s.ndims) self.assertEqual(3, len(s)) self.assertTrue(s) s.assert_has_rank(3) self.assertEqual([tensor_shape.Dimension(3), tensor_shape.Dimension(4), tensor_shape.Dimension(7)], s.dims) self.assertEqual(tensor_shape.Dimension(3), s[0]) self.assertEqual(tensor_shape.Dimension(4), s[1]) self.assertEqual(tensor_shape.Dimension(7), s[2]) self.assertEqual([3, 4, 7], s.as_list()) s.assert_is_compatible_with([3, 4, 7]) s.assert_same_rank([6, 3, 7]) for d1, d2 in zip(s, [3, 4, 7]): assert d1.value == d2
Example #5
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testUnknownDimension(self): dim = tensor_shape.Dimension(None) self.assertIs(None, dim.value) self.assertEqual(dim.value, tensor_shape.Dimension(None).value) self.assertEqual(tensor_shape.Dimension(None).value, (dim + tensor_shape.Dimension(None)).value) self.assertEqual(tensor_shape.Dimension(None).value, (dim * tensor_shape.Dimension(None)).value) self.assertEqual( tensor_shape.Dimension(None).value, (dim // tensor_shape.Dimension(None)).value) self.assertEqual(tensor_shape.Dimension(None).value, dim.merge_with(tensor_shape.Dimension(None)).value) self.assertIs(None, tensor_shape.Dimension(None) < tensor_shape.Dimension(None)) self.assertIs(None, tensor_shape.Dimension(None) <= tensor_shape.Dimension(None)) self.assertIs(None, tensor_shape.Dimension(None) > tensor_shape.Dimension(None)) self.assertIs(None, tensor_shape.Dimension(None) >= tensor_shape.Dimension(None))
Example #6
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testInequality(self): self.assertTrue(tensor_shape.Dimension(12) != tensor_shape.Dimension(13)) self.assertFalse(tensor_shape.Dimension(12) != tensor_shape.Dimension(12)) self.assertIs(None, tensor_shape.Dimension(12) != tensor_shape.Dimension(None)) self.assertIs(None, tensor_shape.Dimension(None) != tensor_shape.Dimension(12)) self.assertIs(None, tensor_shape.Dimension(None) != tensor_shape.Dimension(None)) # None indicates ambiguous comparison, but comparison vs the wrong type # is unambigously False. self.assertIsNotNone(tensor_shape.Dimension(12) != "_") self.assertIsNotNone(tensor_shape.Dimension(None) != 12.99) self.assertTrue(tensor_shape.Dimension(12) != "_") self.assertTrue(tensor_shape.Dimension(None) != 12.99) self.assertIs(None, tensor_shape.Dimension(None) != "13") self.assertIs(None, tensor_shape.Dimension(None) != None) # pylint: disable=g-equals-none self.assertTrue(tensor_shape.Dimension(12) != 12.99)
Example #7
Source File: graph_gen.py From tensorlang with Apache License 2.0 | 5 votes |
def dequeue(self, ctx, queue_ref, component_types=None, name=None): if name is None: name = tf.get_default_graph().unique_name("Dequeue", False).split("/")[-1] ret = gen_data_flow_ops._queue_dequeue_v2( queue_ref, component_types=component_types, name=name) # NOTE(mrry): Not using a shape function because we need access to # the Queue object. # op = ret[0].op # batch_dim = tensor_shape.Dimension(tensor_util.constant_value(op.inputs[1])) # for output, shape in zip(op.values(), shapes): # output.set_shape(tensor_shape.TensorShape([batch_dim]).concatenate(shape)) return ret
Example #8
Source File: dataset_ops.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def output_shapes(self): indices_shape = self._sparse_tensor.indices.get_shape() shape_shape = self._sparse_tensor.dense_shape.get_shape() rank = (indices_shape[1] - 1).merge_with(shape_shape[0] - 1) num_values = tensor_shape.Dimension(None) return (tensor_shape.TensorShape([num_values, rank]), tensor_shape.TensorShape([num_values]), tensor_shape.TensorShape( [rank]))
Example #9
Source File: dataset_ops.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def padded_batch(self, batch_size, padded_shapes, padding_values=None): """Combines consecutive elements of this dataset into padded batches. Like `Dataset.dense_to_sparse_batch()`, this method combines multiple consecutive elements of this dataset, which might have different shapes, into a single element. The tensors in the resulting element have an additional outer dimension, and are padded to the respective shape in `padded_shapes`. Args: batch_size: A `tf.int64` scalar `tf.Tensor`, representing the number of consecutive elements of this dataset to combine in a single batch. padded_shapes: A nested structure of `tf.TensorShape` or `tf.int64` vector tensor-like objects representing the shape to which the respective component of each input element should be padded prior to batching. Any unknown dimensions (e.g. `tf.Dimension(None)` in a `tf.TensorShape` or `-1` in a tensor-like object) will be padded to the maximum size of that dimension in each batch. padding_values: (Optional.) A nested structure of scalar-shaped `tf.Tensor`, representing the padding values to use for the respective components. Defaults are `0` for numeric types and the empty string for string types. Returns: A `Dataset`. """ return PaddedBatchDataset(self, batch_size, padded_shapes, padding_values)
Example #10
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testRepr(self): self.assertEqual(repr(tensor_shape.Dimension(7)), "Dimension(7)") self.assertEqual(repr(tensor_shape.Dimension(None)), "Dimension(None)")
Example #11
Source File: data_flow_ops.py From keras-lambda with MIT License | 5 votes |
def dequeue_many(self, n, name=None): """Dequeues and concatenates `n` elements from this queue. This operation concatenates queue-element component tensors along the 0th dimension to make a single component tensor. All of the components in the dequeued tuple will have size `n` in the 0th dimension. If the queue is closed and there are less than `n` elements left, then an `OutOfRange` exception is raised. At runtime, this operation may raise an error if the queue is [closed](#QueueBase.close) before or during its execution. If the queue is closed, the queue contains fewer than `n` elements, and there are no pending enqueue operations that can fulfill this request, `tf.errors.OutOfRangeError` will be raised. If the session is [closed](../../api_docs/python/client.md#Session.close), `tf.errors.CancelledError` will be raised. Args: n: A scalar `Tensor` containing the number of elements to dequeue. name: A name for the operation (optional). Returns: The tuple of concatenated tensors that was dequeued. """ if name is None: name = "%s_DequeueMany" % self._name ret = gen_data_flow_ops._queue_dequeue_many_v2( self._queue_ref, n=n, component_types=self._dtypes, name=name) # NOTE(mrry): Not using a shape function because we need access to # the Queue object. op = ret[0].op batch_dim = tensor_shape.Dimension(tensor_util.constant_value(op.inputs[1])) for output, shape in zip(op.values(), self._shapes): output.set_shape(tensor_shape.TensorShape([batch_dim]).concatenate(shape)) return self._dequeue_return_value(ret)
Example #12
Source File: core.py From keras-lambda with MIT License | 5 votes |
def __repr__(self): # <LabeledTensor 'foo' shape=(2, 3, 4) dtype=float32 # axes=[('x', Dimension(2)), # ('y', ('a', 'b', 'c'), # ('z', Dimension(4))]> axes = ["('%s', %r)" % (v.name, v.value) for v in self.axes.values()] axes_repr = (',\n' + ' ' * len(' axes=[')).join(axes) return ("<%s '%s' shape=%s dtype=%s\n axes=[%s]>" % (type(self).__name__, self.tensor.name, self.tensor.get_shape(), self.tensor.dtype.name, axes_repr))
Example #13
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def setUp(self): d_7 = tensor_shape.Dimension(7) p_rgb = ['red', 'green', 'blue'] self.i_7 = core.Axis('7', d_7) self.i_7p = core.Axis('7prime', d_7) self.i_rgb = core.Axis('rgb', p_rgb) self.i_range = core.Axis('range', range(7)) self.i_unknown = core.Axis('unknown', None)
Example #14
Source File: core.py From keras-lambda with MIT License | 5 votes |
def __init__(self, name, value): """Construct an Axis. Args: name: Name of the axis. value: Either None, an int or tf.Dimension giving the size of the axis, or a sequence that is not a string additionally providing coordinate (tick) labels. Raises: ValueError: If the user provides labels with duplicate values. """ if isinstance(value, tensor_shape.Dimension): dimension = value labels = None elif isinstance(value, int) or value is None: dimension = tensor_shape.Dimension(value) labels = None else: dimension = tensor_shape.Dimension(len(value)) labels = tuple(value) if dimension.value == 0: # Treat a zero-length axis as if it has labels. labels = () if labels is not None: index = dict(zip(labels, range(len(labels)))) if len(index) != len(labels): raise ValueError('Tick labels must be unique, but got {}' .format(labels)) else: index = None self._name = name # type: string_types self._dimension = dimension # type: tensor_shape.Dimension self._labels = labels # type: Optional[tuple] self._index = index # type: Optional[Dict[Any, int]]
Example #15
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def test_repr(self): self.assertEqual("Axis('7', Dimension(7))", repr(self.i_7))
Example #16
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def setUp(self): d_7 = tensor_shape.Dimension(7) d_8 = tensor_shape.Dimension(8) p_rgb = ['red', 'green', 'blue'] p_range = range(7) self.i_8 = core.Axis('8', d_8) self.a0 = core.Axes([('d7', d_7)]) self.a1 = core.Axes([('d7', d_7)]) self.a2 = core.Axes([('d7', d_7), ('rgb', p_rgb)]) self.a3 = core.Axes([('8', d_8), ('range', p_range)])
Example #17
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def test_repr(self): self.assertEqual("Axes([('d7', Dimension(7))])", repr(self.a0))
Example #18
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def test_typecheck_error_message(self): pattern = ('List(Union(labeled_tensor.Axis, Tuple(..., ' 'Union(Union(numpy.ndarray, %s, list, tuple), ' 'Optional(Union(tensorflow.Dimension, int))))))' % range.__name__) regexp = re.escape(pattern).replace(re.escape('...'), '.*') with self.assertRaisesRegexp(tc.Error, 'allowed type ' + regexp): core.Axes(None)
Example #19
Source File: core_test.py From keras-lambda with MIT License | 5 votes |
def setUp(self): tensor = array_ops.ones([7, 3, 8, 1]) a0 = ('x', range(7)) a1 = ('channel', ['red', 'green', 'blue']) a2 = ('y', 8) a3 = ('z', tensor_shape.Dimension(1)) self.lt = core.LabeledTensor(tensor, [a0, a1, a2, a3])
Example #20
Source File: core.py From keras-lambda with MIT License | 5 votes |
def __repr__(self): # Axes([('x', Dimension(2)), # ('y', ['a', 'b', 'c']), # ('z', Dimension(4))]) cls_name = type(self).__name__ values = ["('%s', %r)" % (v.name, v.value) for v in self._axes.values()] values_repr = (',\n' + ' ' * len(cls_name + '([')).join(values) return '%s([%s])' % (cls_name, values_repr)
Example #21
Source File: graph_gen.py From tensorlang with Apache License 2.0 | 5 votes |
def enqueue_many(self, ctx, queue_ref, components, name=None): if name is None: name = tf.get_default_graph().unique_name("EnqueueMany", False).split("/")[-1] ret = gen_data_flow_ops._queue_enqueue_many_v2( queue_ref, components=components, name=name) # NOTE(mrry): Not using a shape function because we need access to # the Queue object. # op = ret[0].op # batch_dim = tensor_shape.Dimension(tensor_util.constant_value(op.inputs[1])) # for output, shape in zip(op.values(), shapes): # output.set_shape(tensor_shape.TensorShape([batch_dim]).concatenate(shape)) return ret
Example #22
Source File: op_def_library_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testAttr(self): self._add_op("name: 'Attr' attr { name: 'a' type: 'int' }") op = self._lib.apply_op("Attr", a=12, name="t") self.assertProtoEquals(""" name: 't' op: 'Attr' attr { key: 'a' value { i: 12 } } """, op.node_def) op = self._lib.apply_op("Attr", a=tensor_shape.Dimension(13), name="u") self.assertProtoEquals(""" name: 'u' op: 'Attr' attr { key: 'a' value { i: 13 } } """, op.node_def) with self.assertRaises(TypeError) as cm: self._lib.apply_op("Attr", a="bad") self.assertEqual(str(cm.exception), "Expected int for argument 'a' not 'bad'.") with self.assertRaises(TypeError) as cm: self._lib.apply_op("Attr", a=[12]) self.assertEqual(str(cm.exception), "Expected int for argument 'a' not [12].") with self.assertRaises(TypeError) as cm: self._lib.apply_op("Attr", a=None) self.assertEqual(str(cm.exception), "Expected int for argument 'a' not None.") with self.assertRaises(TypeError) as cm: self._lib.apply_op("Attr") self.assertEqual(str(cm.exception), "No argument for attr a")
Example #23
Source File: constant_op.py From deep_image_model with Apache License 2.0 | 5 votes |
def _dimension_tensor_conversion_function(d, dtype=None, name=None, as_ref=False): _ = as_ref if d.value is None: raise ValueError("Cannot convert an unknown Dimension to a Tensor: %s" % d) if dtype is not None: if dtype not in (dtypes.int32, dtypes.int64): raise TypeError("Cannot convert a TensorShape to dtype: %s" % dtype) else: dtype = dtypes.int32 if name is None: name = "shape_as_tensor" return constant(d.value, dtype=dtype, name=name)
Example #24
Source File: tensor_shape_div_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testDivSucceeds(self): """Without from __future__ import division, __div__ should work.""" if six.PY2: # Old division exists only in Python 2 values = [tensor_shape.Dimension(x) for x in (3, 7, 11, None)] for x in values: for y in values: self.assertEqual((x / y).value, (x // y).value)
Example #25
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testEquality(self): s1 = tensor_shape.TensorShape([tensor_shape.Dimension( 3), tensor_shape.Dimension(4), tensor_shape.Dimension(7)]) s2 = tensor_shape.TensorShape([tensor_shape.Dimension( 3), tensor_shape.Dimension(4), tensor_shape.Dimension(7)]) s3 = tensor_shape.TensorShape([tensor_shape.Dimension(3), tensor_shape.Dimension(4), None]) self.assertTrue(s1 == s2) self.assertFalse(s1 != s2) self.assertFalse(s1 == "a string") self.assertTrue(s1 != "a string") self.assertNotEqual(s1, "347", "Should not equal an ambiguous string.") self.assertEqual(s1, ["3", "4", "7"]) # Test with an unknown shape in s3 self.assertTrue(s1 != s3) self.assertFalse(s3 == "a string") self.assertTrue(s3 != "a string") # eq and neq are not symmetric for unknown shapes. unk0 = tensor_shape.unknown_shape() self.assertFalse(unk0 == s1) self.assertFalse(s1 == unk0) with self.assertRaises(ValueError): unk0 != s1 # pylint: disable=pointless-statement with self.assertRaises(ValueError): s1 != unk0 # pylint: disable=pointless-statement unk1 = tensor_shape.unknown_shape() self.assertTrue(unk0 == unk1) self.assertTrue(unk1 == unk0) with self.assertRaises(ValueError): unk0 != unk1 # pylint: disable=pointless-statement with self.assertRaises(ValueError): unk1 != unk0 # pylint: disable=pointless-statement
Example #26
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testTruedivFails(self): unknown = tensor_shape.Dimension(None) self.assertEqual((unknown // unknown).value, None) with self.assertRaisesRegexp(TypeError, r"unsupported operand type"): unknown / unknown # pylint: disable=pointless-statement
Example #27
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testSlice(self): known = tensor_shape.TensorShape([0, 1, 2, 3, 4]) self.assertEqual(tensor_shape.Dimension(2), known[2]) tensor_shape.TensorShape([1, 2, 3]).assert_is_compatible_with(known[1:4]) unknown = tensor_shape.TensorShape(None) self.assertEqual(tensor_shape.Dimension(None).value, unknown[2].value) tensor_shape.TensorShape( [None, None, None]).assert_is_compatible_with(unknown[1:4])
Example #28
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testMergePartialShapes(self): s1 = tensor_shape.TensorShape([tensor_shape.Dimension( 3), tensor_shape.Dimension(None), tensor_shape.Dimension(7)]) s2 = tensor_shape.TensorShape([tensor_shape.Dimension( None), tensor_shape.Dimension(4), tensor_shape.Dimension(7)]) self.assertEqual([3, 4, 7], s1.merge_with(s2).as_list())
Example #29
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 5 votes |
def testStr(self): self.assertEqual(str(tensor_shape.Dimension(7)), "7") self.assertEqual(str(tensor_shape.Dimension(None)), "?")
Example #30
Source File: data_flow_ops.py From lambda-packs with MIT License | 5 votes |
def dequeue_many(self, n, name=None): """Dequeues and concatenates `n` elements from this queue. This operation concatenates queue-element component tensors along the 0th dimension to make a single component tensor. All of the components in the dequeued tuple will have size `n` in the 0th dimension. If the queue is closed and there are less than `n` elements left, then an `OutOfRange` exception is raised. At runtime, this operation may raise an error if the queue is @{tf.QueueBase.close} before or during its execution. If the queue is closed, the queue contains fewer than `n` elements, and there are no pending enqueue operations that can fulfill this request, `tf.errors.OutOfRangeError` will be raised. If the session is @{tf.Session.close}, `tf.errors.CancelledError` will be raised. Args: n: A scalar `Tensor` containing the number of elements to dequeue. name: A name for the operation (optional). Returns: The tuple of concatenated tensors that was dequeued. """ if name is None: name = "%s_DequeueMany" % self._name ret = gen_data_flow_ops._queue_dequeue_many_v2( self._queue_ref, n=n, component_types=self._dtypes, name=name) # NOTE(mrry): Not using a shape function because we need access to # the Queue object. op = ret[0].op batch_dim = tensor_shape.Dimension(tensor_util.constant_value(op.inputs[1])) for output, shape in zip(op.values(), self._shapes): output.set_shape(tensor_shape.TensorShape([batch_dim]).concatenate(shape)) return self._dequeue_return_value(ret)