1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Graph-only versions of a few op functions, for internal use only.""" 16 17# Must be separate from array_ops to avoid a cyclic dependency. 18 19from __future__ import absolute_import 20from __future__ import division 21from __future__ import print_function 22 23from tensorflow.core.framework import attr_value_pb2 24from tensorflow.python.framework import ops 25from tensorflow.python.framework import tensor_shape 26 27 28def graph_zeros_like(tensor): 29 """Graph-only version of tf.zeros_like(), for internal use only.""" 30 g = ops._get_graph_from_inputs([tensor]) # pylint: disable=protected-access 31 with g.as_default(), ops.name_scope(None, "zeros_like", [tensor]) as name: 32 tensor = ops.convert_to_tensor(tensor, name="tensor") 33 dtype = tensor.dtype.base_dtype 34 dtype_value = attr_value_pb2.AttrValue(type=dtype.as_datatype_enum) 35 op = g.create_op("ZerosLike", [tensor], [dtype], input_types=[dtype], 36 attrs={"T": dtype_value}, name=name) 37 result, = op.outputs 38 return result 39 40 41def graph_placeholder(dtype, shape, name=None): 42 """Graph-only version of tf.placeholder(), for internal use only.""" 43 dtype = dtype.base_dtype 44 dtype_value = attr_value_pb2.AttrValue(type=dtype.as_datatype_enum) 45 if isinstance(shape, (list, tuple)): 46 shape = tensor_shape.TensorShape(shape) 47 shape = attr_value_pb2.AttrValue(shape=shape.as_proto()) 48 g = ops.get_default_graph() 49 with ops.name_scope(name, "placeholder", []) as name: 50 op = g.create_op("Placeholder", [], [dtype], input_types=[], 51 attrs={"dtype": dtype_value, "shape": shape}, name=name) 52 result, = op.outputs 53 return result 54