本文整理汇总了Python中tensorflow.python.framework.types.as_dtype函数的典型用法代码示例。如果您正苦于以下问题:Python as_dtype函数的具体用法?Python as_dtype怎么用?Python as_dtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了as_dtype函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _DefaultGradYs
def _DefaultGradYs(grad_ys, ys, colocate_gradients_with_ops):
"""Fill in default values for grad_ys.
Args:
grad_ys: List of gradients, can contain None.
ys: List of tensors.
colocate_gradients_with_ops: If True, try colocating gradients with
the corresponding op.
Returns:
A list of gradients to use, without None.
Raises:
ValueError: If one of the grad_ys is invalid.
"""
if len(grad_ys) != len(ys):
raise ValueError("Passed %d grad_ys for %d ys" % (len(grad_ys), len(ys)))
grad_ys = ops.convert_n_to_tensor_or_indexed_slices(grad_ys, name="grad_y")
for i in xrange(len(grad_ys)):
grad_y = grad_ys[i]
y = ys[i]
if grad_y is None:
with ops.device(_GetGradsDevice(y.op, colocate_gradients_with_ops)):
grad_ys[i] = array_ops.fill(array_ops.shape(y),
constant_op.constant(1, dtype=y.dtype))
else:
if grad_y.dtype != y.dtype:
raise ValueError("Y and ys_grad must be of the same type, "
"not y: %s, ys_grad: %s " %
(types.as_dtype(y.dtype).name,
types.as_dtype(grad_y.dtype).name))
return grad_ys
示例2: _SatisfiesTypeConstraint
def _SatisfiesTypeConstraint(dtype, attr_def):
if attr_def.HasField("allowed_values"):
allowed_list = attr_def.allowed_values.list.type
if dtype not in allowed_list:
raise TypeError(
"DataType %s for attr '%s' not in list of allowed values: %s" %
(types_lib.as_dtype(dtype).name, attr_def.name,
", ".join(types_lib.as_dtype(x).name for x in allowed_list)))
示例3: testAllTypesConvertibleToNumpyDtype
def testAllTypesConvertibleToNumpyDtype(self):
for datatype_enum in types_pb2.DataType.values():
if datatype_enum == types_pb2.DT_INVALID:
continue
dtype = types.as_dtype(datatype_enum)
numpy_dtype = dtype.as_numpy_dtype
_ = np.empty((1, 1, 1, 1), dtype=numpy_dtype)
if dtype.base_dtype != types.bfloat16:
# NOTE(touts): Intentionally no way to feed a DT_BFLOAT16.
self.assertEqual(
types.as_dtype(datatype_enum).base_dtype, types.as_dtype(numpy_dtype))
示例4: testIsInteger
def testIsInteger(self):
self.assertEqual(types.as_dtype("int8").is_integer, True)
self.assertEqual(types.as_dtype("int16").is_integer, True)
self.assertEqual(types.as_dtype("int32").is_integer, True)
self.assertEqual(types.as_dtype("int64").is_integer, True)
self.assertEqual(types.as_dtype("uint8").is_integer, True)
self.assertEqual(types.as_dtype("complex64").is_integer, False)
self.assertEqual(types.as_dtype("float").is_integer, False)
self.assertEqual(types.as_dtype("double").is_integer, False)
self.assertEqual(types.as_dtype("string").is_integer, False)
self.assertEqual(types.as_dtype("bool").is_integer, False)
示例5: testIsFloating
def testIsFloating(self):
self.assertEqual(types.as_dtype("int8").is_floating, False)
self.assertEqual(types.as_dtype("int16").is_floating, False)
self.assertEqual(types.as_dtype("int32").is_floating, False)
self.assertEqual(types.as_dtype("int64").is_floating, False)
self.assertEqual(types.as_dtype("uint8").is_floating, False)
self.assertEqual(types.as_dtype("complex64").is_floating, False)
self.assertEqual(types.as_dtype("float32").is_floating, True)
self.assertEqual(types.as_dtype("float64").is_floating, True)
self.assertEqual(types.as_dtype("string").is_floating, False)
self.assertEqual(types.as_dtype("bool").is_floating, False)
示例6: ones
def ones(shape, dtype=types.float32, name=None):
"""Creates a tensor with all elements set to 1.
This operation returns a tensor of type `dtype` with shape `shape` and all
elements set to 1.
For example:
```python
tf.ones([2, 3], int32) ==> [[1, 1, 1], [1, 1, 1]]
```
Args:
shape: Either a list of integers, or a 1-D `Tensor` of type `int32`.
dtype: The type of an element in the resulting `Tensor`.
name: A name for the operation (optional).
Returns:
A `Tensor` with all elements set to 1.
"""
with ops.op_scope([shape], name, "ones") as name:
if isinstance(shape, list):
output = constant(1, shape=shape, dtype=dtype, name=name)
else:
shape = ops.convert_to_tensor(shape, name="shape")
output = fill(shape, constant(1, dtype=dtype), name=name)
assert output.dtype.base_dtype == types.as_dtype(dtype).base_dtype
return output
示例7: __init__
def __init__(self, key_dtype, value_dtype, default_value, table_ref):
"""Construct a table object from a table reference.
Args:
key_dtype: The table key type.
value_dtype: The table value type.
default_value: The value to use if a key is missing in the table.
table_ref: The table reference, i.e. the output of the lookup table ops.
"""
self._key_dtype = types.as_dtype(key_dtype)
self._value_dtype = types.as_dtype(value_dtype)
self._shapes = [tensor_shape.TensorShape([1])]
self._table_ref = table_ref
self._name = self._table_ref.op.name.split("/")[-1]
self._default_value = ops.convert_to_tensor(default_value, dtype=self._value_dtype)
self._default_value.get_shape().merge_with(tensor_shape.scalar())
示例8: _MakeType
def _MakeType(v, attr_def):
try:
v = types_lib.as_dtype(v)
except TypeError:
raise TypeError("Expected DataType for argument '%s' not %s." %
(attr_def.name, repr(v)))
i = v.as_datatype_enum
_SatisfiesTypeConstraint(i, attr_def)
return i
示例9: testDTypesHaveUniqueNames
def testDTypesHaveUniqueNames(self):
dtypes = []
names = set()
for datatype_enum in types_pb2.DataType.values():
if datatype_enum == types_pb2.DT_INVALID:
continue
dtype = types.as_dtype(datatype_enum)
dtypes.append(dtype)
names.add(dtype.name)
self.assertEqual(len(dtypes), len(names))
示例10: _VerifyGeneratedGradients
def _VerifyGeneratedGradients(grads, op):
"""Verify that gradients are valid in number and type.
Args:
grads: List of generated gradients.
op: Operation for which the gradients where generated.
Raises:
ValueError: if the gradients are invalid.
"""
if len(grads) != len(op.inputs):
raise ValueError("Num gradients %d generated for op %s do not match num "
"inputs %d" % (len(grads), op.node_def, len(op.inputs)))
for i in xrange(len(grads)):
grad = grads[i]
inp = op.inputs[i]
if grad is not None:
if not grad.dtype.is_compatible_with(inp.dtype):
raise ValueError(
"Gradient type %s generated for op %s does "
"not match input type %s" %
(types.as_dtype(grad.dtype).name, op.node_def,
types.as_dtype(inp.dtype).name))
示例11: _ComputeGradient
def _ComputeGradient(x, x_shape, dx, y, y_shape, dy,
x_init_value=None, delta=1e-3):
"""Computes the theoretical and numerical jacobian."""
t = types.as_dtype(x.dtype)
allowed_types = [types.float32, types.float64]
assert t.base_dtype in allowed_types, "Don't support type %s for x" % t.name
t2 = types.as_dtype(y.dtype)
assert t2.base_dtype in allowed_types, "Don't support type %s for y" % t2.name
if x_init_value is not None:
i_shape = list(x_init_value.shape)
assert(list(x_shape) == i_shape), "x_shape = %s, init_data shape = %s" % (
x_shape, i_shape)
x_data = x_init_value
else:
if t == types.float32:
dtype = np.float32
else:
dtype = np.float64
x_data = np.asfarray(np.random.random_sample(x_shape), dtype=dtype)
jacob_t = _ComputeTheoricalJacobian(x, x_shape, x_data, dy, y_shape, dx)
jacob_n = _ComputeNumericJacobian(x, x_shape, x_data, y, y_shape, delta)
return jacob_t, jacob_n
示例12: testMinMax
def testMinMax(self):
# make sure min/max evaluates for all data types that have min/max
for datatype_enum in types_pb2.DataType.values():
if datatype_enum == types_pb2.DT_INVALID:
continue
dtype = types.as_dtype(datatype_enum)
numpy_dtype = dtype.as_numpy_dtype
# ignore types for which there are no minimum/maximum (or we cannot
# compute it, such as for the q* types)
if (dtype.is_quantized or
dtype.base_dtype == types.bool or
dtype.base_dtype == types.string or
dtype.base_dtype == types.complex64):
continue
print("%s: %s - %s" % (dtype, dtype.min, dtype.max))
# check some values that are known
if numpy_dtype == np.bool_:
self.assertEquals(dtype.min, 0)
self.assertEquals(dtype.max, 1)
if numpy_dtype == np.int8:
self.assertEquals(dtype.min, -128)
self.assertEquals(dtype.max, 127)
if numpy_dtype == np.int16:
self.assertEquals(dtype.min, -32768)
self.assertEquals(dtype.max, 32767)
if numpy_dtype == np.int32:
self.assertEquals(dtype.min, -2147483648)
self.assertEquals(dtype.max, 2147483647)
if numpy_dtype == np.int64:
self.assertEquals(dtype.min, -9223372036854775808)
self.assertEquals(dtype.max, 9223372036854775807)
if numpy_dtype == np.uint8:
self.assertEquals(dtype.min, 0)
self.assertEquals(dtype.max, 255)
if numpy_dtype == np.uint16:
self.assertEquals(dtype.min, 0)
self.assertEquals(dtype.max, 4294967295)
if numpy_dtype == np.uint32:
self.assertEquals(dtype.min, 0)
self.assertEquals(dtype.max, 18446744073709551615)
if numpy_dtype in (np.float16, np.float32, np.float64):
self.assertEquals(dtype.min, np.finfo(numpy_dtype).min)
self.assertEquals(dtype.max, np.finfo(numpy_dtype).max)
示例13: __init__
def __init__(self, op, value_index, dtype):
"""Creates a new `Tensor`.
Args:
op: An `Operation`. `Operation` that computes this tensor.
value_index: An `int`. Index of the operation's endpoint that produces
this tensor.
dtype: A `types.DType`. Type of data stored in this tensor.
Raises:
TypeError: If the op is not an `Operation`.
"""
if not isinstance(op, Operation):
raise TypeError("op needs to be an Operation: %s" % op)
self._op = op
self._value_index = value_index
self._dtype = types.as_dtype(dtype)
self._shape = tensor_shape.unknown_shape()
# List of operations that use this Tensor as input. We maintain this list
# to easily navigate a computation graph.
self._consumers = []
示例14: _restore_slice
def _restore_slice(file_pattern, tensor_name, shape_and_slice, tensor_type,
name="restore_slice", preferred_shard=-1):
"""Restore a tensor slice from a set of files with a given pattern.
Example usage:
RestoreSlice("/foo/bar-?????-of-?????", "w", "10 10 0,2:-", DT_FLOAT)
Args:
file_pattern: the file pattern used to match a set of checkpoint files.
tensor_name: the name of the tensor to restore.
shape_and_slice: the shape-and-slice spec of the slice.
tensor_type: the type of the tensor to restore.
name: string. Optional name for the op.
preferred_shard: Int. Optional shard to open first in the checkpoint file.
Returns:
A tensor of type "tensor_type".
"""
base_type = types.as_dtype(tensor_type).base_dtype
return gen_io_ops._restore_slice(
file_pattern, tensor_name, shape_and_slice, base_type,
preferred_shard, name=name)
示例15: testStringConversion
def testStringConversion(self):
self.assertIs(types.float32, types.as_dtype("float32"))
self.assertIs(types.float64, types.as_dtype("float64"))
self.assertIs(types.int32, types.as_dtype("int32"))
self.assertIs(types.uint8, types.as_dtype("uint8"))
self.assertIs(types.int16, types.as_dtype("int16"))
self.assertIs(types.int8, types.as_dtype("int8"))
self.assertIs(types.string, types.as_dtype("string"))
self.assertIs(types.complex64, types.as_dtype("complex64"))
self.assertIs(types.int64, types.as_dtype("int64"))
self.assertIs(types.bool, types.as_dtype("bool"))
self.assertIs(types.qint8, types.as_dtype("qint8"))
self.assertIs(types.quint8, types.as_dtype("quint8"))
self.assertIs(types.qint32, types.as_dtype("qint32"))
self.assertIs(types.bfloat16, types.as_dtype("bfloat16"))
self.assertIs(types.float32_ref, types.as_dtype("float32_ref"))
self.assertIs(types.float64_ref, types.as_dtype("float64_ref"))
self.assertIs(types.int32_ref, types.as_dtype("int32_ref"))
self.assertIs(types.uint8_ref, types.as_dtype("uint8_ref"))
self.assertIs(types.int16_ref, types.as_dtype("int16_ref"))
self.assertIs(types.int8_ref, types.as_dtype("int8_ref"))
self.assertIs(types.string_ref, types.as_dtype("string_ref"))
self.assertIs(types.complex64_ref, types.as_dtype("complex64_ref"))
self.assertIs(types.int64_ref, types.as_dtype("int64_ref"))
self.assertIs(types.bool_ref, types.as_dtype("bool_ref"))
self.assertIs(types.qint8_ref, types.as_dtype("qint8_ref"))
self.assertIs(types.quint8_ref, types.as_dtype("quint8_ref"))
self.assertIs(types.qint32_ref, types.as_dtype("qint32_ref"))
self.assertIs(types.bfloat16_ref, types.as_dtype("bfloat16_ref"))
with self.assertRaises(TypeError):
types.as_dtype("not_a_type")