Concat that enables int, Tensor, or TensorShape values. This function takes a size specification, which can be an integer, a TensorShape, or a Tensor, and converts it into a concatenated Tensor (if static = False) or a list of integers (if static = True). Args: prefix: The prefix; usua
(prefix, suffix, static=False)
| 103 | |
| 104 | |
| 105 | def _concat(prefix, suffix, static=False): |
| 106 | """Concat that enables int, Tensor, or TensorShape values. |
| 107 | |
| 108 | This function takes a size specification, which can be an integer, a |
| 109 | TensorShape, or a Tensor, and converts it into a concatenated Tensor |
| 110 | (if static = False) or a list of integers (if static = True). |
| 111 | |
| 112 | Args: |
| 113 | prefix: The prefix; usually the batch size (and/or time step size). |
| 114 | (TensorShape, int, or Tensor.) |
| 115 | suffix: TensorShape, int, or Tensor. |
| 116 | static: If `True`, return a python list with possibly unknown dimensions. |
| 117 | Otherwise return a `Tensor`. |
| 118 | |
| 119 | Returns: |
| 120 | shape: the concatenation of prefix and suffix. |
| 121 | |
| 122 | Raises: |
| 123 | ValueError: if `suffix` is not a scalar or vector (or TensorShape). |
| 124 | ValueError: if prefix or suffix was `None` and asked for dynamic |
| 125 | Tensors out. |
| 126 | """ |
| 127 | if isinstance(prefix, ops.Tensor): |
| 128 | p = prefix |
| 129 | p_static = tensor_util.constant_value(prefix) |
| 130 | if p.shape.ndims == 0: |
| 131 | p = array_ops.expand_dims(p, 0) |
| 132 | elif p.shape.ndims != 1: |
| 133 | raise ValueError("prefix tensor must be either a scalar or vector, " |
| 134 | "but saw tensor: %s" % p) |
| 135 | else: |
| 136 | p = tensor_shape.as_shape(prefix) |
| 137 | p_static = p.as_list() if p.ndims is not None else None |
| 138 | p = ( |
| 139 | constant_op.constant(p.as_list(), dtype=dtypes.int32) |
| 140 | if p.is_fully_defined() else None) |
| 141 | if isinstance(suffix, ops.Tensor): |
| 142 | s = suffix |
| 143 | s_static = tensor_util.constant_value(suffix) |
| 144 | if s.shape.ndims == 0: |
| 145 | s = array_ops.expand_dims(s, 0) |
| 146 | elif s.shape.ndims != 1: |
| 147 | raise ValueError("suffix tensor must be either a scalar or vector, " |
| 148 | "but saw tensor: %s" % s) |
| 149 | else: |
| 150 | s = tensor_shape.as_shape(suffix) |
| 151 | s_static = s.as_list() if s.ndims is not None else None |
| 152 | s = ( |
| 153 | constant_op.constant(s.as_list(), dtype=dtypes.int32) |
| 154 | if s.is_fully_defined() else None) |
| 155 | |
| 156 | if static: |
| 157 | shape = tensor_shape.as_shape(p_static).concatenate(s_static) |
| 158 | shape = shape.as_list() if shape.ndims is not None else None |
| 159 | else: |
| 160 | if p is None or s is None: |
| 161 | raise ValueError("Provided a prefix or suffix of None: %s and %s" % |
| 162 | (prefix, suffix)) |