本文整理汇总了Python中tensorflow.dtype方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.dtype方法的具体用法?Python tensorflow.dtype怎么用?Python tensorflow.dtype使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.dtype方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _bbox_to_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _bbox_to_mask(yy, region_size, dtype):
# trim bounding box exeeding region_size on top and left
neg_part = tf.nn.relu(-yy[:2])
core = tf.ones(tf.to_int32(tf.round(yy[2:] - neg_part)), dtype=dtype)
y1 = tf.maximum(yy[0], 0.)
x1 = tf.maximum(yy[1], 0.)
y2 = tf.minimum(region_size[0], yy[0] + yy[2])
x2 = tf.minimum(region_size[1], yy[1] + yy[3])
padding = (y1, region_size[0] - y2, x1, region_size[1] - x2)
padding = tf.reshape(tf.stack(padding), (-1, 2))
padding = tf.to_int32(tf.round(padding))
mask = tf.pad(core, padding)
# trim bounding box exeeding region_size on bottom and right
rs = tf.to_int32(tf.round(region_size))
mask = mask[:rs[0], :rs[1]]
mask.set_shape((None, None))
return mask
示例2: bbox_to_mask
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def bbox_to_mask(bbox, region_size, output_size, dtype=tf.float32):
"""Creates a binary mask of size `region_size` where rectangle given by
`bbox` is filled with ones and the rest is zeros. Finally, the binary mask
is resized to `output_size` with bilinear interpolation.
:param bbox: tensor of shape (..., 4)
:param region_size: tensor of shape (..., 2)
:param output_size: 2-tuple of ints
:param dtype: tf.dtype
:return: a tensor of shape = (..., output_size)
"""
shape = tf.concat(axis=0, values=(tf.shape(bbox)[:-1], output_size))
bbox = tf.reshape(bbox, (-1, 4))
region_size = tf.reshape(region_size, (-1, 2))
def create_mask(args):
yy, region_size = args
return _bbox_to_mask_fixed_size(yy, region_size, output_size, dtype)
mask = tf.map_fn(create_mask, (bbox, region_size), dtype=dtype)
return tf.reshape(mask, shape)
示例3: loss_function_crossentropy_1D
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def loss_function_crossentropy_1D( y_pred, y_target, class_weights=None, num_classes=None):
""" Cross entropy loss function op, comparing 1D tensors for network prediction and target. Weights the classes \
when calculating the loss to balance un-even training batches. If class weights are not provided, then no \
weighting is done (weight of 1 assigned to each class).
Args:
y_pred (tensor): Output of network (1D vector of class scores). Shape [numSamples x numClasses].
y_target (tensor): One-hot classification labels (1D vector). Shape [numSamples x numClasses].
class_weights (tensor): Weight for each class. Shape [numClasses].
num_classes (int):
Returns:
(tensor): Cross-entropy loss.
"""
if class_weights==None:
class_weights = tf.constant(1,shape=[num_classes],dtype=tf.dtypes.float32)
sample_weights = tf.reduce_sum( tf.multiply(y_target, class_weights ), axis=1) # weight of each sample
loss = tf.reduce_mean( tf.losses.softmax_cross_entropy(
onehot_labels=y_target,logits=y_pred,weights=sample_weights ) )
return loss
示例4: balance_classes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def balance_classes(y_target,num_classes):
""" Calculates the class weights needed to balance the classes, based on the number of samples of each class in the \
batch of data.
Args:
y_target (tensor): One-hot classification labels (1D vector). Shape [numSamples x numClasses]
num_classes (int):
Returns:
(tensor): A weighting for each class that balances their contribution to the loss. Shape [numClasses].
"""
y_target = tf.reshape( y_target, [-1, num_classes] )
class_count = tf.add( tf.reduce_sum( y_target, axis=0 ), tf.constant( [1]*num_classes, dtype=tf.float32 ) )
class_weights = tf.multiply( tf.divide( tf.ones( ( 1, num_classes) ), class_count ), tf.reduce_max( class_count ) )
return class_weights
示例5: column_to_dtype
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def column_to_dtype(feature, feature_conf):
"""Parse columns to tf.dtype
Return:
similar to _csv_column_defaults()
"""
_column_dtype_dic = OrderedDict()
_column_dtype_dic['label'] = tf.int32
for f in feature:
if f in feature_conf:
conf = feature_conf[f]
if conf['type'] == 'category':
if conf['transform'] == 'identity': # identity category column need int type
_column_dtype_dic[f] = tf.int32
else:
_column_dtype_dic[f] = tf.string
else:
_column_dtype_dic[f] = tf.float32 # 0.0 for float32
else:
_column_dtype_dic[f] = tf.string
return _column_dtype_dic
示例6: get_datatype
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def get_datatype():
"""Get the default datatype used for Tensors
Returns
-------
dtype : tf.dtype or torch.dtype
The current default datatype
"""
if __SETTINGS__._DATATYPE is None:
if get_backend() == 'pytorch':
import torch
return torch.float32
else:
import tensorflow as tf
return tf.dtypes.float32
else:
return __SETTINGS__._DATATYPE
示例7: set_datatype
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def set_datatype(datatype):
"""Set the datatype to use for Tensors
Parameters
----------
datatype : tf.dtype or torch.dtype
The default datatype to use
"""
if get_backend() == 'pytorch':
import torch
if datatype is None or isinstance(datatype, torch.dtype):
__SETTINGS__._DATATYPE = datatype
else:
raise TypeError('datatype must be a torch.dtype')
else:
import tensorflow as tf
if datatype is None or isinstance(datatype, tf.dtypes.DType):
__SETTINGS__._DATATYPE = datatype
else:
raise TypeError('datatype must be a tf.dtypes.DType')
示例8: _placeholders_from_graphs_tuple
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _placeholders_from_graphs_tuple(graph, force_dynamic_num_graphs=True):
"""Creates a `graphs.GraphsTuple` of placeholders that matches a numpy graph.
Args:
graph: A `graphs.GraphsTuple` that contains numpy data.
force_dynamic_num_graphs: A `bool` that forces the batch dimension to be
dynamic. Defaults to `True`.
Returns:
A `graphs.GraphsTuple` containing placeholders.
"""
graph_dtypes = graph.map(
lambda v: tf.as_dtype(v.dtype) if v is not None else None, ALL_FIELDS)
graph_shapes = graph.map(lambda v: list(v.shape) if v is not None else None,
ALL_FIELDS)
return _build_placeholders_from_specs(
graph_dtypes,
graph_shapes,
force_dynamic_num_graphs=force_dynamic_num_graphs)
示例9: _populate_number_fields
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _populate_number_fields(data_dict):
"""Returns a dict with the number fields N_NODE, N_EDGE filled in.
The N_NODE field is filled if the graph contains a non-`None` NODES field;
otherwise, it is set to 0.
The N_EDGE field is filled if the graph contains a non-`None` RECEIVERS field;
otherwise, it is set to 0.
Args:
data_dict: An input `dict`.
Returns:
The data `dict` with number fields.
"""
dct = data_dict.copy()
for number_field, data_field in [[N_NODE, NODES], [N_EDGE, RECEIVERS]]:
if dct.get(number_field) is None:
if dct[data_field] is not None:
dct[number_field] = tf.shape(dct[data_field])[0]
else:
dct[number_field] = tf.constant(0, dtype=tf.int32)
return dct
示例10: _to_compatible_data_dicts
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _to_compatible_data_dicts(data_dicts):
"""Convert the content of `data_dicts` to tensors of the right type.
All fields are converted to `Tensor`s. The index fields (`SENDERS` and
`RECEIVERS`) and number fields (`N_NODE`, `N_EDGE`) are cast to `tf.int32`.
Args:
data_dicts: An iterable of dictionaries with keys `ALL_KEYS` and
values either `None`s, or quantities that can be converted to `Tensor`s.
Returns:
A list of dictionaries containing `Tensor`s or `None`s.
"""
results = []
for data_dict in data_dicts:
result = {}
for k, v in data_dict.items():
if v is None:
result[k] = None
else:
dtype = tf.int32 if k in [SENDERS, RECEIVERS, N_NODE, N_EDGE] else None
result[k] = tf.convert_to_tensor(v, dtype)
results.append(result)
return results
示例11: _check_valid_index
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _check_valid_index(index, element_name):
"""Verifies if a value with `element_name` is a valid index."""
if isinstance(index, int):
return True
elif isinstance(index, tf.Tensor):
if index.dtype != tf.int32 and index.dtype != tf.int64:
raise TypeError(
"Invalid tensor `{}` parameter. Valid tensor indices must have "
"types tf.int32 or tf.int64, got {}."
.format(element_name, index.dtype))
if index.shape.as_list():
raise TypeError(
"Invalid tensor `{}` parameter. Valid tensor indices must be scalars "
"with shape [], got{}"
.format(element_name, index.shape.as_list()))
return True
else:
raise TypeError(
"Invalid `{}` parameter. Valid tensor indices must be integers "
"or tensors, got {}."
.format(element_name, type(index)))
示例12: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def __init__(self, shape, dtype=tf.float32, name=None):
"""Creates a placeholder for a batch of tensors of a given shape and dtype
Parameters
----------
shape: [int]
shape of a single elemenet of the batch
dtype: tf.dtype
number representation used for tensor contents
name: str
name of the underlying placeholder
"""
super().__init__(tf.placeholder(dtype, [None] + list(shape), name=name))
示例13: _bbox_to_mask_fixed_size
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def _bbox_to_mask_fixed_size(yy, region_size, output_size, dtype):
mask = _bbox_to_mask(yy, region_size, dtype)
nonzero_region = tf.greater(tf.reduce_prod(tf.shape(mask)), 0)
mask = tf.cond(nonzero_region, lambda: mask, lambda: tf.zeros(output_size, dtype))
mask = tf.image.resize_images(mask[..., tf.newaxis], output_size)[..., 0]
return mask
示例14: is_object_array
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def is_object_array(p):
"""Returns True iff p is an object array.
Args:
p (Any): object to be checked
Returns:
bool: True iff p is a NumPy object array
"""
return isinstance(p, np.ndarray) and p.dtype == object
示例15: gather_indexes
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import dtype [as 别名]
def gather_indexes(sequence_tensor, positions):
"""Gathers the vectors at the specific positions.
Args:
sequence_tensor: Sequence output of `BertModel` layer of shape
(`batch_size`, `seq_length`, num_hidden) where num_hidden is number of
hidden units of `BertModel` layer.
positions: Positions ids of tokens in sequence to mask for pretraining of
with dimension (batch_size, max_predictions_per_seq) where
`max_predictions_per_seq` is maximum number of tokens to mask out and
predict per each sequence.
Returns:
Masked out sequence tensor of shape (batch_size * max_predictions_per_seq,
num_hidden).
"""
sequence_shape = modeling.get_shape_list(
sequence_tensor, name='sequence_output_tensor')
batch_size = sequence_shape[0]
seq_length = sequence_shape[1]
width = sequence_shape[2]
flat_offsets = tf.keras.backend.reshape(
tf.range(0, batch_size, dtype=tf.int32) * seq_length, [-1, 1])
flat_positions = tf.keras.backend.reshape(positions + flat_offsets, [-1])
flat_sequence_tensor = tf.keras.backend.reshape(
sequence_tensor, [batch_size * seq_length, width])
output_tensor = tf.gather(flat_sequence_tensor, flat_positions)
return output_tensor
开发者ID:ShivangShekhar,项目名称:Live-feed-object-device-identification-using-Tensorflow-and-OpenCV,代码行数:32,代码来源:bert_models.py