本文整理汇总了Python中tensorflow.python.training.training.get_global_step函数的典型用法代码示例。如果您正苦于以下问题:Python get_global_step函数的具体用法?Python get_global_step怎么用?Python get_global_step使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_global_step函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: model_fn
def model_fn(features, labels, mode):
_ = labels
step = training.get_global_step()
w = variable_scope.get_variable(
'w',
shape=[],
initializer=init_ops.zeros_initializer(),
dtype=dtypes.int64)
if estimator_lib.ModeKeys.TRAIN == mode:
# to consume features, we have control dependency
with ops.control_dependencies([features]):
step_inc = state_ops.assign_add(training.get_global_step(), 1)
with ops.control_dependencies([step_inc]):
assign_w_to_step_plus_2 = w.assign(step + 2)
return estimator_lib.EstimatorSpec(
mode,
loss=constant_op.constant(3.),
train_op=assign_w_to_step_plus_2)
if estimator_lib.ModeKeys.EVAL == mode:
# to consume features, we have control dependency
with ops.control_dependencies([features]):
loss = constant_op.constant(5.)
return estimator_lib.EstimatorSpec(
mode,
loss=loss,
# w is constant in each step, so the mean.
# w = 0 if step==0 else step+2
eval_metric_ops={'mean_of_const': metrics_lib.mean(w)})
示例2: _model_fn
def _model_fn(features, labels, mode):
predictions = layers.dense(
features['x'], 1, kernel_initializer=init_ops.zeros_initializer())
export_outputs = {
'predictions': export_output.RegressionOutput(predictions)
}
if mode == model_fn_lib.ModeKeys.PREDICT:
return model_fn_lib.EstimatorSpec(
mode, predictions=predictions, export_outputs=export_outputs)
loss = losses.mean_squared_error(labels, predictions)
train_op = training.GradientDescentOptimizer(learning_rate=0.5).minimize(
loss, training.get_global_step())
eval_metric_ops = {
'absolute_error': metrics_lib.mean_absolute_error(
labels, predictions)
}
return model_fn_lib.EstimatorSpec(
mode,
predictions=predictions,
loss=loss,
train_op=train_op,
eval_metric_ops=eval_metric_ops,
export_outputs=export_outputs)
示例3: model_fn_global_step_incrementer
def model_fn_global_step_incrementer(features, labels, mode):
_, _ = features, labels
global_step = training.get_global_step()
return model_fn_lib.EstimatorSpec(
mode,
loss=constant_op.constant(1.),
train_op=state_ops.assign_add(global_step, 1))
示例4: _create_global_step
def _create_global_step(self, graph):
"""Creates a global step suitable for TPUs.
Args:
graph: The graph in which to create the global step.
Returns:
A global step `Tensor`.
Raises:
ValueError: if the global step tensor is already defined.
"""
graph = graph or ops.get_default_graph()
if training.get_global_step(graph) is not None:
raise ValueError('"global_step" already exists.')
# Create in proper graph and base name_scope.
with graph.as_default() as g, g.name_scope(None):
return variable_scope.get_variable(
ops.GraphKeys.GLOBAL_STEP,
shape=[],
dtype=dtypes.int32,
initializer=init_ops.zeros_initializer(),
trainable=False,
use_resource=True,
collections=[ops.GraphKeys.GLOBAL_VARIABLES,
ops.GraphKeys.GLOBAL_STEP])
示例5: model_fn_diff_modes
def model_fn_diff_modes(features, labels, mode):
_, _ = features, labels
v = variables.Variable(21, name='some_var')
train_op = None
loss = constant_op.constant(104)
if mode == model_fn_lib.ModeKeys.TRAIN:
loss = constant_op.constant(105)
predictions = constant_op.constant([501])
train_op = control_flow_ops.group(
state_ops.assign_add(training.get_global_step(), 1),
state_ops.assign_add(v, 3))
elif mode == model_fn_lib.ModeKeys.EVAL:
loss = constant_op.constant(106)
predictions = constant_op.constant([502])
else:
loss = constant_op.constant(107)
predictions = constant_op.constant([503])
return model_fn_lib.EstimatorSpec(
mode,
loss=loss,
train_op=train_op,
eval_metric_ops={
'abs_err': metrics_lib.mean_absolute_error(
constant_op.constant(0), predictions)},
predictions=predictions)
示例6: model_fn
def model_fn(features, labels, mode):
_ = labels
with ops.control_dependencies([features['x']]):
loss = features['x'][1][0]
return model_fn_lib.EstimatorSpec(
mode,
loss=loss,
train_op=state_ops.assign_add(training.get_global_step(), 1))
示例7: model_fn
def model_fn(features, mode):
del features
global_step = training.get_global_step()
return estimator_lib.EstimatorSpec(
mode,
loss=constant_op.constant([5.]),
predictions={'x': constant_op.constant([5.])},
train_op=global_step.assign_add(1))
示例8: _model_fn_with_incremental_loss
def _model_fn_with_incremental_loss(features, labels, mode):
_, _ = features, labels
local_weight = variables.Variable(
0., name='local_weight', collections=[ops.GraphKeys.LOCAL_VARIABLES])
# Loss will be 2, 4, 6, ...
loss = 2 * state_ops.assign_add(local_weight, 1.)
return model_fn_lib.EstimatorSpec(
mode,
loss=loss,
train_op=state_ops.assign_add(training.get_global_step(), 1))
示例9: _create_and_assert_global_step
def _create_and_assert_global_step(self, graph):
"""Creates and asserts properties of the global step.
Args:
graph: The graph in which to create the global step tensor.
Returns:
The global step `Tensor`.
"""
step = self._create_global_step(graph)
assert step == training.get_global_step()
assert step.dtype.is_integer
return step
示例10: _model_fn_with_eval_metric_ops
def _model_fn_with_eval_metric_ops(features, labels, mode, params):
_, _ = features, labels
metric_name = params.get('metric_name') or 'metric'
metric_value = params.get('metric_value') or 2.
global_step = training.get_global_step()
loss = constant_op.constant(1.)
metric_update_op = loss.op
metric_tensor = control_flow_ops.with_dependencies(
[metric_update_op], constant_op.constant(metric_value))
return model_fn_lib.EstimatorSpec(
mode,
loss=loss,
predictions={'predictions': constant_op.constant(1.)},
train_op=state_ops.assign_add(global_step, 1),
eval_metric_ops={metric_name: (metric_tensor, metric_update_op)})
示例11: _model_fn
def _model_fn(features, labels, mode, config, params=None):
"""model_fn."""
# TODO(jhseu): Move to EVAL and PREDICT to TPU.
if mode != model_fn_lib.ModeKeys.TRAIN:
return _call_model_fn_without_tpu(
model_fn, features, labels, mode, config, params)
# Now for TPU training.
if params is not None and _BATCH_SIZE_KEY in params:
params[_BATCH_SIZE_KEY] //= config.tpu_config.num_shards
assert isinstance(features, _PerShardOutput)
features = features.as_list()
if labels is not None:
assert isinstance(labels, _PerShardOutput)
labels = labels.as_list()
dequeue_fn, enqueue_fn = (
_create_infeed_enqueue_ops_and_dequeue_fn(config, features, labels))
loss = _train_on_tpu_shards(
config,
train_step=_convert_model_fn_to_train_step(
model_fn, dequeue_fn, mode, config, params))
# Gets the variables back from TPU nodes. This means the variables updated
# by TPU will now be *synced* to host memory.
update_ops = [
array_ops.check_numerics(v.read_value(),
'Gradient for %s is NaN' % v.name).op
for v in variables.trainable_variables()
]
hooks = [
TpuInfeedSessionHook(config, enqueue_fn),
training.LoggingTensorHook(
{'loss': array_ops.identity(loss),
'step': training.get_global_step()},
every_n_secs=30)
]
return model_fn_lib.EstimatorSpec(
mode,
loss=array_ops.identity(loss),
training_hooks=hooks,
train_op=control_flow_ops.group(*update_ops))
示例12: _build_train_op
def _build_train_op(self, loss):
"""Creates the training operation,
In case of use_target_network == True, we append also the update op
while taking into account the update_frequency.
"""
train_op = super(BaseQModel, self)._build_train_op(loss)
# check if we need to update the target graph
if self.use_target_graph:
update_op = tf.cond(
tf.equal(tf.mod(training.get_global_step(), self.target_update_frequency), 0),
self._build_update_target_graph,
lambda: tf.no_op(name='no_op_copy_target'))
# append the target update op to the train op.
train_op = tf.group(*[train_op, update_op], name='train_and_update_target')
return train_op
示例13: _model_fn
def _model_fn(features, labels, mode, config, params):
"""A Estimator `model_fn` for TPUEstimator."""
model_fn_wrapper = _ModelFnWrapper(model_fn, config, params, mode,
train_batch_size)
# TODO(jhseu): Move to EVAL and PREDICT to TPU.
if not use_tpu or mode != model_fn_lib.ModeKeys.TRAIN:
return model_fn_wrapper.call_without_tpu(features, labels)
inputs = _InputsHolder(features=features, labels=labels,
num_shards=config.tpu_config.num_shards)
dequeue_fn, enqueue_fn = _create_infeed_enqueue_ops_and_dequeue_fn(
inputs, config)
loss = _train_on_tpu_system(model_fn_wrapper, dequeue_fn)
# Gets the variables back from TPU nodes. This means the variables updated
# by TPU will now be *synced* to host memory.
update_ops = [
array_ops.check_numerics(v.read_value(),
'Gradient for %s is NaN' % v.name).op
for v in variables.trainable_variables()
]
hooks = [
TPUInfeedSessionHook(config, enqueue_fn),
training.LoggingTensorHook(
{'loss': array_ops.identity(loss),
'step': training.get_global_step()},
every_n_secs=30)
]
return model_fn_lib.EstimatorSpec(
mode,
loss=array_ops.identity(loss),
training_hooks=hooks,
train_op=control_flow_ops.group(*update_ops))
示例14: testSaveAndLoadSavedModelExport
def testSaveAndLoadSavedModelExport(
self, model_builder, uses_learning_phase, optimizer, train_before_export):
saved_model_path = self._save_model_dir()
with self.session(graph=ops.Graph()):
np.random.seed(130)
input_arr = np.random.random((1, 3))
target_arr = np.random.random((1, 3))
model = model_builder(uses_learning_phase)
if optimizer is not None:
model.compile(
loss='mse',
optimizer=optimizer,
metrics=['mae'])
if train_before_export:
model.train_on_batch(input_arr, target_arr)
ref_loss, ref_mae = model.evaluate(input_arr, target_arr)
ref_predict = model.predict(input_arr)
# Export SavedModel
output_path = keras_saved_model.save_keras_model(model, saved_model_path)
input_name = model.input_names[0]
output_name = model.output_names[0]
target_name = output_name + '_target'
# Load predict graph, and test predictions
with session.Session(graph=ops.Graph()) as sess:
inputs, outputs, _ = load_model(sess, output_path,
model_fn_lib.ModeKeys.PREDICT)
predictions = sess.run(outputs[output_name],
{inputs[input_name]: input_arr})
self.assertAllClose(ref_predict, predictions, atol=1e-05)
if optimizer:
# Load eval graph, and test predictions, loss and metric values
with session.Session(graph=ops.Graph()) as sess:
inputs, outputs, _ = load_model(sess, output_path,
model_fn_lib.ModeKeys.EVAL)
# First obtain the loss and predictions, and run the metric update op by
# feeding in the inputs and targets.
loss, predictions, _ = sess.run(
(outputs['loss'], outputs['predictions/' + output_name],
outputs['metrics/mean_absolute_error/update_op']), {
inputs[input_name]: input_arr,
inputs[target_name]: target_arr
})
# The metric value should be run after the update op, to ensure that it
# reflects the correct value.
metric_value = sess.run(outputs['metrics/mean_absolute_error/value'])
self.assertEqual(int(train_before_export),
sess.run(training_module.get_global_step()))
self.assertAllClose(ref_loss, loss, atol=1e-05)
self.assertAllClose(ref_mae, metric_value, atol=1e-05)
self.assertAllClose(ref_predict, predictions, atol=1e-05)
# Load train graph, and check for the train op, and prediction values
with session.Session(graph=ops.Graph()) as sess:
inputs, outputs, meta_graph_def = load_model(
sess, output_path, model_fn_lib.ModeKeys.TRAIN)
self.assertEqual(int(train_before_export),
sess.run(training_module.get_global_step()))
self.assertIn('loss', outputs)
self.assertIn('metrics/mean_absolute_error/update_op', outputs)
self.assertIn('metrics/mean_absolute_error/value', outputs)
self.assertIn('predictions/' + output_name, outputs)
# Train for a step
train_op = loader_impl.get_train_op(meta_graph_def)
train_outputs, _ = sess.run(
[outputs, train_op], {inputs[input_name]: input_arr,
inputs[target_name]: target_arr})
self.assertEqual(int(train_before_export) + 1,
sess.run(training_module.get_global_step()))
if uses_learning_phase:
self.assertAllClose(
[[0, 0, 0]], train_outputs['predictions/' + output_name],
atol=1e-05)
else:
self.assertNotAllClose(
[[0, 0, 0]], train_outputs['predictions/' + output_name],
atol=1e-05)
示例15: get_grad_multiplier
def get_grad_multiplier(self):
if self._grad_multiplier_fn:
return ops.convert_to_tensor(
self._grad_multiplier_fn(training.get_global_step()),
dtype=dtypes.float32)