本文整理汇总了Python中tensorflow.python.framework.ops.EagerTensor方法的典型用法代码示例。如果您正苦于以下问题:Python ops.EagerTensor方法的具体用法?Python ops.EagerTensor怎么用?Python ops.EagerTensor使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.python.framework.ops
的用法示例。
在下文中一共展示了ops.EagerTensor方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_with_tensors
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def test_with_tensors(self):
tf.reset_default_graph()
tf.set_random_seed(1)
things = tf.convert_to_tensor(np.array([[0, 0], [1, 0], [2, 0.5673]], dtype=np.float32))
entity_relation = lambda x: x
continuous_attribute = lambda x: x
encoders_for_types = {lambda: entity_relation: [0, 1], lambda: continuous_attribute: [2]}
tm = TypewiseEncoder(encoders_for_types, 1)
encoded_things = tm(things) # The function under test
# Check that tensorflow was actually used
self.assertEqual(EagerTensor, type(encoded_things))
示例2: from_tensor
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def from_tensor(cls, tensor, name=None):
if isinstance(tensor, ops.EagerTensor):
return TensorSpec(tensor.shape, tensor.dtype, name)
elif isinstance(tensor, ops.Tensor):
return TensorSpec(tensor.shape, tensor.dtype, name or tensor.op.name)
else:
raise ValueError("`tensor` should be a tf.Tensor")
示例3: args_to_matching_eager
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def args_to_matching_eager(l, ctx, default_dtype=None):
"""Convert sequence `l` to eager same-type Tensors."""
EagerTensor = ops.EagerTensor # pylint: disable=invalid-name
if all(isinstance(x, EagerTensor) for x in l):
return l[0].dtype, l
# TODO(josh11b): Could we do a better job if we also passed in the
# allowed dtypes when that was known?
# Is some input already a Tensor with a dtype?
dtype = None
for t in l:
if isinstance(t, EagerTensor):
dtype = t.dtype
break
internal_convert_to_tensor = ops.internal_convert_to_tensor
if dtype is None:
# Infer a dtype based on the first value, and use that dtype for the
# remaining values.
ret = []
for t in l:
ret.append(internal_convert_to_tensor(
t, dtype, preferred_dtype=default_dtype, ctx=ctx))
if dtype is None:
dtype = ret[-1].dtype
else:
ret = [internal_convert_to_tensor(t, dtype, ctx=ctx) for t in l]
return dtype, ret
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:31,代码来源:execute.py
示例4: convert_to_mixed_eager_tensors
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def convert_to_mixed_eager_tensors(values, ctx):
v = [t if isinstance(t, ops.EagerTensor) else ops.EagerTensor(t, ctx)
for t in values]
types = [t.dtype for t in v]
return types, v
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:7,代码来源:execute.py
示例5: args_to_mixed_eager_tensors
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def args_to_mixed_eager_tensors(lists, ctx):
"""Converts a list of same-length lists of values to eager tensors."""
assert len(lists) > 1
# Generate an error if len(lists[i]) is not the same for all i.
lists_ret = []
for l in lists[1:]:
if len(l) != len(lists[0]):
raise ValueError(
"Expected list arguments to be the same length: %d != %d (%r vs. %r)."
% (len(lists[0]), len(l), lists[0], l))
lists_ret.append([])
# Convert the first element of each list first, then the second element, etc.
types = []
for i in range(len(lists[0])):
dtype = None
# If any list has a Tensor, use that dtype
for l in lists:
if isinstance(l[i], ops.EagerTensor):
dtype = l[i].dtype
break
if dtype is None:
# Convert the first one and use its dtype.
lists_ret[0].append(ops.internal_convert_to_tensor(lists[0][i], ctx=ctx))
dtype = lists_ret[0][i].dtype
for j in range(1, len(lists)):
lists_ret[j].append(
ops.internal_convert_to_tensor(lists[j][i], dtype=dtype, ctx=ctx))
else:
# Convert everything to the found dtype.
for j in range(len(lists)):
lists_ret[j].append(
ops.internal_convert_to_tensor(lists[j][i], dtype=dtype, ctx=ctx))
types.append(dtype)
return types, lists_ret
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:38,代码来源:execute.py
示例6: named_defun
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def named_defun(func, name):
"""Defines a function with a given name.
See the documentation for `defun` for more information on the semantics of the
function.
Args:
func: the function to be wrapped.
name: the name given to it.
Returns:
the wrapped function.
"""
arguments_to_functions = {}
def decorated(*args, **kwds):
"""Decorated version of func."""
# Macroexpand on non-Tensor arguments
cache_key = tuple(_cache_key(x) for x in args)
assert all(not isinstance(x, ops.EagerTensor) for x in kwds.values())
cache_key = (cache_key, tuple(kwds.items()))
if cache_key not in arguments_to_functions:
arguments_to_functions[cache_key] = _defun_internal(
name, func, args, kwds)
return arguments_to_functions[cache_key](*args)
return decorated
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:30,代码来源:function.py
示例7: _eval_helper
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def _eval_helper(self, tensors):
if isinstance(tensors, ops.EagerTensor):
return tensors.numpy()
if isinstance(tensors, resource_variable_ops.ResourceVariable):
return tensors.read_value().numpy()
if isinstance(tensors, tuple):
return tuple([self._eval_helper(t) for t in tensors])
elif isinstance(tensors, list):
return [self._eval_helper(t) for t in tensors]
elif isinstance(tensors, dict):
assert not tensors, "Only support empty dict now."
return dict()
else:
raise ValueError("Unsupported type.")
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:17,代码来源:test_util.py
示例8: constant_value
# 需要导入模块: from tensorflow.python.framework import ops [as 别名]
# 或者: from tensorflow.python.framework.ops import EagerTensor [as 别名]
def constant_value(tensor, partial=False): # pylint: disable=invalid-name
"""Returns the constant value of the given tensor, if efficiently calculable.
This function attempts to partially evaluate the given tensor, and
returns its value as a numpy ndarray if this succeeds.
TODO(mrry): Consider whether this function should use a registration
mechanism like gradients and ShapeFunctions, so that it is easily
extensible.
NOTE: If `constant_value(tensor)` returns a non-`None` result, it will no
longer be possible to feed a different value for `tensor`. This allows the
result of this function to influence the graph that is constructed, and
permits static shape optimizations.
Args:
tensor: The Tensor to be evaluated.
partial: If True, the returned numpy array is allowed to have partially
evaluated values. Values that can't be evaluated will be None.
Returns:
A numpy ndarray containing the constant value of the given `tensor`,
or None if it cannot be calculated.
Raises:
TypeError: if tensor is not an ops.Tensor.
"""
if isinstance(tensor, ops.EagerTensor):
return tensor.numpy()
ret = _ConstantValue(tensor, partial)
if ret is not None:
# The caller may now depend on the constant value of `tensor`, so we
# conservatively prevent it from being fed.
tensor.graph.prevent_feeding(tensor)
return ret
开发者ID:PacktPublishing,项目名称:Serverless-Deep-Learning-with-TensorFlow-and-AWS-Lambda,代码行数:37,代码来源:tensor_util.py