本文整理汇总了Python中tensorflow.python.keras.engine.training_utils.cast_if_floating_dtype函数的典型用法代码示例。如果您正苦于以下问题:Python cast_if_floating_dtype函数的具体用法?Python cast_if_floating_dtype怎么用?Python cast_if_floating_dtype使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了cast_if_floating_dtype函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: test_on_batch
def test_on_batch(model, inputs, targets, sample_weights=None):
"""Calculates the loss for one input batch.
Arguments:
model: Model whose loss has to be calculated.
inputs: Input batch data.
targets: Target batch data.
sample_weights: Sample weight batch data.
Returns:
total loss, loss and metrics associated with each output.
"""
if len(inputs) and tensor_util.is_tensor(inputs[0]):
inputs = training_utils.cast_if_floating_dtype(inputs)
targets = training_utils.cast_if_floating_dtype(targets)
else:
inputs = [
ops.convert_to_tensor(val, dtype=backend.floatx()) for val in inputs
]
targets = [
ops.convert_to_tensor(val, dtype=backend.floatx()) for val in targets
]
if sample_weights:
sample_weights = [
ops.convert_to_tensor(val, dtype=backend.floatx())
if val is not None else None for val in sample_weights
]
outs, loss, loss_metrics = _model_loss(
model, inputs, targets, sample_weights=sample_weights, training=False)
if not isinstance(outs, list):
outs = [outs]
metrics_results = _eager_metrics_fn(model, outs, targets)
if not isinstance(loss, list):
loss = [loss]
return loss + loss_metrics + metrics_results
开发者ID:Huoxubeiyin,项目名称:tensorflow,代码行数:35,代码来源:training_eager.py
示例2: train_on_batch
def train_on_batch(model,
inputs,
targets,
sample_weights=None,
output_loss_metrics=None):
"""Calculates the loss and gradient updates for one input batch.
Arguments:
model: Model whose loss has to be calculated.
inputs: Input batch data.
targets: Target batch data.
sample_weights: Sample weight batch data.
output_loss_metrics: List of metrics that are used to aggregated output
loss values.
Returns:
total loss and the loss associated with each output.
"""
if isinstance(inputs, collections.Sequence):
if len(inputs) and tensor_util.is_tensor(inputs[0]):
inputs = training_utils.cast_if_floating_to_model_input_dtypes(inputs,
model)
if targets:
targets = training_utils.cast_if_floating_dtype(targets)
else:
inputs = training_utils.cast_if_floating_to_model_input_dtypes(
[ops.convert_to_tensor(val) for val in inputs], model)
if targets:
targets = training_utils.cast_if_floating_dtype(
[ops.convert_to_tensor(val) for val in targets])
if sample_weights:
sample_weights = [
training_utils.cast_if_floating_dtype(ops.convert_to_tensor(val))
if val is not None else None for val in sample_weights
]
outs, total_loss, output_losses, masks = (
_process_single_batch(
model,
inputs,
targets,
sample_weights=sample_weights,
training=True,
output_loss_metrics=output_loss_metrics))
if not isinstance(outs, list):
outs = [outs]
metrics_results = _eager_metrics_fn(
model, outs, targets, sample_weights=sample_weights, masks=masks)
total_loss = nest.flatten(total_loss)
results = total_loss + output_losses + metrics_results
return [_non_none_constant_value(v) for v in results]
开发者ID:aritratony,项目名称:tensorflow,代码行数:52,代码来源:training_eager.py
示例3: train_on_batch
def train_on_batch(model, inputs, targets, sample_weights=None):
"""Calculates the loss and gradient updates for one input batch.
Arguments:
model: Model whose loss has to be calculated.
inputs: Input batch data.
targets: Target batch data.
sample_weights: Sample weight batch data.
Returns:
total loss and the loss associated with each output.
"""
if isinstance(inputs, collections.Sequence):
if len(inputs) and tensor_util.is_tensor(inputs[0]):
inputs = training_utils.cast_if_floating_dtype(inputs)
targets = training_utils.cast_if_floating_dtype(targets)
else:
inputs = training_utils.cast_if_floating_dtype([
ops.convert_to_tensor(val) for val in inputs
])
targets = training_utils.cast_if_floating_dtype([
ops.convert_to_tensor(val) for val in targets
])
if sample_weights:
sample_weights = [
training_utils.cast_if_floating_dtype(ops.convert_to_tensor(val))
if val is not None else None for val in sample_weights
]
outs, loss, loss_metrics, _, masks = _process_single_batch(
model, inputs, targets, sample_weights=sample_weights, training=True)
if not isinstance(outs, list):
outs = [outs]
metrics_results = _eager_metrics_fn(
model,
outs,
targets,
sample_weights=sample_weights,
masks=masks,
return_stateful_result=True)
loss = nest.flatten(loss)
return [
tensor_util.constant_value(v)
for v in loss + loss_metrics + metrics_results
]
开发者ID:Wajih-O,项目名称:tensorflow,代码行数:46,代码来源:training_eager.py
示例4: iterator_predict_loop
def iterator_predict_loop(model, inputs, steps, verbose=0):
"""Predict function for eager execution when input is dataset iterator.
Arguments:
model: Instance of `Model`.
inputs: Input dataset iterator.
steps: Total number of steps (batches of samples) before declaring
`_predict_loop` finished.
verbose: Verbosity mode.
Returns:
Array of predictions (if the model has a single output)
or list of arrays of predictions (if the model has multiple outputs).
Raises:
ValueError: In case of mismatch between given number of inputs and
expectations of the model.
"""
assert isinstance(inputs, iterator_ops.EagerIterator)
if not isinstance(inputs.output_shapes,
(list, tuple)) or len(inputs.output_shapes) > 3:
raise ValueError(
'Please provide data as a list or tuple of 1, 2, or 3 elements '
' - `(input)`, or `(input, target)`, or `(input, target,'
'sample_weights)`. Received %s. We do not use the `target` or'
'`sample_weights` value here.' % inputs.output_shapes)
outs = []
if verbose == 1:
progbar = generic_utils.Progbar(target=steps)
for step_index in range(steps):
# Get data from the iterator.
try:
next_element = inputs.get_next()
except errors.OutOfRangeError:
logging.warning(
'Your dataset iterator ran out of data; interrupting prediction. '
'Make sure that your dataset can generate at least `steps` batches '
'(in this case, %d batches). You may need to use the repeat() '
'function when building your dataset.', steps)
break
# expects a tuple, where first element of tuple represents inputs
x = next_element[0]
# Validate and standardize data.
x, _, _ = model._standardize_user_data(x)
x = training_utils.cast_if_floating_dtype(x)
if isinstance(x, list) and len(x) == 1:
x = x[0]
if model._expects_training_arg:
batch_outs = model.call(x, training=False)
else:
batch_outs = model.call(x)
if not isinstance(batch_outs, list):
batch_outs = [batch_outs]
# We collect the results from every step and then concatenate them once
# in the end. This is an expensive process. We are doing this because we
# do not know the number of samples beforehand.
if step_index == 0:
for _ in batch_outs:
outs.append([])
for i, batch_out in enumerate(batch_outs):
outs[i].append(backend.get_value(batch_out))
if verbose == 1:
progbar.update(step_index + 1)
for i, out in enumerate(outs):
outs[i] = np.concatenate(tuple(out), axis=0)
if len(outs) == 1:
return outs[0]
return outs
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:74,代码来源:training_eager.py
示例5: iterator_test_loop
def iterator_test_loop(model, inputs, steps, verbose=0):
"""Test function for eager execution when input is given as dataset iterator.
Arguments:
model: Model instance that is being evaluated in Eager mode.
inputs: Input dataset iterator.
steps: Total number of steps (batches of samples) before declaring
predictions finished.
verbose: Verbosity mode.
Returns:
Scalar loss (if the model has a single output and no metrics)
or list of scalars (if the model has multiple outputs
and/or metrics). The attribute `model.metrics_names` will give you
the display labels for the scalar outputs.
Raises:
ValueError: In case of mismatch between given number of inputs and
expectations of the model.
"""
assert isinstance(inputs, iterator_ops.EagerIterator)
# make sure either x,y or x,y,sample_weights is provided
if (not isinstance(inputs.output_shapes, (list, tuple)) or
len(inputs.output_shapes) < 2 or len(inputs.output_shapes) > 3):
raise ValueError('Please provide either inputs and targets'
'or inputs, targets, and sample_weights')
outs = []
num_samples = 0
if verbose == 1:
progbar = generic_utils.Progbar(target=steps)
for step_index in range(steps):
# Get data from the iterator.
try:
next_element = inputs.get_next()
except errors.OutOfRangeError:
logging.warning(
'Your dataset iterator ran out of data interrupting testing. '
'Make sure that your dataset can generate at least `steps` batches '
'(in this case, %d batches). You may need to use the repeat() '
'function when building your dataset.', steps)
break
if len(inputs.output_shapes) == 2:
x, y = next_element
sample_weights = None
else:
x, y, sample_weights = next_element
# Validate and standardize data.
x, y, sample_weights = model._standardize_user_data(
x, y, sample_weight=sample_weights)
x = training_utils.cast_if_floating_dtype(x)
y = training_utils.cast_if_floating_dtype(y)
if sample_weights:
sample_weights = [
training_utils.cast_if_floating_dtype(
ops.convert_to_tensor(val, dtype=backend.floatx()))
if val is not None else None for val in sample_weights
]
if step_index == 0:
# Get stateful metrics indices. We do not do this before the `steps` loop
# because model will be compiled only in the first iteration of this loop
# in the deferred build scenario.
if hasattr(model, 'metrics'):
for m in model.stateful_metric_functions:
m.reset_states()
stateful_metric_indices = [
i for i, name in enumerate(model.metrics_names)
if str(name) in model.stateful_metric_names
]
else:
stateful_metric_indices = []
# Calculate model output, loss values.
loss_outs, loss, loss_metrics, masks = _model_loss(
model, x, y, sample_weights=sample_weights, training=False)
metrics_results = _eager_metrics_fn(
model, loss_outs, y, sample_weights=sample_weights, masks=masks)
batch_outs = []
for _, v in zip(model.metrics_names,
[backend.mean(loss)] + loss_metrics + metrics_results):
batch_outs.append(tensor_util.constant_value(v))
# Get current step size.
if isinstance(x, list):
step_size = x[0].get_shape().as_list()[0]
elif isinstance(x, dict):
step_size = list(x.values())[0].get_shape().as_list()[0]
else:
step_size = x.get_shape().as_list()[0]
# Accumulate results in output array.
if not isinstance(batch_outs, list):
batch_outs = [batch_outs]
if step_index == 0:
for _ in enumerate(batch_outs):
outs.append(0.)
for i, batch_out in enumerate(batch_outs):
if i in stateful_metric_indices:
#.........这里部分代码省略.........
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:101,代码来源:training_eager.py
示例6: iterator_fit_loop
def iterator_fit_loop(model,
inputs,
class_weight,
steps_per_epoch,
epoch_logs,
val_inputs=None,
val_targets=None,
val_sample_weights=None,
epochs=1,
verbose=1,
callbacks=None,
validation_steps=None,
do_validation=False,
batch_size=None):
"""Fit function for eager execution when input is given as dataset iterator.
Updates the given epoch logs.
Arguments:
model: Instance of the `Model`.
inputs: Input dataset iterator.
class_weight: Optional class-weight array to weight the importance of
samples in `inputs` based on the class they belong to, as conveyed by
the targets from the `inputs` iterator.
steps_per_epoch: Total number of steps (batches of samples)
before declaring one epoch finished and starting the
next epoch.
epoch_logs: Dictionary of logs from every epoch.
val_inputs: Input data for validation.
val_targets: Target data for validation.
val_sample_weights: Sample weight data for validation.
epochs: Number of times to iterate over the data
verbose: Verbosity mode, 0, 1 or 2
callbacks: CallbackList instance. Controls callbacks during training.
validation_steps: Number of steps to run validation for (only if doing
validation from data tensors). Ignored with default value of `None`.
do_validation: Boolean value indicating whether we should do validation.
batch_size: int, val_inputs and val_targets will be evaled batch by
batch with size batch_size if they are array.
Raises:
ValueError: In case of mismatch between given number of inputs and
expectations of the model.
"""
assert isinstance(inputs, iterator_ops.EagerIterator)
# make sure either x,y or x,y,sample_weights is provided
if (not isinstance(inputs.output_shapes, (list, tuple)) or
len(inputs.output_shapes) not in (2, 3)):
raise ValueError('Please provide either inputs and targets '
'or inputs, targets, and sample_weights')
for step_index in range(steps_per_epoch):
batch_logs = {'batch': step_index, 'size': 1}
callbacks.on_batch_begin(step_index, batch_logs)
# Get data from the iterator.
try:
next_element = inputs.get_next()
except errors.OutOfRangeError:
logging.warning(
'Your dataset iterator ran out of data; interrupting training. Make '
'sure that your dataset can generate at least '
'`steps_per_epoch * epochs` batches (in this case, %d batches). You '
'may need to use the repeat() function when building your '
'dataset.' % steps_per_epoch * epochs)
break
if len(inputs.output_shapes) == 2:
x, y = next_element
sample_weights = None
else:
x, y, sample_weights = next_element
# Validate and standardize data.
x, y, sample_weights = model._standardize_user_data(
x, y, sample_weight=sample_weights, class_weight=class_weight)
x = training_utils.cast_if_floating_dtype(x)
y = training_utils.cast_if_floating_dtype(y)
if sample_weights:
sample_weights = [
training_utils.cast_if_floating_dtype(
ops.convert_to_tensor(val, dtype=backend.floatx()))
if val is not None else None for val in sample_weights
]
# Set stateful_metrics in callbacks. We do not do this before the
# `steps_per_epoch` loop because model will be compiled only in the first
# iteration of this loop in the deferred build scenario.
if step_index == 0:
for cbk in callbacks:
if (isinstance(cbk, cbks.BaseLogger) or
isinstance(cbk, cbks.ProgbarLogger)):
cbk.stateful_metrics = model.stateful_metric_names
if step_index == 0 and not callbacks.params['metrics']:
callback_metrics = copy.copy(model.metrics_names)
if do_validation:
callback_metrics += ['val_' + n for n in model.metrics_names]
callbacks.set_params({
#.........这里部分代码省略.........
开发者ID:ThunderQi,项目名称:tensorflow,代码行数:101,代码来源:training_eager.py
示例7: iterator_test_loop
def iterator_test_loop(model, inputs, steps, verbose=0):
"""Test function for eager execution when input is given as dataset iterator.
Arguments:
model: Model instance that is being evaluated in Eager mode.
inputs: Input dataset iterator.
steps: Total number of steps (batches of samples) before declaring
predictions finished.
verbose: Verbosity mode.
Returns:
Scalar loss (if the model has a single output and no metrics)
or list of scalars (if the model has multiple outputs
and/or metrics). The attribute `model.metrics_names` will give you
the display labels for the scalar outputs.
Raises:
ValueError: In case of mismatch between given number of inputs and
expectations of the model.
"""
assert isinstance(inputs, iterator_ops.EagerIterator)
outs = []
num_samples = 0
if verbose == 1:
progbar = generic_utils.Progbar(target=steps)
for step_index in range(steps):
# Get data from the iterator.
try:
next_element = inputs.get_next()
except errors.OutOfRangeError:
logging.warning(
'Your dataset iterator ran out of data interrupting testing. '
'Make sure that your dataset can generate at least `steps` batches '
'(in this case, %d batches).', steps)
break
if not isinstance(next_element, (list, tuple)) or len(next_element) != 2:
raise ValueError('Please provide data as a list or tuple of 2 elements '
' - input and target pair. Received %s' % next_element)
x, y = next_element
# Validate and standardize data.
x, y, sample_weights = model._standardize_user_data(x, y)
x = training_utils.cast_if_floating_dtype(x)
y = training_utils.cast_if_floating_dtype(y)
# Calculate model output, loss values.
loss_outs, loss, loss_metrics = _model_loss(
model, x, y, sample_weights=sample_weights, training=False)
metrics_results = _eager_metrics_fn(model, loss_outs, y)
batch_outs = []
for _, v in zip(model.metrics_names,
[backend.mean(loss)] + loss_metrics + metrics_results):
batch_outs.append(tensor_util.constant_value(v))
# Get current step size.
if isinstance(x, list):
step_size = x[0].get_shape().as_list()[0]
else:
step_size = x.get_shape().as_list()[0]
# Accumulate results in output array.
if not isinstance(batch_outs, list):
batch_outs = [batch_outs]
if step_index == 0:
for _ in enumerate(batch_outs):
outs.append(0.)
for i, batch_out in enumerate(batch_outs):
outs[i] += batch_out * step_size
# Calculate sample size.
num_samples += step_size
if verbose == 1:
progbar.update(step_index + 1)
for i in range(len(outs)):
outs[i] /= num_samples
if len(outs) == 1:
return outs[0]
return outs
开发者ID:Huoxubeiyin,项目名称:tensorflow,代码行数:80,代码来源:training_eager.py
示例8: iterator_fit_loop
def iterator_fit_loop(model,
inputs,
class_weight,
steps_per_epoch,
callback_model,
out_labels,
epoch_logs,
val_inputs=None,
val_targets=None,
val_sample_weights=None,
epochs=1,
verbose=1,
callbacks=None,
callback_metrics=None,
validation_steps=None,
do_validation=False):
"""Fit function for eager execution when input is given as dataset iterator.
Updates the given epoch logs.
Arguments:
model: Instance of the `Model`.
inputs: Input dataset iterator.
class_weight: Optional class-weight array to weight the importance of
samples in `inputs` based on the class they belong to, as conveyed by
the targets from the `inputs` iterator.
steps_per_epoch: Total number of steps (batches of samples)
before declaring one epoch finished and starting the
next epoch.
callback_model: Instance of `Model` to callback.
out_labels: Output labels generated from model metric names.
epoch_logs: Dictionary of logs from every epoch.
val_inputs: Input data for validation.
val_targets: Target data for validation.
val_sample_weights: Sample weight data for validation.
epochs: Number of times to iterate over the data
verbose: Verbosity mode, 0, 1 or 2
callbacks: List of callbacks to be called during training
callback_metrics: List of strings, the display names of the metrics
passed to the callbacks. They should be the
concatenation of list the display names of the outputs of
`f` and the list of display names of the outputs of `f_val`.
validation_steps: Number of steps to run validation for (only if doing
validation from data tensors). Ignored with default value of `None`.
do_validation: Boolean value indicating whether we should do validation.
Raises:
ValueError: In case of mismatch between given number of inputs and
expectations of the model.
"""
assert isinstance(inputs, iterator_ops.EagerIterator)
for step_index in range(steps_per_epoch):
batch_logs = {}
batch_logs['batch'] = step_index
batch_logs['size'] = 1
callbacks.on_batch_begin(step_index, batch_logs)
# Get data from the iterator.
try:
next_element = inputs.get_next()
except errors.OutOfRangeError:
logging.warning(
'Your dataset iterator ran out of data; '
'interrupting training. Make sure that your dataset'
' can generate at least `steps_per_epoch * epochs` '
'batches (in this case, %d batches).' % steps_per_epoch * epochs)
break
if not isinstance(next_element, (list, tuple)) or len(next_element) != 2:
raise ValueError('Please provide data as a list or tuple of 2 elements '
' - input and target pair. Received %s' % next_element)
x, y = next_element
# Validate and standardize data.
x, y, sample_weights = model._standardize_user_data(
x, y, class_weight=class_weight)
x = training_utils.cast_if_floating_dtype(x)
y = training_utils.cast_if_floating_dtype(y)
if sample_weights:
sample_weights = [
ops.convert_to_tensor(val, dtype=backend.floatx())
if val is not None else None for val in sample_weights
]
if step_index == 0 and not callback_metrics:
out_labels = model.metrics_names
if do_validation:
callback_metrics = copy.copy(out_labels) + [
'val_' + n for n in out_labels
]
else:
callback_metrics = copy.copy(out_labels)
callbacks.set_params({
'epochs': epochs,
'steps': steps_per_epoch,
'verbose': verbose,
'do_validation': do_validation,
'metrics': callback_metrics or [],
})
#.........这里部分代码省略.........
开发者ID:Huoxubeiyin,项目名称:tensorflow,代码行数:101,代码来源:training_eager.py
示例9: test_on_batch
def test_on_batch(model,
inputs,
targets,
sample_weights=None,
reset_metrics=True,
output_loss_metrics=None):
"""Calculates the loss for one input batch.
Arguments:
model: Model whose loss has to be calculated.
inputs: Input batch data.
targets: Target batch data.
sample_weights: Sample weight batch data.
reset_metrics: If `True`, the metrics returned will be only for this
batch. If `False`, the metrics will be statefully accumulated across
batches.
output_loss_metrics: List of metrics that are used to aggregated output
loss values.
Returns:
total loss, loss and metrics associated with each output.
"""
if isinstance(inputs, collections.Sequence):
if len(inputs) and tensor_util.is_tensor(inputs[0]):
inputs = training_utils.cast_if_floating_dtype(inputs)
targets = training_utils.cast_if_floating_dtype(targets)
else:
inputs = training_utils.cast_if_floating_dtype(
[ops.convert_to_tensor(val) for val in inputs])
targets = training_utils.cast_if_floating_dtype(
[ops.convert_to_tensor(val) for val in targets])
if sample_weights:
sample_weights = [
training_utils.cast_if_floating_dtype(ops.convert_to_tensor(val))
if val is not None else None for val in sample_weights
]
outs, total_loss, output_losses, aggregated_output_losses, masks = (
_model_loss(
model,
inputs,
targets,
sample_weights=sample_weights,
training=False,
output_loss_metrics=output_loss_metrics))
if not isinstance(outs, list):
outs = [outs]
metrics_results = _eager_metrics_fn(
model,
outs,
targets,
sample_weights=sample_weights,
masks=masks,
return_stateful_result=not reset_metrics)
total_loss = nest.flatten(total_loss)
if reset_metrics:
final_output_losses = output_losses
else:
final_output_losses = aggregated_output_losses
results = total_loss + final_output_losses + metrics_results
return [tensor_util.constant_value(v) for v in results]
开发者ID:kylin9872,项目名称:tensorflow,代码行数:61,代码来源:training_eager.py
注:本文中的tensorflow.python.keras.engine.training_utils.cast_if_floating_dtype函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论