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: A list of integers,
(shape, dtype=dtypes.float32, name=None)
| 2301 | |
| 2302 | @tf_export("zeros") |
| 2303 | def zeros(shape, dtype=dtypes.float32, name=None): |
| 2304 | """Creates a tensor with all elements set to zero. |
| 2305 | |
| 2306 | This operation returns a tensor of type `dtype` with shape `shape` and |
| 2307 | all elements set to zero. |
| 2308 | |
| 2309 | For example: |
| 2310 | |
| 2311 | ```python |
| 2312 | tf.zeros([3, 4], tf.int32) # [[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]] |
| 2313 | ``` |
| 2314 | |
| 2315 | Args: |
| 2316 | shape: A list of integers, a tuple of integers, or a 1-D `Tensor` of type |
| 2317 | `int32`. |
| 2318 | dtype: The type of an element in the resulting `Tensor`. |
| 2319 | name: A name for the operation (optional). |
| 2320 | |
| 2321 | Returns: |
| 2322 | A `Tensor` with all elements set to zero. |
| 2323 | """ |
| 2324 | dtype = dtypes.as_dtype(dtype).base_dtype |
| 2325 | with ops.name_scope(name, "zeros", [shape]) as name: |
| 2326 | if dtype == dtypes.bool: |
| 2327 | zero = False |
| 2328 | elif dtype == dtypes.string: |
| 2329 | zero = "" |
| 2330 | else: |
| 2331 | zero = 0 |
| 2332 | |
| 2333 | if not isinstance(shape, ops.Tensor): |
| 2334 | try: |
| 2335 | # Create a constant if it won't be very big. Otherwise create a fill op |
| 2336 | # to prevent serialized GraphDefs from becoming too large. |
| 2337 | output = _constant_if_small(zero, shape, dtype, name) |
| 2338 | if output is not None: |
| 2339 | return output |
| 2340 | |
| 2341 | # Go through tensor shapes to get int64-if-needed semantics |
| 2342 | shape = constant_op._tensor_shape_tensor_conversion_function( |
| 2343 | tensor_shape.TensorShape(shape)) |
| 2344 | except (TypeError, ValueError): |
| 2345 | # Happens when shape is a list with tensor elements |
| 2346 | shape = ops.convert_to_tensor(shape, dtype=dtypes.int32) |
| 2347 | if not shape._shape_tuple(): |
| 2348 | shape = reshape(shape, [-1]) # Ensure it's a vector |
| 2349 | output = fill(shape, constant(zero, dtype=dtype), name=name) |
| 2350 | assert output.dtype.base_dtype == dtype |
| 2351 | return output |
| 2352 | |
| 2353 | |
| 2354 | @tf_export(v1=["zeros_like"]) |