Internal implementation for the v1/v2 zeros_like API calls.
(tensor, dtype, name, optimize=True)
| 2424 | |
| 2425 | |
| 2426 | def zeros_like_impl(tensor, dtype, name, optimize=True): |
| 2427 | """Internal implementation for the v1/v2 zeros_like API calls.""" |
| 2428 | with ops.name_scope(name, "zeros_like", [tensor]) as name: |
| 2429 | if not tensor_util.is_tensor(tensor): |
| 2430 | tensor = ops.convert_to_tensor(tensor, name="tensor") |
| 2431 | tensor_shape = tensor.shape |
| 2432 | tensor_dtype = tensor.dtype |
| 2433 | |
| 2434 | if context.executing_eagerly(): |
| 2435 | if dtype is not None and dtype != tensor_dtype: |
| 2436 | return zeros( |
| 2437 | shape_internal(tensor, optimize=optimize), dtype=dtype, name=name) |
| 2438 | with ops.device(tensor.device): |
| 2439 | return gen_array_ops.zeros_like(tensor, name=name) |
| 2440 | |
| 2441 | # For now, variant types must be created via zeros_like; as we need to |
| 2442 | # pass the input variant object to the proper zeros callback. |
| 2443 | |
| 2444 | if (optimize and tensor_shape.is_fully_defined() and |
| 2445 | tensor_dtype != dtypes.variant): |
| 2446 | # We can produce a zeros tensor independent of the value of 'tensor', |
| 2447 | # since the shape is known statically. |
| 2448 | return zeros(tensor_shape, dtype=dtype or tensor_dtype, name=name) |
| 2449 | |
| 2450 | if dtype is not None and dtype != tensor_dtype and dtype != dtypes.variant: |
| 2451 | return zeros( |
| 2452 | shape_internal(tensor, optimize=optimize), dtype=dtype, name=name) |
| 2453 | else: |
| 2454 | return gen_array_ops.zeros_like(tensor, name=name) |
| 2455 | |
| 2456 | |
| 2457 | @tf_export(v1=["ones_like"]) |
no test coverage detected