Python tensorflow.python.framework.tensor_shape.as_shape() Examples
The following are 30
code examples of tensorflow.python.framework.tensor_shape.as_shape().
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_signature.py From keras-lambda with MIT License | 6 votes |
def is_compatible_with(self, other): """Returns True if signatures are compatible.""" def _shape_is_compatible_0dim(this, other): """Checks that shapes are compatible skipping dim 0.""" other = tensor_shape.as_shape(other) # If shapes are None (unknown) they may be compatible. if this.dims is None or other.dims is None: return True if this.ndims != other.ndims: return False for dim, (x_dim, y_dim) in enumerate(zip(this.dims, other.dims)): if dim == 0: continue if not x_dim.is_compatible_with(y_dim): return False return True if other.is_sparse: return self.is_sparse and self.dtype.is_compatible_with(other.dtype) return (self.dtype.is_compatible_with(other.dtype) and _shape_is_compatible_0dim(self.shape, other.shape) and not self.is_sparse)
Example #2
Source File: rnn_cell_impl.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #3
Source File: tensor_signature.py From lambda-packs with MIT License | 6 votes |
def is_compatible_with(self, other): """Returns True if signatures are compatible.""" def _shape_is_compatible_0dim(this, other): """Checks that shapes are compatible skipping dim 0.""" other = tensor_shape.as_shape(other) # If shapes are None (unknown) they may be compatible. if this.dims is None or other.dims is None: return True if this.ndims != other.ndims: return False for dim, (x_dim, y_dim) in enumerate(zip(this.dims, other.dims)): if dim == 0: continue if not x_dim.is_compatible_with(y_dim): return False return True if other.is_sparse: return self.is_sparse and self.dtype.is_compatible_with(other.dtype) return (self.dtype.is_compatible_with(other.dtype) and _shape_is_compatible_0dim(self.shape, other.shape) and not self.is_sparse)
Example #4
Source File: op_def_library.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def _MakeShape(v, arg_name): """Convert v into a TensorShapeProto.""" # Args: # v: A TensorShapeProto, a list of ints, or a tensor_shape.TensorShape. # arg_name: String, for error messages. # Returns: # A TensorShapeProto. if isinstance(v, tensor_shape_pb2.TensorShapeProto): for d in v.dim: if d.name: logging.warning("Warning: TensorShapeProto with a named dimension: %s", str(v)) break return v return tensor_shape.as_shape(v).as_proto()
Example #5
Source File: tensor_signature.py From auto-alt-text-lambda-api with MIT License | 6 votes |
def is_compatible_with(self, other): """Returns True if signatures are compatible.""" def _shape_is_compatible_0dim(this, other): """Checks that shapes are compatible skipping dim 0.""" other = tensor_shape.as_shape(other) # If shapes are None (unknown) they may be compatible. if this.dims is None or other.dims is None: return True if this.ndims != other.ndims: return False for dim, (x_dim, y_dim) in enumerate(zip(this.dims, other.dims)): if dim == 0: continue if not x_dim.is_compatible_with(y_dim): return False return True if other.is_sparse: return self.is_sparse and self.dtype.is_compatible_with(other.dtype) return (self.dtype.is_compatible_with(other.dtype) and _shape_is_compatible_0dim(self.shape, other.shape) and not self.is_sparse)
Example #6
Source File: rnn_cell.py From ROLO with Apache License 2.0 | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #7
Source File: predict_utils.py From model_server with Apache License 2.0 | 6 votes |
def _prepare_output_as_AppendArrayToTensorProto( inference_output, model_available_outputs): response = predict_pb2.PredictResponse() for response_output_name, model_output_name in \ model_available_outputs.items(): if model_output_name in inference_output: dtype = dtypes.as_dtype(inference_output[model_output_name].dtype) output_tensor = tensor_pb2.TensorProto( dtype=dtype.as_datatype_enum, tensor_shape=tensor_shape.as_shape( inference_output[model_output_name].shape).as_proto()) result = inference_output[model_output_name].flatten() tensor_util._NP_TO_APPEND_FN[dtype.as_numpy_dtype](output_tensor, result) response.outputs[response_output_name].CopyFrom(output_tensor) return response
Example #8
Source File: rnn_cell.py From deep_image_model with Apache License 2.0 | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #9
Source File: tensor_shape_test.py From deep_image_model with Apache License 2.0 | 6 votes |
def testConvertFromProto(self): def make_tensor_shape_proto(shape): return tensor_shape_pb2.TensorShapeProto( dim=[tensor_shape_pb2.TensorShapeProto.Dim(size=x) for x in shape]) proto = make_tensor_shape_proto([]) self.assertEqual(tensor_shape.TensorShape([]), tensor_shape.TensorShape(proto)) self.assertEqual(tensor_shape.TensorShape([]), tensor_shape.as_shape(proto)) proto = make_tensor_shape_proto([1, 37, 42]) self.assertEqual(tensor_shape.TensorShape([1, 37, 42]), tensor_shape.TensorShape(proto)) self.assertEqual(tensor_shape.TensorShape([1, 37, 42]), tensor_shape.as_shape(proto)) partial_proto_shape = tensor_shape.as_shape( make_tensor_shape_proto([-1, 37, 42])) partial_shape = tensor_shape.TensorShape([None, 37, 42]) self.assertNotEqual(partial_proto_shape, partial_shape) self.assertEqual(partial_proto_shape[0].value, None) self.assertEqual(partial_proto_shape[1].value, 37) self.assertEqual(partial_proto_shape[2].value, 42) self.assertTrue(partial_shape.is_compatible_with(partial_proto_shape))
Example #10
Source File: op_def_library.py From deep_image_model with Apache License 2.0 | 6 votes |
def _MakeShape(v, arg_name): """Convert v into a TensorShapeProto.""" # Args: # v: A TensorShapeProto, a list of ints, or a tensor_shape.TensorShape. # arg_name: String, for error messages. # Returns: # A TensorShapeProto. if isinstance(v, tensor_shape_pb2.TensorShapeProto): for d in v.dim: if d.name: logging.warning("Warning: TensorShapeProto with a named dimension: %s", str(v)) break return v return tensor_shape.as_shape(v).as_proto()
Example #11
Source File: rnn_cell.py From ecm with Apache License 2.0 | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #12
Source File: modules.py From Cross-Modal-Projection-Learning with MIT License | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #13
Source File: op_def_library.py From keras-lambda with MIT License | 6 votes |
def _MakeShape(v, arg_name): """Convert v into a TensorShapeProto.""" # Args: # v: A TensorShapeProto, a list of ints, or a tensor_shape.TensorShape. # arg_name: String, for error messages. # Returns: # A TensorShapeProto. if isinstance(v, tensor_shape_pb2.TensorShapeProto): for d in v.dim: if d.name: logging.warning("Warning: TensorShapeProto with a named dimension: %s", str(v)) break return v return tensor_shape.as_shape(v).as_proto()
Example #14
Source File: rnn_cell_impl.py From keras-lambda with MIT License | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. Args: state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. Returns: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #15
Source File: op_def_library.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def _MakeShape(v, arg_name): """Convert v into a TensorShapeProto.""" # Args: # v: A TensorShapeProto, a list of ints, or a tensor_shape.TensorShape. # arg_name: String, for error messages. # Returns: # A TensorShapeProto. if isinstance(v, tensor_shape_pb2.TensorShapeProto): for d in v.dim: if d.name: logging.warning("Warning: TensorShapeProto with a named dimension: %s", str(v)) break return v try: return tensor_shape.as_shape(v).as_proto() except TypeError as e: raise TypeError("Error converting %s to a TensorShape: %s" % (arg_name, e)) except ValueError as e: raise ValueError("Error converting %s to a TensorShape: %s" % (arg_name, e))
Example #16
Source File: execute.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 6 votes |
def make_shape(v, arg_name): """Convert v into a list.""" # Args: # v: A TensorShapeProto, a list of ints, or a tensor_shape.TensorShape. # arg_name: String, for error messages. # Returns: # None if the rank is unknown, otherwise a list of ints (or Nones in the # position where the dimension is unknown). try: shape = tensor_shape.as_shape(v) except TypeError as e: raise TypeError("Error converting %s to a TensorShape: %s." % (arg_name, e)) except ValueError as e: raise ValueError("Error converting %s to a TensorShape: %s." % (arg_name, e)) if shape.ndims is None: return None else: return shape.as_list()
Example #17
Source File: rnn.py From pinn with MIT License | 6 votes |
def _generate_zero_filled_state(batch_size_tensor, state_size, dtype): """Generate a zero filled tensor with shape [batch_size, state_size].""" if None in [batch_size_tensor, dtype]: raise ValueError( 'batch_size and dtype cannot be None while constructing initial state: ' 'batch_size={}, dtype={}'.format(batch_size_tensor, dtype)) if _is_multiple_state(state_size): states = [] for dims in state_size: flat_dims = tensor_shape.as_shape(dims).as_list() init_state_size = [batch_size_tensor] + flat_dims init_state = array_ops.zeros(init_state_size, dtype=dtype) states.append(init_state) return states else: flat_dims = tensor_shape.as_shape(state_size).as_list() init_state_size = [batch_size_tensor] + flat_dims return array_ops.zeros(init_state_size, dtype=dtype)
Example #18
Source File: beam_search_decoder_from_tensorflow.py From tensorflow_end2end_speech_recognition with MIT License | 6 votes |
def _merge_batch_beams(self, t, s=None): """Merges the tensor from a batch of beams into a batch by beams. More exactly, t is a tensor of dimension [batch_size, beam_width, s]. We reshape this into [batch_size*beam_width, s] Args: t: Tensor of dimension [batch_size, beam_width, s] s: (Possibly known) depth shape. Returns: A reshaped version of t with dimension [batch_size * beam_width, s]. """ if isinstance(s, ops.Tensor): s = tensor_shape.as_shape(tensor_util.constant_value(s)) else: s = tensor_shape.TensorShape(s) t_shape = tf.shape(t) static_batch_size = tensor_util.constant_value(self._batch_size) batch_size_beam_width = ( None if static_batch_size is None else static_batch_size * self._beam_width) reshaped_t = tf.reshape( t, tf.concat( ([self._batch_size * self._beam_width], t_shape[2:]), 0)) reshaped_t.set_shape( (tensor_shape.TensorShape([batch_size_beam_width]).concatenate(s))) return reshaped_t
Example #19
Source File: tf_ops.py From safekit with MIT License | 6 votes |
def _state_size_with_prefix(state_size, prefix=None): """Helper function that enables int or TensorShape shape specification. This function takes a size specification, which can be an integer or a TensorShape, and converts it into a list of integers. One may specify any additional dimensions that precede the final state size specification. :param state_size: TensorShape or int that specifies the size of a tensor. prefix: optional additional list of dimensions to prepend. :return: result_state_size: list of dimensions the resulting tensor size. """ result_state_size = tensor_shape.as_shape(state_size).as_list() if prefix is not None: if not isinstance(prefix, list): raise TypeError("prefix of _state_size_with_prefix should be a list.") result_state_size = prefix + result_state_size return result_state_size
Example #20
Source File: tensor_signature.py From deep_image_model with Apache License 2.0 | 6 votes |
def is_compatible_with(self, other): """Returns True if signatures are compatible.""" def _shape_is_compatible_0dim(this, other): """Checks that shapes are compatible skipping dim 0.""" other = tensor_shape.as_shape(other) # If shapes are None (unknown) they may be compatible. if this.dims is None or other.dims is None: return True if this.ndims != other.ndims: return False for dim, (x_dim, y_dim) in enumerate(zip(this.dims, other.dims)): if dim == 0: continue if not x_dim.is_compatible_with(y_dim): return False return True if other.is_sparse: return self.is_sparse and self.dtype.is_compatible_with(other.dtype) return (self.dtype.is_compatible_with(other.dtype) and _shape_is_compatible_0dim(self.shape, other.shape) and not self.is_sparse)
Example #21
Source File: input.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _merge_shapes(shape_list, enqueue_many): shape_list = [tensor_shape.as_shape(s) for s in shape_list] if enqueue_many: # We want the shapes without the leading batch dimension. shape_list = [s.with_rank_at_least(1)[1:] for s in shape_list] merged_shape = shape_list[0] for s in shape_list[1:]: merged_shape.merge_with(s) return merged_shape.as_list()
Example #22
Source File: rnn_beam_search_decoder.py From OpenSeq2Seq with Apache License 2.0 | 5 votes |
def _merge_batch_beams(self, t, s=None): """Merges the tensor from a batch of beams into a batch by beams. More exactly, t is a tensor of dimension [batch_size, beam_width, s]. We reshape this into [batch_size*beam_width, s] Args: t: Tensor of dimension [batch_size, beam_width, s] s: (Possibly known) depth shape. Returns: A reshaped version of t with dimension [batch_size * beam_width, s]. """ if isinstance(s, ops.Tensor): s = tensor_shape.as_shape(tensor_util.constant_value(s)) else: s = tensor_shape.TensorShape(s) t_shape = array_ops.shape(t) static_batch_size = tensor_util.constant_value(self._batch_size) batch_size_beam_width = ( None if static_batch_size is None else static_batch_size * self._beam_width) reshaped_t = array_ops.reshape( t, array_ops.concat(([self._batch_size * self._beam_width], t_shape[2:]), 0)) reshaped_t.set_shape( (tensor_shape.TensorShape([batch_size_beam_width]).concatenate(s))) return reshaped_t
Example #23
Source File: quantize_graph.py From sketch-to-react-native with MIT License | 5 votes |
def set_attr_shape(node, key, value): try: node.attr[key].CopyFrom( attr_value_pb2.AttrValue(shape=tensor_shape.as_shape(value).as_proto())) except KeyError: pass
Example #24
Source File: quantize_graph.py From pokemon-mini with Apache License 2.0 | 5 votes |
def set_attr_shape(node, key, value): try: node.attr[key].CopyFrom( attr_value_pb2.AttrValue(shape=tensor_shape.as_shape(value).as_proto())) except KeyError: pass
Example #25
Source File: dataset_ops.py From lambda-packs with MIT License | 5 votes |
def _partial_shape_to_tensor(shape_like): try: # First attempt to convert the input to a shape, and return the # "canonical" tensor representation, which uses `-1` in place of # `None`. shape_like = tensor_shape.as_shape(shape_like) return ops.convert_to_tensor( [dim if dim is not None else -1 for dim in shape_like.as_list()], dtype=dtypes.int64) except (TypeError, ValueError): # The argument was not trivially convertible to a # `tf.TensorShape`, so fall back on the conversion to tensor # machinery. return ops.convert_to_tensor(shape_like, dtype=dtypes.int64)
Example #26
Source File: quantize_graph.py From AudioNet with MIT License | 5 votes |
def set_attr_shape(node, key, value): try: node.attr[key].CopyFrom( attr_value_pb2.AttrValue(shape=tensor_shape.as_shape(value).as_proto())) except KeyError: pass
Example #27
Source File: dataset_ops.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _partial_shape_to_tensor(shape_like): try: # First attempt to convert the input to a shape, and return the # "canonical" tensor representation, which uses `-1` in place of # `None`. shape_like = tensor_shape.as_shape(shape_like) return ops.convert_to_tensor( [dim if dim is not None else -1 for dim in shape_like.as_list()], dtype=dtypes.int64) except (TypeError, ValueError): # The argument was not trivially convertible to a # `tf.TensorShape`, so fall back on the conversion to tensor # machinery. return ops.convert_to_tensor(shape_like, dtype=dtypes.int64)
Example #28
Source File: data_flow_ops.py From Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda with MIT License | 5 votes |
def _as_shape_list(shapes, dtypes, unknown_dim_allowed=False, unknown_rank_allowed=False): """Convert shapes to a list of tuples of int (or None).""" del dtypes if unknown_dim_allowed: if (not isinstance(shapes, collections.Sequence) or not shapes or any(shape is None or isinstance(shape, int) for shape in shapes)): raise ValueError( "When providing partial shapes, a list of shapes must be provided.") if shapes is None: return None if isinstance(shapes, tensor_shape.TensorShape): shapes = [shapes] if not isinstance(shapes, (tuple, list)): raise TypeError( "shapes must be a TensorShape or a list or tuple of TensorShapes.") if all(shape is None or isinstance(shape, int) for shape in shapes): # We have a single shape. shapes = [shapes] shapes = [tensor_shape.as_shape(shape) for shape in shapes] if not unknown_dim_allowed: if any([not shape.is_fully_defined() for shape in shapes]): raise ValueError("All shapes must be fully defined: %s" % shapes) if not unknown_rank_allowed: if any([shape.dims is None for shape in shapes]): raise ValueError("All shapes must have a defined rank: %s" % shapes) return shapes
Example #29
Source File: array_ops.py From keras-lambda with MIT License | 5 votes |
def ones(shape, dtype=dtypes.float32, name=None): """Creates a tensor with all elements set to 1. This operation returns a tensor of type `dtype` with shape `shape` and all elements set to 1. For example: ```python tf.ones([2, 3], tf.int32) ==> [[1, 1, 1], [1, 1, 1]] ``` Args: shape: Either a list of integers, or a 1-D `Tensor` of type `int32`. dtype: The type of an element in the resulting `Tensor`. name: A name for the operation (optional). Returns: A `Tensor` with all elements set to 1. """ dtype = dtypes.as_dtype(dtype).base_dtype with ops.name_scope(name, "ones", [shape]) as name: one = True if dtype == dtypes.bool else 1 try: shape = tensor_shape.as_shape(shape) output = constant(one, shape=shape, dtype=dtype, name=name) except (TypeError, ValueError): shape = ops.convert_to_tensor(shape, dtype=dtypes.int32, name="shape") output = fill(shape, constant(one, dtype=dtype), name=name) assert output.dtype.base_dtype == dtype return output
Example #30
Source File: array_ops.py From lambda-packs with MIT License | 5 votes |
def zeros(shape, dtype=dtypes.float32, name=None): """Creates a tensor with all elements set to zero. This operation returns a tensor of type `dtype` with shape `shape` and all elements set to zero. For example: ```python tf.zeros([3, 4], tf.int32) ==> [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] ``` Args: shape: Either a list of integers, or a 1-D `Tensor` of type `int32`. dtype: The type of an element in the resulting `Tensor`. name: A name for the operation (optional). Returns: A `Tensor` with all elements set to zero. """ dtype = dtypes.as_dtype(dtype).base_dtype with ops.name_scope(name, "zeros", [shape]) as name: if dtype == dtypes.bool: zero = False elif dtype == dtypes.string: zero = "" else: zero = 0 try: shape = tensor_shape.as_shape(shape) output = constant(zero, shape=shape, dtype=dtype, name=name) except (TypeError, ValueError): shape = ops.convert_to_tensor(shape, dtype=dtypes.int32, name="shape") output = fill(shape, constant(zero, dtype=dtype), name=name) assert output.dtype.base_dtype == dtype return output