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

Function _ConstantValue

tensorflow/python/framework/tensor_util.py:656–764  ·  view source on GitHub ↗
(tensor, partial)

Source from the content-addressed store, hash-verified

654
655
656def _ConstantValue(tensor, partial):
657 # TODO(touts): Support Variables?
658 if not isinstance(tensor, ops.Tensor):
659 raise TypeError("%r is not a Tensor, has type %s" % (tensor, type(tensor)))
660 if tensor.op.type == "Const":
661 return MakeNdarray(tensor.op.get_attr("value"))
662 elif tensor.op.type == "Shape":
663 input_shape = tensor.op.inputs[0].get_shape()
664 if input_shape.is_fully_defined():
665 return np.array(
666 [dim.value for dim in input_shape.dims],
667 dtype=tensor.dtype.as_numpy_dtype)
668 else:
669 return None
670 elif tensor.op.type == "Size":
671 input_shape = tensor.op.inputs[0].get_shape()
672 if input_shape.is_fully_defined():
673 return np.prod([dim.value for dim in input_shape.dims], dtype=np.int32)
674 else:
675 return None
676 elif tensor.op.type == "Rank":
677 input_shape = tensor.op.inputs[0].get_shape()
678 if input_shape.ndims is not None:
679 return np.ndarray(
680 shape=(),
681 buffer=np.array([input_shape.ndims], dtype=np.int32),
682 dtype=np.int32)
683 else:
684 return None
685 elif tensor.op.type == "Range":
686 start = constant_value(tensor.op.inputs[0])
687 if start is None:
688 return None
689 limit = constant_value(tensor.op.inputs[1])
690 if limit is None:
691 return None
692 delta = constant_value(tensor.op.inputs[2])
693 if delta is None:
694 return None
695 return np.arange(start, limit, delta, dtype=tensor.dtype.as_numpy_dtype)
696 elif tensor.op.type == "Cast":
697 pre_cast = constant_value(tensor.op.inputs[0])
698 if pre_cast is None:
699 return None
700 cast_dtype = dtypes.as_dtype(tensor.op.get_attr("DstT"))
701 return pre_cast.astype(cast_dtype.as_numpy_dtype)
702 elif tensor.op.type == "Concat":
703 dim = constant_value(tensor.op.inputs[0])
704 if dim is None:
705 return None
706 values = []
707 for x in tensor.op.inputs[1:]:
708 value = constant_value(x)
709 if value is None:
710 return None
711 values.append(value)
712 return np.concatenate(values, axis=dim)
713 elif tensor.op.type == "ConcatV2":

Callers 1

constant_valueFunction · 0.85

Calls 12

typeFunction · 0.85
MakeNdarrayFunction · 0.85
is_fully_definedMethod · 0.80
fullMethod · 0.80
equalMethod · 0.80
not_equalMethod · 0.80
constant_valueFunction · 0.70
get_attrMethod · 0.45
get_shapeMethod · 0.45
appendMethod · 0.45
concatenateMethod · 0.45
as_listMethod · 0.45

Tested by

no test coverage detected