Represents the value of one dimension in a TensorShape.
| 179 | |
| 180 | @tf_export(v1=["Dimension"]) |
| 181 | class Dimension(object): |
| 182 | """Represents the value of one dimension in a TensorShape.""" |
| 183 | |
| 184 | def __init__(self, value): |
| 185 | """Creates a new Dimension with the given value.""" |
| 186 | if value is None: |
| 187 | self._value = None |
| 188 | elif isinstance(value, Dimension): |
| 189 | self._value = value.value |
| 190 | elif isinstance(value, dtypes.DType): |
| 191 | raise TypeError("Cannot convert %s to Dimension" % value) |
| 192 | else: |
| 193 | self._value = int(value) |
| 194 | if (not isinstance(value, compat.bytes_or_text_types) and |
| 195 | self._value != value): |
| 196 | raise ValueError("Ambiguous dimension: %s" % value) |
| 197 | if self._value < 0: |
| 198 | raise ValueError("Dimension %d must be >= 0" % self._value) |
| 199 | |
| 200 | def __repr__(self): |
| 201 | return "Dimension(%s)" % repr(self._value) |
| 202 | |
| 203 | def __str__(self): |
| 204 | value = self._value |
| 205 | return "?" if value is None else str(value) |
| 206 | |
| 207 | def __eq__(self, other): |
| 208 | """Returns true if `other` has the same known value as this Dimension.""" |
| 209 | try: |
| 210 | other = as_dimension(other) |
| 211 | except (TypeError, ValueError): |
| 212 | return NotImplemented |
| 213 | if self._value is None or other.value is None: |
| 214 | return None |
| 215 | return self._value == other.value |
| 216 | |
| 217 | def __ne__(self, other): |
| 218 | """Returns true if `other` has a different known value from `self`.""" |
| 219 | try: |
| 220 | other = as_dimension(other) |
| 221 | except (TypeError, ValueError): |
| 222 | return NotImplemented |
| 223 | if self._value is None or other.value is None: |
| 224 | return None |
| 225 | return self._value != other.value |
| 226 | |
| 227 | def __int__(self): |
| 228 | return self._value |
| 229 | |
| 230 | # This is needed for Windows. |
| 231 | # See https://github.com/tensorflow/tensorflow/pull/9780 |
| 232 | def __long__(self): |
| 233 | return self._value |
| 234 | |
| 235 | def __index__(self): |
| 236 | # Allow use in Python 3 range |
| 237 | return self._value |
| 238 |
no outgoing calls
no test coverage detected