MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / _constant_impl

Function _constant_impl

tensorflow/python/framework/constant_op.py:230–272  ·  view source on GitHub ↗

Implementation of constant.

(
    value, dtype, shape, name, verify_shape, allow_broadcast)

Source from the content-addressed store, hash-verified

228
229
230def _constant_impl(
231 value, dtype, shape, name, verify_shape, allow_broadcast):
232 """Implementation of constant."""
233 ctx = context.context()
234 if ctx.executing_eagerly():
235 t = convert_to_eager_tensor(value, ctx, dtype)
236 if shape is None:
237 return t
238 shape = tensor_shape.as_shape(shape)
239 if shape == t.shape:
240 return t
241 if verify_shape:
242 raise TypeError("Expected Tensor's shape: %s, got %s." % (tuple(shape),
243 tuple(t.shape)))
244 num_t = t.shape.num_elements()
245 # TODO(josh11b): Implement shape -> eager tensor conversion.
246 if num_t == shape.num_elements():
247 return _eager_reshape(t, shape.as_list(), ctx)
248 if num_t == 1:
249 if t.dtype == dtypes.bool:
250 # We don't have a Fill kernel for bool dtype on GPU. So we first run
251 # Fill on CPU and then copy to GPU if needed.
252 with ops.device("/device:CPU:0"):
253 x = _eager_fill(shape.as_list(), t.cpu(), ctx)
254 return _eager_identity(x, ctx)
255 else:
256 return _eager_fill(shape.as_list(), t, ctx)
257 raise TypeError("Eager execution of tf.constant with unsupported shape "
258 "(value has %d elements, shape is %s with %d elements)." %
259 (num_t, shape, shape.num_elements()))
260 g = ops.get_default_graph()
261 tensor_value = attr_value_pb2.AttrValue()
262 tensor_value.tensor.CopyFrom(
263 tensor_util.make_tensor_proto(
264 value, dtype=dtype, shape=shape, verify_shape=verify_shape,
265 allow_broadcast=allow_broadcast))
266 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)
267 const_tensor = g.create_op(
268 "Const", [], [dtype_value.type],
269 attrs={"value": tensor_value,
270 "dtype": dtype_value},
271 name=name).outputs[0]
272 return const_tensor
273
274
275def is_constant(tensor_or_op):

Callers 2

constant_v1Function · 0.85
constantFunction · 0.85

Calls 13

convert_to_eager_tensorFunction · 0.85
tupleFunction · 0.85
_eager_reshapeFunction · 0.85
_eager_fillFunction · 0.85
_eager_identityFunction · 0.85
executing_eagerlyMethod · 0.80
cpuMethod · 0.80
contextMethod · 0.45
num_elementsMethod · 0.45
as_listMethod · 0.45
deviceMethod · 0.45
CopyFromMethod · 0.45

Tested by

no test coverage detected